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(!flow)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 }

 

 

 

转载于:https://www.cnblogs.com/five20/p/8146380.html

以下是P4087 [USACO17DEC]Milk Measurement的c++代码: ```c++ #include<bits/stdc++.h> using namespace std; int n,d,i,x,minn=1e9,maxn=-1e9,sum=7;//注意sum要初始化为7,因为一开始有三个人挤奶! map<int,int> mp; struct node{ int day,milk,id;//day表示某一天,milk表示这一天的产奶量,id表示这头牛的编号 }a[100010]; bool cmp(node x,node y){ return x.day<y.day; } int main(){ scanf("%d%d",&n,&d); for(i=1;i<=n;i++){ scanf("%d%d%d",&a[i].day,&a[i].id,&a[i].milk); minn=min(minn,a[i].id);//记录最小的牛的编号 maxn=max(maxn,a[i].id);//记录最大的牛的编号 } sort(a+1,a+n+1,cmp);//排序 for(i=1;i<=n;i++){ int p=a[i].id; mp[p]+=a[i].milk;//记录每头牛产奶总量 if(mp[p]-a[i].milk>=mp[minn]&&mp[p]>=mp[minn]){//如果这头牛的产奶总量减去这一天的产奶量后等于最小产奶量且这头牛的产奶总量大于等于最小产奶量 sum--; } if(mp[p]>=mp[maxn]&&mp[p]-a[i].milk<mp[maxn]){//如果这头牛的产奶总量大于等于最大产奶量且这头牛的产奶总量减去这一天的产奶量小于最大产奶量 sum++; } if(mp[p]-a[i].milk<mp[maxn]&&mp[p]>=mp[maxn]){//如果这头牛的产奶总量减去这一天的产奶量小于最大产奶量且这头牛的产奶总量大于等于最大产奶量 if(mp[maxn]-mp[p]+a[i].milk>0)sum++; } mp[p]-=a[i].milk;//减去这一天的产奶量 if(i==n||a[i].day!=a[i+1].day){//如果到了新的一天或者到了最后一天 if(mp[maxn]!=mp[a[i].id]&&mp[a[i].id]>=mp[maxn])sum++;//如果这头牛的产奶总量不等于最大产奶量且这头牛的产奶总量大于等于最大产奶量 if(mp[maxn]==mp[a[i].id]){//如果这头牛的产奶总量等于最大产奶量 if(a[i].id==maxn)sum+=0;//如果这头牛就是最大产奶量的牛,那么不需要增加计数器 else sum++;//否则需要增加计数器 } if(mp[minn]!=mp[a[i].id]&&mp[a[i].id]>=mp[minn])sum++;//如果这头牛的产奶总量不等于最小产奶量且这头牛的产奶总量大于等于最小产奶量 if(mp[minn]==mp[a[i].id]){ if(a[i].id==minn)sum+=0;//如果这头牛就是最小产奶量的牛,那么不需要增加计数器 else sum++;//否则需要增加计数器 } } } printf("%d\n",sum); return 0; } ``` 该题的解题思路是模拟,需要注意细节问题。我们可以首先将输入的数据按天数排序,然后模拟每一天挤奶的情况,并根据题目要求进行计数即可。具体细节请见代码注释。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值