Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11833 | Accepted: 5441 |
Description
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.
Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.
Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and preparedD (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.
Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).
Input
Lines 2.. N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fi integers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.
Output
Sample Input
4 3 3 2 2 1 2 3 1 2 2 2 3 1 2 2 2 1 3 1 2 2 1 1 3 3
Sample Output
3
Hint
Cow 1: no meal
Cow 2: Food #2, Drink #2
Cow 3: Food #1, Drink #1
Cow 4: Food #3, Drink #3
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
#define N 410
using namespace std;
int dis[N];
int vis[N];
int c[N];
int head[N],edgenum;
struct zz
{
int from;
int to;
int cap;
int flow;
int next;
}edge[N<<4];
void add(int u,int v,int w)
{
zz E={u,v,w,0,head[u]};
edge[edgenum]=E;
head[u]=edgenum++;
zz EE={v,u,0,0,head[v]};
edge[edgenum]=EE;
head[v]=edgenum++;
}
int n,f,d;
bool bfs(int s,int e)
{
queue<int>q;
memset(dis,-1,sizeof(dis));
memset(vis,0,sizeof(vis));
while(!q.empty())
q.pop();
q.push(s);
dis[s]=0;
vis[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
zz v=edge[i];
if(!vis[v.to]&&v.cap>v.flow)
{
vis[v.to]=1;
dis[v.to]=dis[u]+1;
if(v.to==e)
return true;
q.push(v.to);
}
}
}
return false;
}
int dfs(int x,int a,int e)
{
if(x==e||a==0)
return a;
int flow=0,f;
for(int i=c[x];i!=-1;i=edge[i].next)
{
zz &v=edge[i];
if(dis[v.to]==dis[x]+1&&(f=dfs(v.to,min(a,v.cap-v.flow),e))>0)
{
v.flow+=f;
edge[i^1].flow-=f;
flow+=f;
a-=f;
if(a==0)
break;
}
}
return flow;
}
int maxflow(int s,int e)
{
int flow=0;
while(bfs(s,e))
{
memcpy(c,head,sizeof(head));
flow+=dfs(s,INF,e);
}
return flow;
}
int main()
{
int ff,dd,i,j;
while(scanf("%d%d%d",&n,&f,&d)!=EOF)
{
edgenum=0;
memset(head,-1,sizeof(head));
for(i=1;i<=n;i++)
{
scanf("%d%d",&ff,&dd);
while(ff--)
{
scanf("%d",&j);
add(n*2+j,i,1);
}
while(dd--)
{
scanf("%d",&j);
add(n+i,n*2+f+j,1);
}
add(i,n+i,1);
}
for(i=1;i<=f;i++)
add(0,n*2+i,1);//添加总汇点0
for(i=1;i<=d;i++)
add(n*2+f+i,n*2+f+d+1,1);//添加总结点 n*2+f+d+1
printf("%d\n",maxflow(0,n*2+f+d+1));
}
return 0;
}
//另一种算法
<pre class="cpp" name="code">#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=500;
const int MAXM=50010<<1;
int vis[MAXN],map[MAXN][MAXN],pre[MAXN];
bool bfs(int s,int e)
{
queue<int>dl;
mem(vis,0);
mem(pre,-1);
vis[s]=1;
dl.push(s);
int a;
while(!dl.empty())
{
a=dl.front();
dl.pop();
if(a==e)
return true;
for(int i=1;i<=e;i++)
{
if(!vis[i]&&map[a][i])
{
dl.push(i);
vis[i]=1;
pre[i]=a;
}
}
}
return false;
}
int maxflow(int s,int e)
{
int flow=0;
while(bfs(s,e))
{
int temp=INF;
int r=e;
while(r!=s)
temp=min(temp,map[pre[r]][r]),r=pre[r];
r=e;
while(r!=s)
map[pre[r]][r]-=temp,map[r][pre[r]]+=temp,r=pre[r];
flow+=temp;
}
return flow;
}
int main(){
int n,F,D,f,d,a,b;
while(~scanf("%d%d%d",&n,&F,&D))
{
mem(map,0);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&f,&d);
while(f--)
{
scanf("%d",&a);
map[2*n+a][i]=1;
}
while(d--)
{
scanf("%d",&a);
map[n+i][2*n+F+a]=1;
}
map[i][n+i]=1;
}
for(int i=1;i<=F;i++)
map[0][2*n+i]=1;
for(int i=1;i<=D;i++)
map[2*n+F+i][2*n+F+D+1]=1;
printf("%d\n",maxflow(0,2*n+F+D+1));
}
return 0;
}