POJ 3281 Dining(网络流)
来源:互联网 发布:国内编程语言排行榜 编辑:程序博客网 时间:2024/04/28 07:03
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 cowi likes. The next Fi integers denote the dishes that cowi will eat, and theDi 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.
人生第一道网络流!!!思路大致是建一个流网络将问题转化为裸的最大流。先设一个超级源点和一个超级汇点,源点向每种食物建一条cap为1的弧,每种饮料向汇点建一条cap为1的弧(饮料和食物可互换)。接下来讨论每头牛,由于一头牛只能与一种饮料和一种食物匹配,故此处应考虑拆点,将每个牛拆分为左牛和右牛,每头牛的左向右建一条cap为1弧即可满足上述要,再将每种食物和饮料与喜欢的牛建弧,cap同为1,跑一次最大流即可。
调了一晚上,为了练习算法遂用ISAP和Dinic各写了一遍。一开始用ISAP写的,由于总点数的计算有误导致不能回流。。。其实maxflow里面 “d[s] < N” 的 “N” 是你建的流网络的总点数也是增广路长度的上限,一开始脑子短路了莫名其妙给了个 6。。。,总分算是调出来了。Dinic就顺利多了,1A。上代码:
//Dinic
#include<iostream>#include<algorithm>#include<cstring>#include<queue>#include<vector>using namespace std;typedef struct node{ int from,to,cap,flow; node(int f = 0,int t = 0,int c = 0,int ff = 0):from(f),to(t),cap(c),flow(ff){}}node;const int INF = 1<<30;const int maxn = 400 + 5;int n,f,d,s,t,N;vector<node> edge;vector<int> G[maxn];int cur[maxn],di[maxn];bool vis[maxn];void addedge(int from,int to,int cap,int flow){ edge.push_back(node(from,to,cap,0)); edge.push_back(node(to,from,0,0)); int m = edge.size(); G[from].push_back(m-2); G[to].push_back(m-1);}bool BFS(){ int x; memset(vis,false,sizeof(vis)); queue<int> q; q.push(s); vis[s] = true; di[s] = 0; while(!q.empty()) { x = q.front(); q.pop(); for(int i = 0;i < G[x].size(); ++i) { node &e = edge[G[x][i]]; if(e.cap > e.flow && !vis[e.to]) { di[e.to] = di[x] + 1; vis[e.to] = true; q.push(e.to); } } } return vis[t];}int DFS(int x,int a){ int flow = 0,f; if(x == t || a == 0) return a; for(int &i = cur[x];i< G[x].size(); ++i) { node &e = edge[G[x][i]]; if(di[e.to] == di[x] + 1 && (f = DFS(e.to,min(a,e.cap - e.flow))) > 0) { e.flow += f; edge[G[x][i]^1].flow -= f; flow += f; a -= f; if(a == 0) break; } } return flow;}int maxflow(){ int flow = 0; while(BFS()) { memset(cur,0,sizeof(cur)); flow += DFS(s,INF); } return flow;}int main(){ int x,y,p; cin >> n >> f >> d; s = 0,t = 400 + 1; for(int i = 1;i <= f; ++i) addedge(s,i,1,0); //food for(int i = 1;i <= d; ++i) addedge(i + 300,t,1,0);//drink for(int i = 1;i <= n; ++i) addedge(i + 100,i + 200,1,0);//cow for(int i = 1;i <= n; ++i) { cin >> x >> y; for(int j = 0;j < x; ++j) { cin >> p; addedge(p,i + 100,1,0); }//food for(int j = 0;j < y; ++j) { cin>>p; addedge(i + 200,p + 300,1,0); }//drink } cout<<maxflow()<<endl; return 0;}//ISAP
#include<iostream>#include<algorithm>#include<cstring>#include<queue>#include<vector>using namespace std;typedef struct node{ int from,to,cap,flow; node(int f = 0,int t = 0,int c = 0,int ff = 0):from(f),to(t),cap(c),flow(ff){}}node;const int INF = 1<<30;const int maxn = 400 + 5;int n,f,d,s,t,N;vector<node> edge;vector<int> G[maxn];int p[maxn],num[maxn],cur[maxn],a[maxn],di[maxn];bool vis[maxn];void addedge(int from,int to,int cap,int flow){ edge.push_back(node(from,to,cap,0)); edge.push_back(node(to,from,0,0)); int m = edge.size(); G[from].push_back(m-2); G[to].push_back(m-1);}void BFS(){ queue<int> q; memset(di,0,sizeof(di)); memset(vis,false,sizeof(vis)); q.push(t); di[t] = 0; vis[t] = true; while(!q.empty()) { int x = q.front(); q.pop(); for(int i = 0;i < G[x].size(); ++i) { node &e = edge[G[x][i]]; if(!vis[e.to] && e.cap == 0) { vis[e.to] = true; di[e.to] = di[x] + 1; q.push(e.to); } } }}int add(){ int x = t,a = INF; while(x != s) { node &e = edge[p[x]]; a = min(a,e.cap-e.flow); x = edge[p[x]].from; } x = t; while(x != s) { edge[p[x]].flow +=a; edge[p[x]^1].flow -= a; x = edge[p[x]].from; } return a;}int maxflow(){ int flow = 0; BFS(); memset(cur,0,sizeof(cur)); memset(num,0,sizeof(num)); for(int i = 0;i <= maxn - 5; ++i) num[di[i]]++; num[0] = INF,N = 2 * n + 2 + f + d; int x = s; while(di[s] < N) { if(x == t) { flow += add(); x = s; } bool ok = false; for(int i = cur[x];i < G[x].size(); ++i) { node &e = edge[G[x][i]]; if(e.cap > e.flow && di[e.to] + 1== di[x]) { ok = true; p[e.to] = G[x][i]; cur[x] = i; x = e.to; break; } } if(!ok) { int m = N; for(int i = 0;i < G[x].size(); ++i) { node &e = edge[G[x][i]]; if(e.cap > e.flow) m = min(m,di[e.to]); } if(--num[di[x]] == 0) break; num[di[x] = m + 1]++; cur[x] = 0; if(x != s) x = edge[p[x]].from; } } return flow;}int main(){ int x,y,p; cin >> n >> f >> d; s = 0,t = 400 + 1; for(int i = 1;i <= f; ++i) addedge(s,i,1,0); //food for(int i = 1;i <= d; ++i) addedge(i + 300,t,1,0);//drink for(int i = 1;i <= n; ++i) addedge(i + 100,i + 200,1,0);//cow for(int i = 1;i <= n; ++i) { cin >> x >> y; for(int j = 0;j < x; ++j) { cin >> p; addedge(p,i + 100,1,0); }//food for(int j = 0;j < y; ++j) { cin>>p; addedge(i + 200,p + 300,1,0); }//drink } cout<<maxflow()<<endl; return 0;}
- POJ 3281 Dining(网络流,拆点法)
- POJ 3281 Dining (网络流)
- POJ 3281 Dining(网络流)
- poj 3281 Dining (网络流)
- POJ 3281 Dining(网络流建模)
- POJ 3281 Dining 网络流
- POJ 3281 Dining(网络流)
- 【网络流】 POJ 3281 Dining
- poj 3281 Dining (网络流)
- POJ - 3281 Dining 网络流
- poj 3281 Dining(网络流)
- POJ 3281 Dining 网络流
- POJ 3281Dining(网络流之最大流)
- POJ 3281 - Dining(网络流‘最大流)
- poj 3281 Dining(网络流最大流)
- NYOJ 326 && POJ 3281 Dining (网络流)
- Poj 3281 Dining (拆点网络流)
- POJ 3281 Dining 拆点网络流
- poj_2299 归并排序找逆序数
- 算法训练 寻找数组中最大值
- JZOJ1489.2017.04.02【NOIP 普及组】模拟赛C组 T1区间
- hihocoder 1295 : 数论二·Eular质数筛法
- hdu5066——Harry And Physical Teacher(物理)
- POJ 3281 Dining(网络流)
- 贪心+优先队列(需优化) POJ2010
- HDU 4920 坑爹的矩阵乘法
- Linux内核之禁止中断和禁止内核抢占
- Web API与OAuth:既生access token,何生refresh token
- STL之String详解
- string 的常见用法
- 全排列实现
- hdu 3572 Task Schedule 最大流 Dinic算法