POJ 3281 Dining (网络流最大流 拆点建图 Edmonds-Karp算法)

来源:互联网 发布:lol比赛数据查询 编辑:程序博客网 时间:2024/06/14 17:32

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: NF, 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 Fiintegers 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 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 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.

题意:有n 头牛 f 个食物 d 个饮料各一份  每头牛需要1份套餐(1份食物和1份饮料) 问 最多可以满足多少头牛 

解 : 很容易想到  源点-食物-牛-饮料-汇点 但是这样会出现1头牛吃了多份套餐的可能 为了保证每头牛只吃一份 我们可以 源点-食物-牛左-牛右-饮料-汇点 


#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<map>#include<vector>#include<queue>const int INF = 0x3f3f3f3f;using namespace std;int n,food,drink;int c[405][405];//容量int f[405][405];//流量int a[405];//这里的a数组右两个作用 1.标记是否访问过 2.记录增广路上的瓶颈int pre[405];//记录增广路int ek(int s,int t){    int ans=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]==0)            break;       for(int u=t;u!=s;u=pre[u])//调整流量大小       {           f[pre[u]][u]+=a[t];           f[u][pre[u]]-=a[t];//为了让所有的残余流量都能用上       }        ans+=a[t];    }   return ans;}int main(){     int nf,nd,getf,getd;       memset(c,0,sizeof(c));       memset(f,0,sizeof(f));    scanf("%d%d%d",&n,&food,&drink);       for(int i=1;i<=food;i++) //源点 0         c[0][i]=1;       for(int i=1;i<=n;i++)       {           scanf("%d%d",&nf,&nd);           while(nf--)//食物编号 1 - f           {               scanf("%d",&getf);               c[getf][food+i]=1;           }           c[food+i][food+n+i]=1;//牛左 f+1 - f+n     牛右  f+n+1 - f+2n           while(nd--)//饮料  f+2n+1 - f+2n+d           {               scanf("%d",&getd);               c[food+n+i][food+2*n+getd]=1;           }       }       for(int i=1;i<=drink;i++)        c[food+2*n+i][food+2*n+drink+1]=1;         printf("%d\n",ek(0,food+2*n+drink+1)); //汇点  f+2n+d+1}





原创粉丝点击