杭电2063 过山车 匈牙利算法
过山车
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19179 Accepted Submission(s): 8375
1<=N 和M<=500.接下来的K行,每行有两个数,分别表示女生Ai愿意和男生Bj做partner。最后一个0结束输入。
#include<iostream>
#include<cstring>
using namespace std;
int iCount,K,iBoyNum,iGirlNum,iMap[1000][1000],iVisit[1000],iLink[1000];
bool dfs(int x)
{
for(int i =0;i < iBoyNum;i++)
{
if(iMap[x][i] && iVisit[i] == 0)
{
iVisit[i] = 1;
if(iLink[i] == -1 || dfs(iLink[i]))
{
iLink[i] = x;
return true;
}
}
}
return false;
}
int main()
{
int iG,iB;
while(cin>>K && K)
{
iCount = 0;
cin>>iGirlNum>>iBoyNum;
memset(iMap,0,sizeof(iMap));
memset(iLink,-1,sizeof(iLink));
for(int i = 0;i < K;i++)
{
cin>>iG>>iB;
iMap[iG-1][iB-1] = 1;
}
for(int i = 0;i < iGirlNum;i++)
{
memset(iVisit,0,sizeof(iVisit));
if(dfs(i))
iCount++;
}
cout<<iCount<<endl;
}
return 0;
}