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种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种食物了,饮料也同理,问最多有多少头牛可以吃到它喜欢的 ...
随机推荐
- 【转】HttpClient使用Post和Get提交参数
package httpclient; import java.io.IOException; import java.net.URLEncoder; import org.apache.common ...
- 编写高质量的Objective-C代码
点标记语法 属性和幂等方法(多次调用和一次调用返回的结果相同)使用点标记语法访问,其他的情况使用方括号标记语法. 良好的风格: view.backgroundColor = [UIColor or ...
- Android studio修改Logcat颜色
Android studio默认的Logcat配色不利于阅读,我们可以修改自定义自己的颜色配置
- Log4jConfigListener cannot be found — context fails to start
解决方案 Reticulogic's second suggestion is correct. However, in Eclipse Helios, the "Java EE Modul ...
- redis数据类型
Redis 数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). String(字符串) st ...
- mac os下可能是最好的豆瓣电台——diumoo
由于我一直用豆瓣fm听音乐,在网上找了下豆瓣的相关应用,都感觉不是太好, 最后发现一个mac版的app--diumoo! 这个软件看着非常舒服,一点也不占桌面空间,它一直默默在桌面右上角,鼠标划上去会 ...
- wpf开发桌面软件记录
我的开发环境是win7,vs2013,sql2012,用wpf开发了一个很简单的桌面软件,用Installshield制作的安装包,安装包包含了.framework4.5,在自己电脑上测试正常,想着挺 ...
- Github入门(一)
之前早就听说过Git的大名,但由于合作项目时的团体都非常小,所以一直没有开始系统的学习和使用(其实就是懒!),最近终于有动力开始进行入门的学习. 首先介绍一下自学用书:https://git-scm. ...
- Extjs插入图片
Extjs并没有提供直接的组件来显示图片.网上通过设置textfield的InputType为Image然后再修改dom对象来实现,其实 image的属性值并没有包含在Extjs的官方文档当中.可以通 ...
- C#语言基础——数组
数组 一.一位数组 数组初始化,创建数组,数组长度为5 int[] array = new int[5]; array[0] = 1; array[1] = 2; array[2] = 3; arra ...