poj 3281 Dining(网络流+拆点)
Dining
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 20052 | Accepted: 8915 |
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 DLines 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 wishesSample 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分析
拆点+网络流最大流
code
#include<cstdio> #include<algorithm> using namespace std; const int INF = 1e9; const int N = 10010; struct Edge{ int to,nxt,c; }e ; int head ,dis ,cur ,q[500100]; int tot = 1,L,R,S,T; inline char nc() { static char buf[100000],*p1 = buf,*p2 = buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; } inline int read() { int x = 0,f = 1;char ch = nc(); for (; ch<'0'||ch>'9'; ch = nc()) if (ch=='-') f = -1; for (; ch>='0'&&ch<='9'; ch = nc()) x = x * 10 + ch - '0'; return x * f; } inline void add_edge(int u,int v,int w) { e[++tot].to = v,e[tot].c = w,e[tot].nxt = head[u],head[u] = tot; e[++tot].to = u,e[tot].c = 0,e[tot].nxt = head[v],head[v] = tot; } bool bfs() { for (int i=0; i<=T; ++i) { cur[i] = head[i];dis[i] = -1; } L = 1;R = 0; q[++R] = S;dis[S] = 0; while (L <= R) { int u = q[L++]; for (int i=head[u]; i; i=e[i].nxt) { int v = e[i].to,c = e[i].c; if (dis[v]==-1 && c>0) { dis[v] = dis[u] + 1; q[++R] = v; if (v==T) return true; } } } return false; } int dfs(int u,int flow) { if (u==T) return flow; int used = 0; for (int &i=cur[u]; i; i=e[i].nxt) { int v = e[i].to,c = e[i].c; if (dis[v]==dis[u]+1 && c>0) { int tmp = dfs(v,min(c,flow-used)); if (tmp > 0) { e[i].c -= tmp;e[i^1].c += tmp; used += tmp; if (used==flow) break; } } } if (used!=flow) dis[u] = -1; return used; } inline int dinic() { int ans = 0; while (bfs()) ans += dfs(S,INF); return ans; } int main() { int n = read(),F = read(),D = read(); S = 0,T = n+n+F+D+1; for (int i=1; i<=n; ++i) { int f = read(),d = read(); for (int a,j=1; j<=f; ++j) a = read(),add_edge(a+n+n,i,1); for (int a,j=1; j<=d; ++j) a = read(),add_edge(i+n,a+n+n+F,1); } for (int i=1; i<=n; ++i) add_edge(i,i+n,1); for (int i=1; i<=F; ++i) add_edge(S,i+n+n,1); for (int i=1; i<=D; ++i) add_edge(i+n+n+F,T,1); printf("%d",dinic()); return 0; }
- Poj 3281 Dining (拆点网络流)
- POJ 3281(Dining-网络流拆点)[Template:网络流dinic]
- POJ 3281 Dining (网络流构图)
- POJ 3281 Dining 网络流
- POJ 3281 Dining(网络流,拆点法)
- POJ 3281 Dining (网络流构图)
- poj 3281 Dining (网络流)
- poj 3281 Dining 网络流-最大流-建图的题
- POJ 3281 Dining (网络流最大流 拆点建图 Edmonds-Karp算法)
- POJ 3281 Dining( 网络流, Dinic 和ISAP )
- POJ 3281 Dining (网络流)
- poj 3281 Dining 【图论-网络流-最大流-EK&Ford-Fulkerson】
- POJ 3281 Dining 【网络流建模汇总】最大流
- poj 3281 Dining 网络流-最大流-建图的题
- POJ 3281 Dining(网络流最大匹配)
- poj 3281 Dining(网络流最大流)
- POJ 3281 网络流 拆点 Dining
- poj 3281 Dining 【拆点网络流】【最大流经典问题 关键建图】
- POJ 3281-Dining(网络流_最大流_ISAP算法+拆点)
- POJ 3281 Dining[网络流]