HDOJ 1150 Machine Schedule
用二分图的最大匹配来求解题目,关于其思想详见:http://blog.csdn.net/xinhanggebuguake/article/details/6668071
关于增广了路径的查找很多时候很纠结为什么这样实现。仔细分析不难发现,link本身就是存储了已有的增广路径,在①处的操作刚好就是取反的过程
View Code
1 //#include<fstream>
2 #include<iostream>
3 using namespace std;
4 const int MAX = 102;
5 bool linkMap[MAX][MAX];
6 int crossPath[MAX];
7 bool used[MAX];
8 int n, m;
9 bool search(int u)
10 {
11 for (int i=1;i<m;i++)
12 {
13 if (linkMap[u][i]&&!used[i])
14 {
15 used[i]=1;//保证路径上无重复点出现
16 if (crossPath[i]==-1||search(crossPath[i]))
17 {
18 crossPath[i]=u;//①增广路径的取反
19 return true;
20 }
21 }
22 }
23 return false;
24 }
25
26 int hungary()
27 {
28 int cnt = 0;
29 memset(crossPath, -1, sizeof(crossPath));
30 for(int i= 1; i<n; i++)
31 {
32 memset(used,0, sizeof(used));
33 if(search(i))
34 cnt++;
35 }
36 return cnt;
37 }
38 int main()
39 {
40 //ifstream cin("Machine Schedule.txt");
41 int k;
42 while(cin>>n,n)
43 {
44 cin>>m>>k;
45 memset(linkMap,false, sizeof(linkMap));
46 for(int i =0; i < k; i++)
47 {
48 int v1, v2;
49 cin>>v1>>v1>>v2;
50 if(v1&&v2)
51 linkMap[v1][v2] = true;
52 }
53 cout<<hungary()<<endl;
54 }
55 return 0;
56 }

浙公网安备 33010602011771号