Dining 【dicnic 求最大流】+【拆点 建模】(解匹配问题)

来源:互联网 发布:医疗大数据案例 编辑:程序博客网 时间:2024/06/06 20:07

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

看题,觉得匹配应该可以解,最近在学 最大流,就用最大流解一下。
思路 建立 超级源点,和超级汇点,使其 流向为
S–>食物–>左奶牛–>右奶牛–>饮料–>T
(注意这里的权值都是一) 这里容易写成 一头奶牛,如果只有一头牛的话,那么对于每头牛来说,他可能吃两份食物和两份水,这样肯定是不行的,所以 要拆点,分为左奶牛和右奶牛,让他们之间的权值也为1 , 这样才符合题意(注意 : 这里一定要理解,要不然以后建模的时候 就无法把握 边权值的 含义 ,就懵逼了 )
代码

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#define LL long long#define M  200000+100 #define inf 0x3f3f3f3fusing namespace std;void read(int &x){    x=0;char c;    while((c=getchar())<'0');    do x=x*10+c-'0';while((c=getchar())>='0');}struct Edge {    int from,to,cap,flow,next;}edge[M];int head[M],cur[M],dis[M],top;void init(){    top=0;    memset(head,-1,sizeof(head));}void addedge(int a,int b,int c){    Edge e={a,b,c,0,head[a]};    edge[top]=e;head[a]=top++;    Edge ee={b,a,0,0,head[b]};    edge[top]=ee;head[b]=top++;} queue<int>Q;int bfs(int st,int ed){    while(!Q.empty()) Q.pop();    memset(dis,-1,sizeof(dis));    dis[st]=0;Q.push(st); int now,nexts;    while(!Q.empty())    {        now=Q.front();Q.pop();        for(int i=head[now];i!=-1;i=edge[i].next)        {            Edge e=edge[i];            if(dis[e.to]==-1&&e.cap-e.flow>0)            {                dis[e.to]=dis[now]+1;                if(e.to==ed) return 1;                Q.push(e.to);            }        }     }    return 0; }int dfs(int now,int a,int ed){    if(a==0||now==ed) return a;    int flow=0,f;    for(int& i=cur[now];i!=-1;i=edge[i].next)    {        Edge& e=edge[i];        if(dis[e.to]==dis[now]+1&&(f=dfs(e.to,min(a,e.cap-e.flow),ed))>0)        {            e.flow+=f;            edge[i^1].flow-=f;            a-=f;            flow+=f;            if(a==0) break;        }    }    return flow;}int max_flow(int st,int ed ){    int flow=0;    while(bfs(st,ed))    {        memcpy(cur,head,sizeof(head));        flow+=dfs(st,inf,ed);    }    return flow;}吗int main(){    int st,ed;    int n,f,d; read(n);read(f);read(d);    init();int i,j;    st=0; ed=f+n*2+d+1;    for(i=1;i<=f;i++) // 源点和食物的链接    addedge(st,i,1);    for(i=1;i<=d;i++)// 饮料与汇点的链接    addedge(f+2*n+i,ed,1);int fnum,dnum;    for(i=1;i<=n;i++)    {        read(fnum);read(dnum);        while(fnum--)        {            read(j);            addedge(j,f+i,1); // 食物和左奶牛的连接         }         while(dnum--)         {            read(j);            addedge(f+n+i,f+n+n+j,1); // 右奶牛和饮料的链接         }         addedge(f+i,f+n+i,1); //左奶牛和右奶牛的链接     }    printf("%d\n",max_flow(0,ed));    return 0;}
0 0
原创粉丝点击