解题报告 之 POJ3281 Dining

时间:2022-01-13 04:32:17

解题报告 之 POJ3281 Dining


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: NF, 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 cowi 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.


题目大意:有n头牛,f种草料,d种水,每种草料和每种水仅有一个单位,每头牛要吃一个单位草料和喝一个单位水。每头牛只对一部分草料和一部分水感兴趣。问你最多能完全满足多少头牛(草料和水均满意)?


分析:经典构图题:用拆点来限制流量,超级源点与每种食物相连,负载为1。每头牛拆点,负载为1(此处限制流量,一头牛只能转移一的流量)。每头牛拆点的出点与所有满意的食物相连,负载为1;每头牛拆点的入点再与所有满意的水相连,负载为1,每种水再与超级汇点相连,负载为1。最终看最大流跑出多少就是最大的,能够双双满足的牛的数量。


图解:

解题报告 之 POJ3281 Dining

解题报告 之 POJ3281 Dining


(另外有一种变体是食物和饮料数量不是1,则相应改变超级源点和超级汇点连接的边为对应数量即可。)


上代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;

const int MAXM = 251000;
const int MAXN = 510;
const int INF = 0x3f3f3f3f;

struct Edge
{
int to, cap, next;
};

Edge edge[MAXM];
int level[MAXN];
int head[MAXN];
int src, des, cnt;

void addedge( int from, int to, int cap )
{
edge[cnt].to = to;
edge[cnt].cap = cap;
edge[cnt].next = head[from];
head[from] = cnt++;

swap( from, to );

edge[cnt].to = to;
edge[cnt].cap = 0;
edge[cnt].next = head[from];
head[from] = cnt++;
}

int bfs()
{
memset( level, -1, sizeof level );
queue<int>q;
while (!q.empty())
q.pop();

level[src] = 0;
q.push( src );

while (!q.empty())
{
int u = q.front();
q.pop();

for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (edge[i].cap > 0 && level[v] == -1)
{
level[v] = level[u] + 1;
q.push( v );
}
}
}
return level[des] != -1;
}

int dfs( int u, int f )
{
if (u == des) return f;
int tem;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (edge[i].cap>0&&level[v] == level[u] + 1)
{
tem = dfs( v, min( f, edge[i].cap ) );
if (tem > 0)
{
edge[i].cap -= tem;
edge[i^1].cap += tem;
return tem;
}
}
}
level[u] = -1;
return 0;
}


int Dinic()
{
int ans = 0, tem;

while (bfs())
{
while ((tem = dfs( src, INF )) > 0)
{
ans += tem;
}
}
return ans;
}



int main()
{
int n, f, d;
src = 0;
des = 505;
while (cin >> n>>f>>d)
{
memset( head, -1, sizeof head );
cnt = 0;
for (int i = 1; i <= f; i++)
addedge( src, i + 200, 1 );
for (int i = 1; i <= d; i++)
addedge( i + 300, des, 1 );

for (int i = 1; i <= n; i++)
{
addedge( i, i + 100, 1 );
int F, D;
cin >> F>>D;
for (int j = 1; j <= F; j++)
{
int tem;
cin >> tem;
addedge( tem + 200, i, 1 );
}
for (int j = 1; j <= D; j++)
{
int tem;
cin >> tem;
addedge( i+100, tem + 300, 1 );
}
}
cout << Dinic() << endl;
}
return 0;

}

啦啦啦,通信原理课果然很无聊。。。他都不知道我们听不懂。。