图论算法 住宿安排

来源:互联网 发布:g71车内孔编程实例 编辑:程序博客网 时间:2024/06/10 00:09

 

 

package com.bluecup.org;import java.util.ArrayList;import java.util.Arrays;import java.util.Scanner;public class CheckIn {/** * @param args */static boolean check[][] = new boolean[20][2];static int v[][] = new int[20][2];static int f[][] = new int[20][20];static boolean used[] = new boolean[20];static int linkey[] = new int[20];static int n, m;static ArrayList<Integer> a = new ArrayList<Integer>();static ArrayList<Integer> b = new ArrayList<Integer>();public static void main(String[] args) {// TODO Auto-generated method stubScanner scan = new Scanner(System.in);n = scan.nextInt();m = scan.nextInt();for (int i = 0; i < m; i++) {v[i][0] = scan.nextInt();v[i][1] = scan.nextInt();f[v[i][0]][v[i][1]] = f[v[i][1]][v[i][0]] = 1;}if (divide(0)) {for (int i = 0; i <= n; i++) {if (check[i][0]) {a.add(i);}if (check[i][1]) {b.add(i);}}int t = 0;for (int i = 0; i < a.size(); i++) {Arrays.fill(used, false);if (hungary(i))t++;}System.out.println(t);for (int i = 0; i < b.size(); i++) {System.out.println(b.get(i) + " " + a.get(linkey[i]));}}else{System.out.println("no");}}private static boolean hungary(int i) {// TODO Auto-generated method stubfor (int j = 0; j < b.size(); j++) {if (used[j] == false && f[a.get(i)][b.get(j)] == 1) {used[j] = true;if (linkey[j] == 0 || hungary(linkey[j])) {linkey[j] = i;return true;}}}return false;}private static boolean divide(int x) {// TODO Auto-generated method stubif (x == m)return true;if (check[v[x][0]][0] && check[v[x][1]][0]|| (check[v[x][0]][1] && check[v[x][1]][1]))return false;if (check[v[x][0]][0]) {check[v[x][1]][1] = true;if (divide(x + 1))return true;check[v[x][1]][1] = false;} else if (check[v[x][0]][1]) {check[v[x][1]][0] = true;if (divide(x + 1))return true;check[v[x][1]][0] = false;} else if (check[v[x][1]][0]) {check[v[x][0]][1] = true;if (divide(x + 1))return true;check[v[x][0]][1] = false;} else if (check[v[x][1]][1]) {check[v[x][0]][0] = true;if (divide(x + 1))return true;check[v[x][0]][0] = false;} else {check[v[x][0]][0] = true;check[v[x][1]][1] = true;if (divide(x + 1))return true;check[v[x][0]][0] = false;check[v[x][1]][1] = false;check[v[x][0]][1] = true;check[v[x][1]][0] = true;if (divide(x + 1))return true;check[v[x][0]][1] = false;check[v[x][1]][0] = false;}return false;}}


 

0 0
原创粉丝点击