您的位置:首页 > 其它

POJ 3281

2015-08-21 18:33 246 查看

Dining

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

Line 1: Three space-separated integers: N,
F, and D

Lines 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 wishes

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

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

题意: 有一些奶牛,每头奶牛都有自己喜欢的食物和饮料(一种或多种),为了让每头牛都得到满足,人们决定让每头牛只选一种食物和饮料,并且每种食物或饮料只能被一头牛选,求最多有多少头牛能得到满足o(* ̄︶ ̄*)o
思路: 我打死也不会想到网络流哒= =,然而就是一个最大流问题~ ~

奇妙的建图,建立一个超级源点s和超级汇点t,连边: s-食物—奶牛—饮料—t,首先想到的是这样,但是这有一个问题,奶牛可能会选择多种食物或饮料,为了避免这个,我们把一头奶牛拆成两个点,在两头相同的奶牛之间连一条边就可以啦~ ~ 然后最大流,套模板,注意连边时候各个节点的标号问题。

AC代码:
#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 最大流