Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11844 | Accepted: 5444 |
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 prepared D (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<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=;
const int MAXM=<<;
int head[MAXM];
int vis[MAXN],dis[MAXN];
int edgnum;
struct Node{
int from,to,next,cup,flow;
};
Node edg[MAXM];
queue<int>dl;
void add(int u,int v,int w){
Node E={u,v,head[u],w,};
edg[edgnum]=E;
head[u]=edgnum++;
E={v,u,head[v],,};
edg[edgnum]=E;
head[v]=edgnum++;
}
void initial(){
mem(head,-);edgnum=;
}
bool bfs(int s,int e){
mem(vis,);mem(dis,-);
while(!dl.empty())dl.pop();
dis[s]=;vis[s]=;dl.push(s);
while(!dl.empty()){
int u=dl.front();
dl.pop();
for(int i=head[u];i!=-;i=edg[i].next){
Node v=edg[i];
if(!vis[v.to]&&v.cup>v.flow){
vis[v.to]=;dl.push(v.to);
dis[v.to]=dis[u]+;
if(v.to==e)return true;
}
}
}
return false;
}
int dfs(int x,int la,int e){
if(x==e||la==)return la;
int temp;
int flow=;
for(int i=head[x];i!=-;i=edg[i].next){
Node &v=edg[i];
if(dis[v.to]==dis[x]+&&(temp=dfs(v.to,min(la,v.cup-v.flow),e))>){
v.flow+=temp;
edg[i^].flow-=temp;
la-=temp;
flow+=temp;
if(la==)break;
}
}
return flow;
}
int maxflow(int s,int e){
int flow=;
while(bfs(s,e)){
flow+=dfs(s,INF,e);
}
return flow;
}
int main(){
int n,F,D,f,d,a,b;
while(~scanf("%d%d%d",&n,&F,&D)){
initial();
for(int i=;i<=n;i++){
scanf("%d%d",&f,&d);
while(f--){
scanf("%d",&a);
add(*n+a,i,);
}
while(d--){
scanf("%d",&a);
add(n+i,*n+F+a,);
}
add(i,n+i,);
}
for(int i=;i<=F;i++)add(,*n+i,);
for(int i=;i<=D;i++)add(*n+F+i,*n+F+D+,);
printf("%d\n",maxflow(,*n+F+D+));//醉了,应该从0开始,找了半天错。。。
}
return ;
}
另一种解法:
#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=;
const int MAXM=<<;
queue<int>dl;
int vis[MAXN],map[MAXN][MAXN],pre[MAXN];
bool bfs(int s,int e){
mem(vis,);mem(pre,-);
while(!dl.empty())dl.pop(); //忘了初始化。。。。。
vis[s]=;dl.push(s);
int a;
while(!dl.empty()){
a=dl.front();
dl.pop();//忘写pop了。。。
if(a==e)return true;
for(int i=;i<=e;i++){
if(!vis[i]&&map[a][i]){
dl.push(i);
vis[i]=;
pre[i]=a;
//if(i==e)return true;
}
}
}
return false;
}
int maxflow(int s,int e){
int flow=;
while(bfs(s,e)){
int temp=INF;
int r=e;
//puts("fasf");
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,);
for(int i=;i<=n;i++){
scanf("%d%d",&f,&d);
while(f--){
scanf("%d",&a);
// add(2*n+a,i,1);
map[*n+a][i]=;
}
while(d--){
scanf("%d",&a);
// add(n+i,2*n+F+a,1);
map[n+i][*n+F+a]=;
}
//add(i,n+i,1);
map[i][n+i]=;
}
for(int i=;i<=F;i++)map[][*n+i]=;//add(0,2*n+i,1);
for(int i=;i<=D;i++)map[*n+F+i][*n+F+D+]=;//add(2*n+F+i,2*n+F+D+1,1);
printf("%d\n",maxflow(,*n+F+D+));//醉了,应该从0开始,找了半天错。。。
}
return ;
}