Dining(最大流)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 11844 | Accepted: 5444 |
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.

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
const int MAXM=<<;
int head[MAXM];
int vis[MAXN],dis[MAXN];
int edgnum;
struct Node{
int from,to,next,cup,flow;
};
Node edg[MAXM];
queue<int>dl;
void add(int u,int v,int w){
Node E={u,v,head[u],w,};
edg[edgnum]=E;
head[u]=edgnum++;
E={v,u,head[v],,};
edg[edgnum]=E;
head[v]=edgnum++;
}
void initial(){
mem(head,-);edgnum=;
}
bool bfs(int s,int e){
mem(vis,);mem(dis,-);
while(!dl.empty())dl.pop();
dis[s]=;vis[s]=;dl.push(s);
while(!dl.empty()){
int u=dl.front();
dl.pop();
for(int i=head[u];i!=-;i=edg[i].next){
Node v=edg[i];
if(!vis[v.to]&&v.cup>v.flow){
vis[v.to]=;dl.push(v.to);
dis[v.to]=dis[u]+;
if(v.to==e)return true;
}
}
}
return false;
}
int dfs(int x,int la,int e){
if(x==e||la==)return la;
int temp;
int flow=;
for(int i=head[x];i!=-;i=edg[i].next){
Node &v=edg[i];
if(dis[v.to]==dis[x]+&&(temp=dfs(v.to,min(la,v.cup-v.flow),e))>){
v.flow+=temp;
edg[i^].flow-=temp;
la-=temp;
flow+=temp;
if(la==)break;
}
}
return flow;
}
int maxflow(int s,int e){
int flow=;
while(bfs(s,e)){
flow+=dfs(s,INF,e);
}
return flow;
}
int main(){
int n,F,D,f,d,a,b;
while(~scanf("%d%d%d",&n,&F,&D)){
initial();
for(int i=;i<=n;i++){
scanf("%d%d",&f,&d);
while(f--){
scanf("%d",&a);
add(*n+a,i,);
}
while(d--){
scanf("%d",&a);
add(n+i,*n+F+a,);
}
add(i,n+i,);
}
for(int i=;i<=F;i++)add(,*n+i,);
for(int i=;i<=D;i++)add(*n+F+i,*n+F+D+,);
printf("%d\n",maxflow(,*n+F+D+));//醉了,应该从0开始,找了半天错。。。
}
return ;
}
另一种解法:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
const int MAXM=<<;
queue<int>dl;
int vis[MAXN],map[MAXN][MAXN],pre[MAXN];
bool bfs(int s,int e){
mem(vis,);mem(pre,-);
while(!dl.empty())dl.pop(); //忘了初始化。。。。。
vis[s]=;dl.push(s);
int a;
while(!dl.empty()){
a=dl.front();
dl.pop();//忘写pop了。。。
if(a==e)return true;
for(int i=;i<=e;i++){
if(!vis[i]&&map[a][i]){
dl.push(i);
vis[i]=;
pre[i]=a;
//if(i==e)return true;
}
}
}
return false;
}
int maxflow(int s,int e){
int flow=;
while(bfs(s,e)){
int temp=INF;
int r=e;
//puts("fasf");
while(r!=s)temp=min(temp,map[pre[r]][r]),r=pre[r];
r=e;
while(r!=s)map[pre[r]][r]-=temp,map[r][pre[r]]+=temp,r=pre[r];
flow+=temp;
}
return flow;
}
int main(){
int n,F,D,f,d,a,b;
while(~scanf("%d%d%d",&n,&F,&D)){
mem(map,);
for(int i=;i<=n;i++){
scanf("%d%d",&f,&d);
while(f--){
scanf("%d",&a);
// add(2*n+a,i,1);
map[*n+a][i]=;
}
while(d--){
scanf("%d",&a);
// add(n+i,2*n+F+a,1);
map[n+i][*n+F+a]=;
}
//add(i,n+i,1);
map[i][n+i]=;
}
for(int i=;i<=F;i++)map[][*n+i]=;//add(0,2*n+i,1);
for(int i=;i<=D;i++)map[*n+F+i][*n+F+D+]=;//add(2*n+F+i,2*n+F+D+1,1);
printf("%d\n",maxflow(,*n+F+D+));//醉了,应该从0开始,找了半天错。。。
}
return ;
}
Dining(最大流)的更多相关文章
- POJ3281 Dining —— 最大流 + 拆点
题目链接:https://vjudge.net/problem/POJ-3281 Dining Time Limit: 2000MS Memory Limit: 65536K Total Subm ...
- POJ 3281 Dining(最大流)
POJ 3281 Dining id=3281" target="_blank" style="">题目链接 题意:n个牛.每一个牛有一些喜欢的 ...
- POJ3281 Dining 最大流
题意:有f种菜,d种饮品,每个牛有喜欢的一些菜和饮品,每种菜只能被选一次,饮品一样,问最多能使多少头牛享受自己喜欢的饮品和菜 分析:建边的时候,把牛拆成两个点,出和入 1,源点向每种菜流量为1 2,每 ...
- 【网络流#7】POJ 3281 Dining 最大流 - 《挑战程序设计竞赛》例题
不使用二分图匹配,使用最大流即可,设源点S与汇点T,S->食物->牛->牛->饮料->T,每条边流量为1,因为流过牛的最大流量是1,所以将牛拆成两个点. 前向星,Dini ...
- P2891 [USACO07OPEN]吃饭Dining 最大流
\(\color{#0066ff}{ 题目描述 }\) 有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类 ...
- [poj3281]Dining(最大流+拆点)
题目大意:有$n$头牛,$f$种食物和$d$种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢 ...
- poj3281 Dining 最大流(奇妙的构图)
我是按照图论500题的文档来刷题的,看了这题怎么也不觉得这是最大流的题目.这应该是题目做得太少的缘故. 什么是最大流问题?最大流有什么特点? 最大流的特点我觉得有一下几点: 1.只有一个起点.一个终点 ...
- POJ 3281 Dining(最大流板子)
牛是很挑食的.每头牛都偏爱特定的食物和饮料,其他的就不吃了. 农夫约翰为他的牛做了美味的饭菜,但他忘了根据它们的喜好检查菜单.虽然他不可能喂饱所有的人,但他想让尽可能多的奶牛吃上一顿有食物和水的大餐. ...
- POJ 3281 Dining 最大流
饮料->牛->食物. 牛拆成两点. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include ...
- POJ 3281 Dining(最大流+拆点)
题目链接:http://poj.org/problem?id=3281 题目大意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 1 ...
随机推荐
- 在 win 10 中使用sql 2012 附加低版本数据失败的解决办法。
随着win 10 的发布,我也尝试把自己的笔记本升级下,体验win10,由于自己电脑好长时间没有管理过,东西比较乱,一激动就格式了硬盘.但是所有的资料都丢失了,不过我都提前备份到网盘上.好了,废话不多 ...
- 虎扯:小众玩物 webkit家的滚动条
前面的话:对只有一种浏览器支持的属性,就不要出来秀咱前端同学,就像是早些年手机们的充电口一样,集各家所长,咱今天说的是webkit的滚动条样式,视乎只有webkit支持此项定义,有见识的前辈来辩.这玩 ...
- mysql 时间差问题集锦
SELECT * from grouptoadd where taskid = '103244'; select datediff(max(spreadtime),min(createtime)) f ...
- Set 与 Multiset
Set 与 Multiset 会根据待定的排序准则,自动将元素排序,两者不同之处在于前者不允许元素重复,后者允许,下面介绍一下set中的函数: 一.set 中的 begin.end.rbegin.re ...
- Java 网络编程(Elliotte Rusty Harold)
最近看了本Java网络编程方面的书,系统化一下Java网络编程,主要内容如下: 1.网络基础篇 这里包括基础的网络概念/ 输入输出流BIO/ Internet地址/URI/URL/HTTP/URLCo ...
- Myeclipse利用maven构建sping web项目
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQQAAADvCAIAAACbnj2oAAAfq0lEQVR4nO2d+1MUV9rH+294U9mqpG ...
- asp.net mvc重写RequestValidator
/// <summary> /// <httpRuntime requestValidationType="xxx.CustomRequestValidator" ...
- PROTEL99生成GERBER的操作说明
GBL BOTTOM LAYER(底层布线图)GBO BOTTOM OVERLAYER(底层丝印层)GBP BOTTOM PASTE LAYER(底层锡膏层)GBS BOTTOM SOLDER MAS ...
- png的格式及像素存储分析
从图片的数据块存储方式来看png分两种 1.索引色模式存储.png8的索引色透明就采取该种方式.该种方式是将颜色存在png的可选模块调色板中,调色板的色彩存储格式为RGB(各1byte).而图片的数据 ...
- flex正则表达式
正则表达式是一种通用的标准,大部分计算机语言都支持正则表达式,包括as3,这里收集了一些常用的正则表达式语句,大家用到的时候就不用自己写了 ^\d+$ //匹配非负整数(正整数 + 0) ^[0-9] ...