Dining POJ3281 最大流
2016-09-24 01:53
183 查看
来自《挑战程序设计竞赛》
①图的顶点在食物对应的匹配中的食物和牛,饮料对应的匹配中的饮料和牛,还有一个源点s,一个汇点t
②在两个匹配相同的牛之间连一条边,在s和所有食物之间连一条边,在所有饮料和t之间连一条边。
③边的方向为s→ 食物→ 牛→ 牛→ 饮料→ t。
这个图中的每一条路径都对应一个牛的食物和饮料的匹配方案。问题转化为最大流问题。
1.题目原文
http://poj.org/problem?id=3281Language: Default Dining
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 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 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. Source USACO 2007 Open Gold |
2.解题思路
如果只是分配食物的话,那么直接用二分图最大匹配就能够解决了。但是遇到这种需要同时给一头牛分配所喜欢的食物和饮料,就不能很好地处理。我们可以将食物和饮料所对应的两个匹配通过下面的方法联合起来求解。①图的顶点在食物对应的匹配中的食物和牛,饮料对应的匹配中的饮料和牛,还有一个源点s,一个汇点t
②在两个匹配相同的牛之间连一条边,在s和所有食物之间连一条边,在所有饮料和t之间连一条边。
③边的方向为s→ 食物→ 牛→ 牛→ 饮料→ t。
这个图中的每一条路径都对应一个牛的食物和饮料的匹配方案。问题转化为最大流问题。
3.AC代码
#include<iostream> #include<cstdio> #include<vector> #include<cstring> #include<algorithm> #include<utility> #include<queue> using namespace std; #define INF 0x7fffffff #define MAX_N 105 #define MAX_F 105 #define MAX_D 105 //最大流算法Dinic模板 #define MAX_V 405 //用于表示边的结构体(终点、容量、反向边) struct edge { int to,cap,rev; }; vector<edge> G[MAX_V];//图的邻接表表示 int level[MAX_V];//顶点到源点的距离标号 int iter[MAX_V];//当前弧,在其之前的边已经没有用了 //向图中增加一条从from到to的容量为cap的边 void add_edge(int from,int to,int cap) { G[from].push_back((edge){to,cap,G[to].size()}); G[to].push_back((edge){from,0,G[from].size()-1}); } //通过BFS计算从源点出发的距离标号 void bfs(int s) { memset(level,-1,sizeof(level)); queue<int> que; level[s]=0; que.push(s); while(!que.empty()){ int v=que.front(); que.pop(); for(int i=0;i<G[v].size();i++){ edge &e=G[v][i]; if(e.cap>0&&level[e.to]<0){ level[e.to]=level[v]+1; que.push(e.to); } } } } //通过DFS寻找增广路 int dfs(int v,int t,int f) { if(v==t) return f; for(int &i=iter[v];i<G[v].size();i++){ edge &e=G[v][i]; if(e.cap>0&&level[v]<level[e.to]){ 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; } //求解从s到t的最大流 int max_flow(int s,int t) { int flow=0; for(;;){ bfs(s); if(level[t]<0) return flow; memset(iter,0,sizeof(iter)); int f; while((f=dfs(s,t,INF))>0){ flow+=f; } } } //模板 int N,F,D; bool likeF[MAX_N][MAX_F];//食物的喜好 bool likeD[MAX_N][MAX_D];//饮料的喜好 void solve() { //0-N-1:食物一侧的牛 //N-2N-1:饮料一侧的牛 //2N-2N+F-1:食物 //2N+F-2N+F+D-1:饮料 int s=2*N+F+D,t=s+1; //在s和食物之间连边 for(int i=0;i<F;i++){ add_edge(s,2*N+i,1); } //在饮料和t之间连边 for(int i=0;i<D;i++){ add_edge(2*N+F+i,t,1); } for(int i=0;i<N;i++){ //在食物一侧的牛和饮料一侧的牛之间连边 add_edge(i,N+i,1); //在牛和所喜欢的食物或者饮料之间连边 for(int j=0;j<F;j++){ if(likeF[i][j]){ add_edge(2*N+j,i,1); } 4000 } for(int j=0;j<D;j++){ if(likeD[i][j]){ add_edge(N+i,2*N+F+j,1); } } } printf("%d\n",max_flow(s,t)); } int main() { memset(likeF,0,sizeof(likeF)); memset(likeD,0,sizeof(likeD)); scanf("%d%d%d",&N,&F,&D); for(int i=0;i<N;i++){ int n,m,f,d; scanf("%d%d",&n,&m); for(int j=0;j<n;j++){ scanf("%d",&f); likeF[i][f-1]=true; } for(int j=0;j<m;j++){ scanf("%d",&d); likeD[i][d-1]=true; } } solve(); return 0; }
相关文章推荐
- SQL查询不包含没文字,和取某字段相同的最大值的那条记录
- poj2239 Selecting Courses(最大二分图匹配 (匈牙利算法) 实现 )
- zoj2314 经典 无源汇有上下界最大流 并输出可行流
- 一个整数数组,长度为n,将其分为m份,使各份的和相等,求m的最大值
- 二分图最佳完美匹配——Kuhn-Munkres算法 (最大权匹配)
- 返回二维数组最大子数组的和(2)
- Codeforces Gym 100825F Transportation Delegation (最大流)
- c++实验7-最大公约数和最小公倍数
- mysql常用sql总结_获取多个字段最大值最小值
- 网易面试题之小易是一个数论爱好者,并且对于一个数的奇数约数十分感兴趣。一天小易遇到这样一个问题: 定义函数f(x)为x最大的奇数约数,x为正整数。 例如:f(44) = 11. 现在给出一个N,需要求
- 滑动窗口最大值
- hdu 5970 最大公约数
- Collector'sProblem(考虑建边的问题!最大流)
- 远程桌面连接--显示终端服务器超出了最大允许连接数
- HDU 3879 Base Station【最大权闭包】
- POJ 3281 - Dining 简单构图最大流..
- 【剑指offer】打印1到最大的n位数
- 为什么32位系统支持的最大内存为4G?
- sphinx SetFilter 单个过滤器最大过滤值数目
- 最小费用-最大流---hdu1533