题意简单,不说了,,明显的匈牙利。。下面给出一种用队列写的匈牙利,以免以后点多了,用数组做不了。。

#include <cstdio>
#include <memory>
#include <vector>
using namespace std;
const int Max=1100;
vector< vector<int> >Bmap;
int n,m,k,nm;
int mark[Max];
bool flag[Max];

bool dfs(int pos)
{
 int i,pre,tp;
 for(i=0;i<Bmap[pos].size();i++)
 {
  tp=Bmap[pos][i];
  if(!flag[tp])
  {
   flag[tp]=true;
   pre=mark[tp];
   mark[tp]=pos;
   if(pre==-1||dfs(pre))
    return true;
   mark[tp]=pre;
  }
 }
 return false;
}
inline int Max_Match()
{
 int mmax=0,i;
 for(i=1;i<=m;i++)
 {
  memset(flag,0,sizeof(flag));
  if(dfs(i))
   mmax++;
 }
 return mmax++;
}
int main()
{
 int j,id,id2;
 while(scanf("%d",&k)==1&&k)
 {
  scanf("%d%d",&m,&n);
  nm=n+m;
  Bmap.clear();
  Bmap.resize(nm+10);
  memset(mark,-1,sizeof(mark));
  for(j=0;j<k;j++)
  {
   scanf("%d%d",&id,&id2);
   id2+=m;
   Bmap[id].push_back(id2);
  }
  printf("%d\n",Max_Match());
 }
 return 0;
}

posted on 2011-08-11 19:35  →木头←  阅读(307)  评论(0编辑  收藏  举报