Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10159 | Accepted: 4676 |
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
Output
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
Source
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; int const INF = INT_MAX; int const MAX = 405; int c[MAX][MAX]; int f[MAX][MAX]; int a[MAX]; int pre[MAX]; int n, food, drink; int Edmonds_Karp(int s, int t) { int ans = 0; queue <int> q; while(true) { memset(a, 0, sizeof(a)); a[s] = INF; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); for(int v = s; v <= t; v++) { if(!a[v] && c[u][v] > f[u][v]) { a[v] = min(a[u], c[u][v] - f[u][v]); pre[v] = u; q.push(v); } } } if(a[t] == 0) break; for(int u = t; u != s; u = pre[u]) { f[pre[u]][u] += a[t]; f[u][pre[u]] -= a[t]; } ans += a[t]; } return ans; } int main() { memset(c, 0, sizeof(c)); memset(f, 0, sizeof(f)); scanf("%d %d %d", &n, &food, &drink); for(int i = 1; i <= food; i++) c[0][i] = 1; for(int i = 1; i <= n; i++) { int nf, nd, getf, getd; scanf("%d %d", &nf, &nd); while(nf --) { scanf("%d", &getf); c[getf][food + i] = 1; } c[food + i][food + n + i] = 1; while(nd --) { scanf("%d", &getd); c[food + n + i][food + 2 * n + getd] = 1; } } for(int i = 1; i <= drink; i++) c[food + 2 * n + i][food + 2 * n + drink + 1] = 1; printf("%d\n", Edmonds_Karp(0, food + 2 * n + drink + 1)); }
POJ 3281 Dining (网络流最大流 拆点建图 Edmonds-Karp算法)
原文地址:http://blog.csdn.net/tc_to_top/article/details/44127239