ACM PKU 1325 Machine Schedule http://poj.org/problem?id=1325

第一个二分图的题目,匈牙利算法,跟着老师做完了,展示老师的代码,威武,哈哈

 1 #include <iostream>
2 #include <stdio.h>
3 #include <string.h>
4 using namespace std;
5 const int maxn=105;
6
7 bool flag[maxn];
8 int match[maxn];
9 int map[maxn][maxn];
10
11 int N,M,K;
12
13 bool xyy(int s)
14 {
15 for(int i=0; i<M; i++)
16 {
17 if(map[s][i]&&!flag[i])
18 {
19 flag[i]=1;
20 if(match[i]==-1||xyy(match[i]))
21 {
22 match[i]=s;
23 return true;
24 }
25 }
26 }
27 return false;
28 }
29
30 int main()
31 {
32 //freopen("in.txt","r",stdin);
33 while(scanf("%d",&N))
34 {
35 if(N==0) break;
36 scanf("%d%d",&M,&K);
37 int a,x,y;
38 memset(map,0,sizeof(map));
39 memset(match,-1,sizeof(match));
40 for(int i=0; i<K; i++)
41 {
42 scanf("%d%d%d",&a,&x,&y);
43 if(!x||!y) continue;
44 map[x][y]=1;
45 }
46 int cnt=0;
47 for(int i=1;i<=N;i++)
48 {
49 memset(flag,0,sizeof(flag));
50 if(xyy(i))cnt++;
51 }
52 printf("%d\n",cnt);
53 }
54 return 0;
55 }

  

posted on 2011-08-12 19:03  _Clarence  阅读(134)  评论(0编辑  收藏  举报

导航