博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3281 Dining (最大流,建图)
阅读量:7013 次
发布时间:2019-06-28

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

Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20928   Accepted: 9279

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.
 
分析:题意就是有N头牛,F个食物,D个饮料。N头牛每头牛,只喜欢几个食物和饮料。
每个食物和饮料只能给一头牛,一头牛只能得到一个食物和饮料,且一头牛必须同时获得一个食物和一个饮料,
问至多有多少头牛可以获得满足。
最大流建图是把食物和饮料放在两端。一头牛拆分成两个点(刚开始没有分成两个点),
分成两个点表示“一头牛必须同时获得一个食物和一个饮料”才能得到满足(流量可以流过),
两点之间的容量为1,喜欢的食物和饮料跟牛建条边,容量为1,
加个源点和汇点。源点与食物、饮料和汇点的边容量都是1,表示每种食物和饮料只有一个。
这样话完全是最大流问题了。
#include
#include
#include
#include
#define INF 999999999using namespace std;int N,F,D;//N只cows,F种food,D种drinks int map[500][500],dis[500];int T;int bfs(){ memset(dis,-1,sizeof(dis)); dis[0]=0; queue
q; q.push(0); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=0;i<=T;i++) if(dis[i]==-1&&map[u][i]>0) { dis[i]=dis[u]+1; q.push(i); } } if(dis[T]>0) return 1; return 0;}int dfs(int cur,int m){ if(cur==T) return m; int f,res=0; for(int i=0;i<=T;i++) { if(dis[i]==dis[cur]+1&&map[cur][i]>0&&(f=dfs(i,min(m,map[cur][i])))) { map[cur][i]-=f; map[i][cur]+=f; res+=f; m-=f; if(!m) break; } } if(res) return res; dis[cur]=-1; return 0;}int main(){ scanf("%d%d%d",&N,&F,&D); //0为源点,1到F为food结点,F+1到F+2N为cow结点 //F+2N+1到F+2N+D为drinks结点,T=F+2N+D+1为汇点 //一头牛拆成两个点,两个点的边容量为1 T=F+N+N+D+1; for(int i=1;i<=N;i++) { int f,d,x; scanf("%d%d",&f,&d); while(f--) { scanf("%d",&x); map[x][F+i]=1;//food->cow } while(d--) { scanf("%d",&x); map[F+N+i][F+N+N+x]=1;//cow->drink } } for(int i=1;i<=F;i++) map[0][i]=1;//源点->food for(int i=1;i<=D;i++) map[F+N+N+i][T]=1;//drink->汇点 for(int i=1;i<=N;i++) map[F+i][F+i+N]=1;//cow->cow int ans=0,res; while(bfs()) { while(res=dfs(0,INF)) ans+=res; } printf("%d\n",ans); return 0;}
View Code

 

 
 
 
 

转载于:https://www.cnblogs.com/ACRykl/p/8784264.html

你可能感兴趣的文章
Flask 第八篇 实例化Flask的参数 及 对app的配置
查看>>
JDK 环境变量的配置
查看>>
3DSMAX联机渲染、网络渲染、分布式渲染效率评测
查看>>
CDH版本hadoop2.6伪分布式安装
查看>>
Java练习题
查看>>
速度向量之单轴速度
查看>>
程序员的七大坏毛病
查看>>
MyBabis 用法详解
查看>>
leetcode-104
查看>>
C++ STL编程轻松入门【转载】
查看>>
Linux中的进程调度(五)
查看>>
.Net操作Excel —— NPOI
查看>>
黑马程序员--Java基础加强(高新技术)学习第三天
查看>>
RedHat Enterprise Linux 6.4使用Centos 6 的yum源
查看>>
ios如何实现静音模式下声音仍然可以外放
查看>>
alibaba笔试1
查看>>
JAVA 中BIO,NIO,AIO的理解以及 同步 异步 阻塞 非阻塞
查看>>
数据的索引
查看>>
Python之面向对象函数式编程
查看>>
终于又博客了
查看>>