您的位置:首页 > 理论基础 > 计算机网络

POJ 3281 Dining(网络流,最大流ek记录板子)

2018-01-11 20:52 417 查看
Dining

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 20182 Accepted: 8970
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.

最大流ek算法参考博客:点击打开链接

题意:有n头牛,f种食物,d种饮料,每头牛有x种喜欢的食物,y种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种食物了,饮料也同理,问最多有多少头牛可以吃到它喜欢的饮料和食物。

思路:这个题如果不是饮料和食物同时选那么就可以用二分图了,但这个不行那么就用网络流了,加个源点和汇点。源点与食物、饮料和汇点的边容量都是1,表示每种食物和饮料只有一个。但是要注意的是要拆点,因为不拆点会造成一个牛会吃多份。

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
int f[500][500];//流量
int c[500][500];//容量
int a[500],pre[500];//a是标记加保存最小的容量与流量的差值
int ek(int s,int t)
{
int sum=0;
queue<int>q;
while(1)
{
memset(a,0,sizeof(a));
a[s]=inf;
q.push(s);
while(!q.empty())//找增广路
{
int u=q.front();
q.pop();
for(int v=s;v<=t;v++)
{
if(!a[v]&&c[u][v]>f[u][v])
{
a[v]=min(a[u],c[u][v]-f[u][v]);//上一节点能流的流量和路径未被使用的部分比较
pre[v]=u;
q.push(v);
}
}
}
if(!a[t]) break;//找不到就跳出
for(int i=t;i!=s;i=pre[i])//调整流量
{
f[pre[i]][i]+=a[t];
f[i][pre[i]]-=a[t];//为了让所有的残余流量都能用上
}
sum+=a[t];
}
return sum;
}
int main()
{
int n,ff,d,a,b,xf,xd;
while(~scanf("%d%d%d",&n,&ff,&d))
{
memset(c,0,sizeof(c));
memset(f,0,sizeof(f));
for(int i=1;i<=ff;i++)
c[0][i]=1;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
while(a--)
{
scanf("%d",&xf);
c[xf][i+ff]=1;
}
c[i+ff][i+ff+n]=1;
while(b--)
{
scanf("%d",&xd);
c[i+ff+n][xd+ff+2*n]=1;
}
}
for(int i=1;i<=d;i++)
c[ff+2*n+i][ff+2*n+d+1]=1;
printf("%d\n",ek(0,ff+2*n+d+1));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: