[POJ3281] Dining
标签 :网络流 POJ
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
Line 1: Three space-separated integers: N, F, and D
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
Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes
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
One way to satisfy three cows is:
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.
题解
- 此题很像二分图匹配的模型。但是我们发现如果将牛与食物、饮料分别连边的话肯定不行。因为只有一只牛同时有了食物和饮料,他才能对答案做出1的贡献。因此,我们考虑网络流的常用技巧——”拆点"
- 把一头牛拆成入点和出点,再将食物向入连一条容量为1的弧,出点向饮料连一条容量为1的弧,再将源点向食物相连,饮料想汇点相连。最后跑一遍最大流就行了。
Code
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
#define inf 1<<30
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,a) for(int i=start[(a)];i;i=e[i].next)
inline int read()
{
int sum=0,p=1;char ch=getchar();
while(!(('0'<=ch && ch<='9') || ch=='-'))ch=getchar();
if(ch=='-')p=-1,ch=getchar();
while('0'<=ch && ch<='9')sum=sum*10+ch-48,ch=getchar();
return sum*p;
}
const int maxn=410;
struct node {
int v,next,w;
};
node e[maxn*410];
int cnt=1,start[maxn];
int n,cn,fn,dn,S,T;
void addedge(int u,int v,int w)
{
e[++cnt]={v,start[u],w};
start[u]=cnt;
}
void init()
{
memset(e,0,sizeof(e));
memset(start,0,sizeof(start));
n=2*cn+fn+dn+1;
S=0,T=n;
cnt=1;
REP(i,1,cn)
{
int Food_cnt=read();
int Drink_cnt=read();
REP(j,1,Food_cnt)
{
int Food=read();
addedge(2*i-1,2*cn+Food,0);
addedge(2*cn+Food,2*i-1,1);
}
addedge(2*i-1,2*i,1);
addedge(2*i,2*i-1,0);
REP(j,1,Drink_cnt)
{
int Drink=read();
addedge(2*i,2*cn+fn+Drink,1);
addedge(2*cn+fn+Drink,2*i,0);
}
}
REP(i,1,fn)
{
addedge(0,2*cn+i,1);
addedge(2*cn+i,0,0);
}
REP(i,1,dn)
{
addedge(n,2*cn+fn+i,0);
addedge(2*cn+fn+i,n,1);
}
}
int lev[maxn];
inline bool bfs()
{
memset(lev,0,sizeof(lev));
queue <int> q;
lev[S]=1;
q.push(S);
do{
int u=q.front();q.pop();
EREP(i,u)
{
int v=e[i].v;
if(!lev[v] && e[i].w)
{
lev[v]=lev[u]+1;
q.push(v);
if(v==T)return true;
}
}
}while(!q.empty());
return false;
}
int ans=0;
int cur[maxn];
int dfs(int u,int flow)
{
if(u==T)return flow;
int tag=0,c=0;
for(int &i=cur[u];i;i=e[i].next)
{
int v=e[i].v;
if(lev[v]==lev[u]+1 && e[i].w)
{
c=dfs(v,min(flow-tag,e[i].w));
e[i].w-=c;
e[i^1].w+=c;
tag+=c;
if(tag==flow)return tag;
}
}
return tag;
}
void doing()
{
ans=0;
while(bfs())
{
REP(i,0,n)cur[i]=start[i];
ans+=dfs(S,inf);
}
cout<<ans<<endl;
}
int main()
{
while(scanf("%d%d%d",&cn,&fn,&dn)==3)
{
init();
doing();
}
return 0;
}