poj3281 Dinig(最大流)
来源:互联网 发布:mac登陆windows 编辑:程序博客网 时间:2024/06/06 04:50
Dining
Time Limit: 2000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
[Submit] [Status] [Discuss]
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.
Source
USACO 2007 Open Gold
有n头牛,f种食物,d种饮料,每头牛有它喜欢的食物和饮料,每种食物,饮料都只能被一头牛吃,问最多多少牛能吃到喜欢的食物和饮料。
源点连食物,食物连牛,牛连牛,牛连饮料,饮料连汇点,权值都为1,超级源点汇点权值无限。然后跑一边网络流就行。牛连牛确保一头牛只能提供1的流量。
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>using namespace std;const int MAXN = 500;const int INF = 2147483647;struct edge{ int to,cap,rev; edge(int a=0,int b=0,int c=0) :to(a),cap(b),rev(c){}};vector<edge> G[MAXN];bool used[MAXN];int iter[MAXN];int level[MAXN];void Add(int f,int t,int c){ G[f].push_back(edge{t,c,G[t].size()} ); G[t].push_back(edge{f,0,G[f].size()-1} );}void init(){ for(int i = 0;i < MAXN;i++){ G[i].clear(); } return ;}int dfs1(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=dfs1(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 MAX1(int s,int t){ int f = 0; while(true){ memset(used,0,sizeof(used)); int tempf = dfs1(s,t,INF); if(tempf == 0) return f; f += tempf; }}void bfs(int s){ memset(level,-1,sizeof(level)); queue<int> que; que.push(s); level[s] = 0; 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] == -1){ level[e.to] = level[v]+1; que.push(e.to); } } }}int dfs2(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 = dfs2(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 MAX2(int s,int t){ int f = 0; while(true){ bfs(s); if(level[t] < 0) return f; memset(iter,0,sizeof(iter)); int tempf; while(tempf = dfs2(s,t,INF) > 0) f += tempf; }}int main(){ int nnum,snum,ynum; while(scanf("%d%d%d",&nnum,&snum,&ynum) != EOF){ init(); for(int i = 1;i <= snum;i++){ Add(0,i,1); } for(int i = snum+nnum+1;i <= snum+2*nnum;i++){ Add(i-nnum,i,1); } for(int i = 2*nnum+snum+1;i <= 2*nnum+snum+ynum;i++){ Add(i,2*nnum+snum+ynum+1,1); } for(int i = 1;i <= nnum;i++){ int c1,c2; scanf("%d%d",&c1,&c2); int a; for(int j = 0;j < c1;j++){ scanf("%d",&a); Add(a,snum+i,1); } for(int j = 0;j < c2;j++){ scanf("%d",&a); Add(snum+nnum+i,snum+2*nnum+a,1); } } int ans = MAX1(0,2*nnum+snum+ynum+1); printf("%d\n",ans); } return 0;}
- poj3281 Dinig(最大流)
- poj3281(最大流)
- POJ3281--Dining(最大流)
- poj3281 Dining(最大流)
- POJ3281:Dining(最大流)
- poj3281最大流(模板)
- poj3281(#网络流最大流)
- poj3281——Dining(最大流)
- POJ3281-Dining(最大流Dinic)
- poj3281—Dining(最大流)
- 【POJ3281 Dining 】最大流
- poj3281-最大流
- 【poj3281】【最大流】Dining
- poj3281 最大流
- POJ3281 Dining(最大流)
- 【poj3281】Dining 最大流
- POJ3281 Dining 最大流
- 【POJ3281】Dining【最大流】
- JAVA常用排序算法之简单选择排序
- Given a roman numeral, convert it to an integer.
- 查看端口占用情况
- 原生态JDBC程序问题总结
- swipe js插件使用
- poj3281 Dinig(最大流)
- 字符串数组
- 离散数学10
- Eclipse修改包访问规则
- [LeetCode] Flatten Binary Tree to Linked List 将二叉树展开成链表 C++
- JAVA-15-多线程的调度和控制、线程安全、死锁、等待和唤醒
- 解决Vim"UltiSnips requires py >= 2.7 or py3"
- Java内存模型
- 树莓派的安装