POJ 3281 Dining(网络流构图)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3281 Dining(网络流构图)相关的知识,希望对你有一定的参考价值。

题目链接:http://poj.org/problem?id=3281

题目:

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: 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 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.
题意:有n只奶牛,每只奶牛都有自己喜欢的食物和饮料。已知每只奶牛喜欢的食物和饮料有若干种。如何分配食物和饮料才能让最多的奶牛满意。
题解:把食物和饮料先分在两端,然后中间把牛分成两个点,给予边值1。再创建一个源点和食物相连,给予边值1,一个汇点和饮料相连,同样给予边值1。然后根据题目中给的奶牛信息连接点,给予边值1即可。最后再跑下网络流就OK啦。(图 画的有点丑...)

技术分享图片

 1 #include <queue>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int N=555;
 9 const int M=250010;
10 const int INF=0x3f3f3f3f;
11 int n,s,t,cnt;
12 int Head[N],Depth[N],Next[M],V[M],W[M];
13 
14 void init(){
15     cnt=-1;
16     memset(Head,-1,sizeof(Head));
17     memset(Next,-1,sizeof(Next));
18 }
19 
20 void add_edge(int u,int v,int w){
21     cnt++;
22     Next[cnt]=Head[u];
23     V[cnt]=v;
24     W[cnt]=w;
25     Head[u]=cnt;
26     cnt++;
27     Next[cnt]=Head[v];
28     V[cnt]=u;
29     W[cnt]=0;
30     Head[v]=cnt;
31 }
32 
33 bool bfs(){
34     queue <int> Q;
35     while(!Q.empty()) Q.pop();
36     memset(Depth,0,sizeof(Depth));
37     Depth[s]=1;
38     Q.push(s);
39     while(!Q.empty()){
40         int u=Q.front();Q.pop();
41         for(int i=Head[u];i!=-1;i=Next[i]){
42             if((W[i]>0)&&(Depth[V[i]]==0)){
43                 Depth[V[i]]=Depth[u]+1;
44                 Q.push(V[i]);
45             }
46         }
47     }
48     if(Depth[t]==0) return 0;
49     return 1;
50 }
51 
52 int dfs(int u,int dist){
53     if(u==t) return dist;
54     for(int i=Head[u];i!=-1;i=Next[i]){
55         if((Depth[V[i]]==Depth[u]+1)&&W[i]!=0){
56             int di=dfs(V[i],min(dist,W[i]));
57             if(di>0){
58                 W[i]-=di;
59                 W[i^1]+=di;
60                 return di;
61             }
62         }
63     }
64     return 0;
65 }
66 
67 int Dinic(){
68     int ans=0;
69     while(bfs()){
70         while(int d=dfs(s,INF))
71         ans+=d;
72     }
73     return ans;
74 }
75 
76 int main(){
77     init();
78     int F,D,a,b,tmp;
79     scanf("%d %d %d",&n,&F,&D);
80     s=0;t=2*n+F+D+1;
81     for(int i=1;i<=n;i++) add_edge(i,i+n,1);
82     for(int i=1;i<=n;i++){
83         scanf("%d %d",&a,&b);
84         for(int j=1;j<=a;j++){
85             scanf("%d",&tmp);
86             add_edge(2*n+tmp,i,1);
87         }
88         for(int j=1;j<=b;j++){
89             scanf("%d",&tmp);
90             add_edge(i+n,2*n+F+tmp,1);
91         }
92     }
93     for(int i=1;i<=F;i++) add_edge(s,2*n+i,1);
94     for(int i=1;i<=D;i++) add_edge(2*n+F+i,t,1);
95 
96     printf("%d",Dinic());
97     return 0;
98 }

 

以上是关于POJ 3281 Dining(网络流构图)的主要内容,如果未能解决你的问题,请参考以下文章

B - Dining POJ - 3281 网络流

poj 3281 Dining(网络流+拆点)

POJ 3281(Dining-网络流拆点)[Template:网络流dinic]

POJ 3281 Dining(网络流拆点)

POJ-3281 Dining 网络流最大流

POJ 3281 Dining (网络流之最大流)