poj3281 Dining
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14316 | Accepted: 6491 |
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
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
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
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
【题目大意】
有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料。现在有N头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮料。(1 <= F <= 100, 1 <= D <= 100, 1 <= N <= 100)
【建模方法】
此题的建模方法比较有开创性。以往一般都是左边一个点集表示供应并与源相连,右边一个点集表示需求并与汇相连。现在不同了,供应有两种资源,需求仍只有一个群体,怎么办?其实只要仔细思考一下最大流的建模原理,此题的构图也不是那么难想。最大流的正确性依赖于它的每一条s-t流都与一种实际方案一一对应。那么此题也需要用s-t流将一头牛和它喜欢的食物和饮料“串”起来,而食物和饮料之间没有直接的关系,自然就想到把牛放在中间,两边是食物和饮料,由s, t将它们串起来构成一种分配方案。至此建模的方法也就很明显了:每种食物i作为一个点并连边(s,
i, 1),每种饮料j作为一个点并连边(j, t, 1),将每头牛k拆成两个点k’, k’’并连边(k’, k’’, 1), (i, k’, 1), (k’’, j, 1),其中i, j均是牛k喜欢的食物或饮料。求一次最大流即为结果。(引自《网络流建模汇总》by Edelweiss)
#include<cstdio>
#include<cstring>
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
int N,F,D,s,t,ecnt,first[20100],nxt[20100];
struct Edge{int u,v,cap,flow;}e[20100];
bool vis[20100];
int p[20100],q[20100],d[20100],num[20100],cur[20100];
void Link(int a,int b,int c)
{
e[++ecnt].u=a,e[ecnt].v=b,e[ecnt].cap=c,e[ecnt].flow=0;
nxt[ecnt]=first[a],first[a]=ecnt;
e[++ecnt].u=b,e[ecnt].v=a,e[ecnt].cap=0,e[ecnt].flow=0;
nxt[ecnt]=first[b],first[b]=ecnt;
}
void bfs()
{
memset(vis,false,sizeof(vis));
int head=0,tail=1;
q[0]=t,d[t]=0,vis[t]=true;
while(head^tail){
int now=q[head++];
for(int i=first[now];i;i=nxt[i])
if(!vis[e[i].u]&&e[i].cap>e[i].flow){
vis[e[i].u]=true;
d[e[i].u]=d[now]+1;
q[tail++]=e[i].u;
}
}
}
int Agument()
{
int x=t,a=INF;
while(x^s){
a=min(a,e[p[x]].cap-e[p[x]].flow);
x=e[p[x]].u;
}
x=t;
while(x^s){
e[p[x]].flow+=a;
e[p[x]^1].flow-=a;
x=e[p[x]].u;
}
return a;
}
int ISAP()
{
int flow=0;
bfs();
memset(num,0,sizeof(num));
for(int i=0;i<=N*2+F+D+1;i++)num[d[i]]++;
int x=s;
for(int i=0;i<=N*2+F+D+1;i++)cur[i]=first[i];
while(d[s]<N*2+F+D+2){
if(!(x^t)){
flow+=Agument();
x=s;
}
bool advanced=false;
for(int i=cur[x];i;i=nxt[i])
if(e[i].cap>e[i].flow&&d[x]==d[e[i].v]+1){
advanced=true;
p[e[i].v]=i;
cur[x]=i;
x=e[i].v;
break;
}
if(!advanced){
int mn=N*2+F+D;
for(int i=first[x];i;i=nxt[i])
if(e[i].cap>e[i].flow)
mn=min(mn,d[e[i].v]);
if(--num[d[x]]==0)break;
num[d[x]=mn+1]++;
cur[x]=first[x];
if(x^s)x=e[p[x]].u;
}
}
return flow;
}
int main()
{
while(~scanf("%d%d%d",&N,&F,&D)){
s=0,t=N*2+F+D+1,ecnt=1;
memset(first,0,sizeof(first));
memset(nxt,0,sizeof(nxt));
memset(p,0,sizeof(p));
for(int i=1;i<=F;i++)Link(s,i,1);
for(int a,b,i=1;i<=N;i++){
scanf("%d%d",&a,&b);
for(int x;a;a--){
scanf("%d",&x);
Link(x,i+F,1);
}
Link(i+F,i+N+F,1);
for(int x;b;b--){
scanf("%d",&x);
Link(i+N+F,x+F+N*2,1);
}
}
for(int i=1+F+N*2;i<=D+F+N*2;i++)Link(i,t,1);
printf("%d\n",ISAP());
}
return 0;
}
/*反思:牛点要拆,不然会出现一头牛吃多份餐的情况;*/
15722550
|
poj3281 Dining的更多相关文章
- POJ3281 Dining —— 最大流 + 拆点
题目链接:https://vjudge.net/problem/POJ-3281 Dining Time Limit: 2000MS Memory Limit: 65536K Total Subm ...
- 2018.06.27 POJ3281 Dining(最大流)
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21578 Accepted: 9545 Description C ...
- POJ3281 Dining 2017-02-11 23:02 44人阅读 评论(0) 收藏
Dining Description Cows are such finicky eaters. Each cow has a preference for certain foods and dri ...
- <每日一题>Day 9:POJ-3281.Dining(拆点 + 多源多汇+ 网络流 )
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24945 Accepted: 10985 Descript ...
- POJ3281 Dining(拆点构图 + 最大流)
题目链接 题意:有F种食物,D种饮料N头奶牛,只能吃某种食物和饮料(而且只能吃特定的一份) 一种食物被一头牛吃了之后,其余牛就不能吃了第一行有N,F,D三个整数接着2-N+1行代表第i头牛,前面两个整 ...
- POJ3281 Dining 最大流
题意:有f种菜,d种饮品,每个牛有喜欢的一些菜和饮品,每种菜只能被选一次,饮品一样,问最多能使多少头牛享受自己喜欢的饮品和菜 分析:建边的时候,把牛拆成两个点,出和入 1,源点向每种菜流量为1 2,每 ...
- POJ3281:Dining(dinic+拆点)
题目链接:http://poj.org/problem?id=3281 PS:刷够网络流了,先这样吧,之后再刷,慢慢补. 题意:有F种食物,D种饮料,N头奶牛,只能吃某种食物和饮料(而且只能吃特定的一 ...
- [poj3281]Dining(最大流+拆点)
题目大意:有$n$头牛,$f$种食物和$d$种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢 ...
- [Poj3281]Dining(最大流)
Description 有n头牛,f种食物,d种饮料,每头牛有nf种喜欢的食物,nd种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种食物了,饮料也同理,问最多有多少头牛可以吃到它喜欢的 ...
随机推荐
- iOS之属性修饰符 retain、strong和copy区别测试
时不时会有点迷惑属性修饰符retain.strong.copy三者之间的区别,还是把测试过程记录下来好一点! 1.属性修饰符结论 2.给retain.strong.copy修饰的字符串属性赋值指针变化 ...
- 改变Android ProgressBar样式颜色
地址: http://blog.csdn.net/lvxiangan/article/details/9110121
- 搭建Maven私服
最近从SVN下载的代码,在本地构建时出现了诸多问题,部分依赖下载超时,就想起在局域网搭建Maven私服,废话不说了,在测试服务器上建的已经成功,就随便找台机子再练习一遍顺道写个日志.1.前往http: ...
- Eclipse中Maven+Spring3.2.17+SpringMVC HelloWorld
遇到的问题 The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path ...
- HttpClient 教程 (A)
前言 超文本传输协议(HTTP)也许是当今互联网上使用的最重要的协议了.Web服务,有网络功能的设备和网络计算的发展,都持续扩展了HTTP协议的角色,超越了用户使用的Web浏览器范畴,同时,也增加了需 ...
- MicroStation VBA基础
实习笔记1 2016年8月1日 14:12 Option Explicit 缺省情况下,如果使用一个没有声明的变量,它将继承“Variant”类型.在模块.窗体和类的通用声明区使用“OptionExp ...
- HTML页面禁止选择、页面禁止复制、页面禁止右键
HTML页面内容禁止选择.复制.右键刚在一个看一个站点的源代码的的时候发现的,其实原来真的很简单 <body leftmargin=0 topmargin=0 oncontextmenu='re ...
- Asp.net MVC使用Filter解除Session, Cookie等依赖
本文,介绍了Filter在MVC请求的生命周期中的作用和角色,以及Filter的一些常用应用场景. 同时针对MVC中的对于Session,Cookie等的依赖,如何使用Filter解依赖. 如果大家有 ...
- mysql set names 命令和 mysql 字符编码问题
先看下面的执行结果: (root@localhost)[(none)]mysql>show variables like 'character%'; +--------------------- ...
- SHA-1 加密算法破解现已只需要 10 天
转自:http://www.linuxeden.com/html/news/20151009/163173.html SHA-1是如今很常见的一种加密哈希算法,HTTPS传输和软件签名认证都很喜欢它, ...