标签:acm edmondkarp poj 最大流
Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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 <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; #define maxn 100+5 #define inf 0x7fffffff int n,f,d; int s,t; int c[500][500],pre[500]; int EK() { int maxflow = 0, minflow; while(1) { minflow = inf; for(int i = 0;i <= t;i ++) pre[i] = -1; queue<int> q; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); if(u == t) break; for(int i = 0;i <= t;i ++) { if(pre[i] == -1 && c[u][i] > 0) { q.push(i); pre[i] = u; } } } if(pre[t] == -1) break; for(int i = t;i != s;i = pre[i]) minflow = min(minflow, c[pre[i]][i]); //cout << minflow<<endl; for(int i = t;i != s;i = pre[i]) { c[pre[i]][i] -= minflow; c[i][pre[i]] += minflow; } maxflow += minflow; } return maxflow; } int main() { int fi,di; while(scanf("%d%d%d", &n,&f,&d) != EOF) { s = 0; t = n*2+f+d+1; memset(c, 0, sizeof(c)); for(int i = 1;i <= n;i ++) { scanf("%d%d", &fi,&di); while(fi --) { int v; scanf("%d", &v); v = 2*n + v; c[s][v] = 1; c[v][i] = 1; } while(di --) { int v; scanf("%d", &v); v = 2*n+f+v; c[n+i][v] = 1; c[v][t] = 1; } } for(int i = 1;i <= n;i ++) c[i][n+i] = 1; /* for(int i = s;i <= t;i ++) { for(int j = s;j <= t;j ++) { cout <<i<<" "<<j<<" "<<c[i][j]<<endl; } } */ int ans = EK(); printf("%d\n", ans); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:acm edmondkarp poj 最大流
原文地址:http://blog.csdn.net/mowenwen_/article/details/47838349