P2891 [USACO07OPEN]吃饭Dining(最大流+拆点)

Posted five20

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P2891 [USACO07OPEN]吃饭Dining(最大流+拆点)相关的知识,希望对你有一定的参考价值。

题目描述

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).

有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料。现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮料。(1 <= f <= 100, 1 <= d <= 100, 1 <= n <= 100)

输入输出格式

输入格式:

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.

输出格式:

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

输入输出样例

输入样例#1: 
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
输出样例#1: 
3

说明

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.

 

Solution:

英文只是来扰人耳目,这道题其实不难,与洛谷P1231类似。由于一只牛最多只能吃一种食物和饮料,所以节点存在限制流量为1,所以要拆点加容量为1的边,然后将食物和饮料分别放在牛的左右两侧,附加炒鸡源S和T,S与食物连容量1的边,T与饮料也连容量1的边,然后跑最大流就好了。。。不懂得可以去看下我P1231的解题博客,这里不多赘述。

代码:

 

 1 #include<bits/stdc++.h>
 2 #define il inline
 3 using namespace std;
 4 const int N=100005,inf=23333333;
 5 queue<int>q;
 6 int s,t,n,f,d,cnt=1,h[N],dis[N],ans;
 7 struct edge{
 8 int to,net,v;
 9 }e[N*2];
10 il void add(int u,int v,int w)
11 {
12     e[++cnt].to=v,e[cnt].net=h[u],e[cnt].v=w,h[u]=cnt;
13     e[++cnt].to=u,e[cnt].net=h[v],e[cnt].v=0,h[v]=cnt;
14 }
15 il bool bfs()
16 {
17     memset(dis,-1,sizeof(dis));
18     dis[s]=0,q.push(s);
19     while(!q.empty())
20     {
21         int u=q.front();q.pop();
22         for(int i=h[u];i;i=e[i].net)
23         if(dis[e[i].to]==-1&&e[i].v>0)dis[e[i].to]=dis[u]+1,q.push(e[i].to);
24     }
25     return dis[t]!=-1;
26 }
27 il int dfs(int u,int op)
28 {
29     if(u==t)return op;
30     int flow=0,used=0;
31     for(int i=h[u];i;i=e[i].net)
32     {
33         int v=e[i].to;
34         if(dis[v]==dis[u]+1&&e[i].v>0){
35             used=dfs(v,min(op,e[i].v));
36             if(!used)continue;
37             flow+=used,op-=used;
38             e[i].v-=used,e[i^1].v+=used;
39             if(!op)break;
40         }
41     }
42     if(!op)dis[u]=-1;
43     return flow;
44 }
45 int main()
46 {
47     scanf("%d%d%d",&n,&f,&d);
48     int u,v,x;t=n*2+f+d+5;
49     for(int i=1;i<=n;i++)add(i+f,i+n+f,1);
50     for(int i=1;i<=f;i++)add(0,i,1);
51     for(int i=1;i<=d;i++)add(i+n*2+f,t,1);
52     for(int i=1;i<=n;i++){
53         scanf("%d%d",&u,&v);
54         for(int j=1;j<=u;j++)scanf("%d",&x),add(x,i+f,1);
55         for(int j=1;j<=v;j++)scanf("%d",&x),add(i+n+f,x+n*2+f,1);
56     }
57     while(bfs())ans+=dfs(s,inf);
58     cout<<ans;
59     return 0;
60 }

 

 

 

以上是关于P2891 [USACO07OPEN]吃饭Dining(最大流+拆点)的主要内容,如果未能解决你的问题,请参考以下文章

[USACO07OPEN]吃饭Dining

[Luogu P2891/POJ 3281/USACO07OPEN ]吃饭Dining

[BZOJ1711][Usaco2007 Open]Dining吃饭

BZOJ 1711: [Usaco2007 Open]Dining吃饭

BZOJ 1711 [Usaco2007 Open]Dining吃饭

BZOJ1711: [Usaco2007 Open]Dining吃饭