博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3281 Dining
阅读量:5817 次
发布时间:2019-06-18

本文共 3825 字,大约阅读时间需要 12 分钟。

Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17734   Accepted: 7908

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

Source

 

题意:n头牛,f种食物,d种饮料,

每头牛有喜欢的食物、饮料,

每头牛只能分配一种食物和一种饮料

问最后最多能满足多少头牛

 

题解:把牛拆点,放在中间,左边食物,右边饮料,最大流

#include
#include
#include
#define N 501#define M N*Nusing namespace std;int src,decc,ans;int from[N],to[M],nxt[M],tot=1;int cap[M];int cur[N],lev[N];queue
q;void add(int u,int v,int f){ to[++tot]=v; nxt[tot]=from[u]; from[u]=tot; cap[tot]=f; to[++tot]=u; nxt[tot]=from[v]; from[v]=tot; cap[tot]=0;}bool bfs(){ while(!q.empty()) q.pop(); for(int i=src;i<=decc;i++) cur[i]=from[i],lev[i]=-1; q.push(src); lev[src]=0; int now; while(!q.empty()) { now=q.front(); q.pop(); for(int i=from[now];i;i=nxt[i]) { if(lev[to[i]]==-1&&cap[i]>0) { lev[to[i]]=lev[now]+1; if(to[i]==decc) return true; q.push(to[i]); } } } return false;}int dinic(int now,int flow){ if(now==decc) return flow; int rest=0,delta; for(int &i=cur[now];i;i=nxt[i]) { if(lev[to[i]]>lev[now]&&cap[i]>0) { delta=dinic(to[i],min(cap[i],flow-rest)); if(delta) { cap[i]-=delta; cap[i^1]+=delta; rest+=delta; if(rest==flow) break; } } } if(rest!=flow) lev[now]=-1; return rest;}int main(){ int n,f,d,fi,di,x; scanf("%d%d%d",&n,&f,&d); decc=(n<<1|1)+f+d+1; for(int i=1;i<=f;i++) add(src,(n<<1|1)+i,1); for(int i=1;i<=d;i++) add((n<<1|1)+f+i,decc,1); for(int i=1;i<=n;i++) add(i<<1,i<<1|1,1); for(int i=1;i<=n;i++) { scanf("%d%d",&fi,&di); for(int j=1;j<=fi;j++) { scanf("%d",&x); add((n<<1|1)+x,i<<1,1); } for(int j=1;j<=di;j++) { scanf("%d",&x); add(i<<1|1,(n<<1|1)+f+x,1); } } while(bfs()) ans+=dinic(src,N); printf("%d",ans);}

错误:拆点方式:i<<1,i<<1|1,所以最后一头牛的编号为n<<1|1

 

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/6881404.html

你可能感兴趣的文章
如何用纯 CSS 为母亲节创作一颗像素画风格的爱心
查看>>
Linux基础命令---rmdir
查看>>
iOS sqlite3(数据库)
查看>>
粤出"飞龙",打造新制造广东样本
查看>>
编玩边学获数千万元A轮融资,投资方为君联资本
查看>>
蓝图(Blueprint)详解
查看>>
Spark之SQL解析(源码阅读十)
查看>>
Android图片添加水印图片并把图片保存到文件存储
查看>>
比特币系统采用的公钥密码学方案和ECDSA签名算法介绍——第二部分:代码实现(C语言)...
查看>>
海贼王十大悲催人物
查看>>
BigDecimal 舍入模式(Rounding mode)介绍
查看>>
开源 免费 java CMS - FreeCMS1.2-标签 infoSign
查看>>
开源 免费 java CMS - FreeCMS1.9 移动APP生成栏目列表数据
查看>>
git reset 三种用法总结
查看>>
虚拟机新增加硬盘,不用重启读到新加的硬盘
查看>>
Java IO流详尽解析
查看>>
邮件服务系列之四基于虚拟用户的虚拟域的邮件系统(安装courier-authlib以及部分配置方法)...
查看>>
Linux VSFTP服务器
查看>>
DHCP中继数据包互联网周游记
查看>>
Squid 反向代理服务器配置
查看>>