标签:
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 const int MAXN = 510; 6 int uN,vN;//u,v的数目,使用前面必须赋值 7 int g[MAXN][MAXN];//邻接矩阵 8 int linker[MAXN]; 9 bool used[MAXN]; 10 bool dfs(int u) 11 { 12 for(int v = 0; v < vN;v++) 13 if(g[u][v] && !used[v]) 14 { 15 used[v] = true; 16 if(linker[v] == -1 || dfs(linker[v])) 17 { 18 linker[v] = u; 19 return true; 20 } 21 } 22 return false; 23 } 24 int hungary() 25 { 26 int res = 0; 27 memset(linker,-1,sizeof(linker)); 28 for(int u = 0;u < uN;u++) 29 { 30 memset(used,false,sizeof(used)); 31 if(dfs(u))res++; 32 } 33 return res; 34 } 35 int main() 36 { 37 int i,j,k; 38 //freopen("1.in","r",stdin); 39 while(scanf("%d",&uN)!=EOF) 40 { 41 if(uN==0) break; 42 scanf("%d%d",&vN,&k); 43 int p,q; 44 memset(g,0,sizeof(g)); 45 while(k--) 46 { 47 scanf("%d%d%d",&i,&p,&q); 48 if(p>0&&q>0) g[p][q]=1; 49 } 50 printf("%d\n",hungary()); 51 } 52 return 0; 53 }
标签:
原文地址:http://www.cnblogs.com/cnblogs321114287/p/4314590.html