poj3281 Dining-网络流最大流-多对一的匹配
来源:互联网 发布:上海市政分院待遇 知乎 编辑:程序博客网 时间:2024/06/06 14:07
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 preparedD (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 andDi, the number of dishes that cowi likes and the number of drinks that cow i likes. The next Fi integers denote the dishes that cowi will eat, and the Di integers following that denote the drinks that cowi will drink.
Output
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
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,f,d
第二行 第1头牛喜欢的食物的种类数,喜欢的饮料的种类数 然后分别是哪几种食物,哪几种饮料
第三行 第2头牛………………………………………………………………………………………………
…………………………………………………………………………………………………………………
解题思路
这是匹配问题, 如果只有食物就好做了,但是还有饮料,对于二分图的匹配问题还有一种做法是用最大流做,自己定义一个起点终点,边的流量都为1 ,做最大流,结果就是最大匹配数
这样我们还可以多定义一些牛,如图;
牛与食物和饮料的关系按照题意连线,
代码
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>using namespace std;const int maxn = 1000;const int INF = 0x3f3f3f3f;struct edge{ int to,cap,rev;};vector <edge> G[maxn];bool used[maxn];void add_edge(int u,int v,int cap){ G[u].push_back((edge){v,cap,G[v].size()}); G[v].push_back((edge){u,0,G[u].size()-1});}int dfs(int v,int t,int f){ if(v==t) return f; used[v]=true; for(int i=0;i<G[v].size();++i) { edge &e = G[v][i]; if(!used[e.to]&&e.cap>0) { int d = dfs(e.to,t,min(f,e.cap)); if(d>0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0;}int max_flow(int s,int t){ int flow=0; while(1) { memset(used,0,sizeof(used)); int f = dfs(s,t,INF); if(f==0) return flow; flow+=f; }}int n,f,d;//0~n是食物一侧的牛,n~2n-1是饮料一侧的牛,2n~2n+f-1是食物,2n+f~2n+f+d是饮料。bool likef[110][110];bool liked[110][110];int main(){ scanf("%d%d%d",&n,&f,&d); int s=n*2+f+d,t=s+1; for(int i=0;i<n;++i) { int f1,d1,lf,ld; scanf("%d%d",&f1,&d1); for(int j=0;j<f1;++j) { scanf("%d",&lf); likef[i][lf-1]=true; } for(int j=0;j<d1;++j) { scanf("%d",&ld); liked[i][ld-1]=true; } } for(int i=0;i<f;++i)//起点和食物连边 { add_edge(s,2*n+i,1); } for(int i=0;i<d;++i)//饮料和t连边 { add_edge(n*2+f+i,t,1); } for(int i=0;i<n;++i)//枚举牛 { add_edge(i,i+n,1);//牛和牛之间连边 for(int j=0;j<f;++j)//喜欢的食物和牛连边 { if(likef[i][j]) add_edge(n*2+j,i,1);//注意食物在前 } for(int j=0;j<d;++j)//牛和喜欢的饮料连边 { if(liked[i][j]) add_edge(n+i,n*2+f+j,1); } } printf("%d\n",max_flow(s,t)); return 0;}
- poj3281 Dining-网络流最大流-多对一的匹配
- 【POJ3281 Dining 】最大流
- 【poj3281】【最大流】Dining
- POJ3281 Dining(最大流)
- 【poj3281】Dining 最大流
- POJ3281 Dining 最大流
- 【POJ3281】Dining【最大流】
- poj3281 Dining 最大流
- poj3281 Dining 最大流
- Dining POJ3281 最大流
- poj3281 dining 网络流最大流算法
- POJ3281 网络流 dining
- 【POJ3281】Dining 网络流
- [网络流]poj3281 Dining
- POJ3281 Dining【网络流】
- POJ3281 Dining [最大流应用]
- POJ3281--Dining(最大流)
- poj3281-Dining ,最大流,建图
- 清除缓存文件
- Linux下Jdk和Tomcat自动安装shell脚本总结
- openssl RSA加密方法初识
- JavaScript中的this
- java中无符号类型的处理
- poj3281 Dining-网络流最大流-多对一的匹配
- eclipse neon 版本打开 could not create the java virtual machine问题
- arm64导致图片显示不出原因
- Linux学习碎片(2)
- Server Tomcat v9.0 Server at localhost failed to start.解决方案
- iOS cocoapods 头文件
- postgres数据库备份恢复
- MySQL 自动故障转移工具--mysqlfailover
- spak on hive 资源占用