link:http://acm.hdu.edu.cn/showproblem.php?pid=2063
匈牙利算法
代码:
View Code
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <iostream> 5 using namespace std; 6 int map[1005][1005]; 7 int used[1005],linker[1005]; 8 int find(int x,int n) 9 { 10 int i; 11 for(i = 0;i <= n;i++) 12 { 13 14 if(map[x][i] && !used[i]) 15 { 16 used[i] = 1; 17 if(!linker[i] || find(linker[i],n)) 18 { 19 linker[i] = x; 20 return 1; 21 } 22 } 23 } 24 return 0; 25 } 26 int main() 27 { 28 int m,n,k,i,j; 29 while(scanf("%d",&n)&&n) 30 { 31 memset(map,0,sizeof(map)); 32 scanf("%d %d",&m,&k); 33 while(n--) 34 { 35 scanf("%d %d",&i,&j); 36 map[i][j] = 1; 37 } 38 memset(used,0,sizeof(used)); 39 40 memset(linker,0,sizeof(used)); 41 42 int ans; 43 ans = 0; 44 for(i = 1;i <= m;i++) 45 { 46 memset(used,0,sizeof(used)); 47 if(find(i,k)) 48 ans++; 49 } 50 cout<<ans<<endl; 51 } 52 return 0; 53 }