【-100】P2891 [USACO07OPEN]吃饭Dining[最大流]

题目描述

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).

有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料。现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮料。(1 <= f <= 100, 1 <= d <= 100, 1 <= n <= 100)

输入输出格式

输入格式:

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.

输出格式:

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

输入输出样例

输入样例#1:

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

输出样例#1:

3

说明

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.

题解

把所有的点拆成边,w为1
然后把奶牛放中间,食物丢最左边,饮料丢最右边,搞个超级源超级汇,具体见代码
还是算很经典的模板的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
int n,m,s,t,d;
struct node
{
int v,id,u;
int w;
} a[20500];//链式前向星
int p[606];
int p1[606];
int h[606];
int U,V,W;
int N,F,D,A,B,X;
long long ans;
queue<int> q;
inline int read()
{
char ch=getchar();
int x=0;
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x;
}
inline void add(int u,int v,int w)
{
d++;
a[d].u=u;a[d].v=v;a[d].w=w;a[d].id=p[u];p[u]=d;
}
inline int bfs()
{
memset(h,-1,sizeof(h));
while (!q.empty()) q.pop();
q.push(s);
h[s]=0;
while (!q.empty())
{
int u=q.front();q.pop();
for (int i=p[u];i;i=a[i].id)
{
int v=a[i].v;
if (h[v]==-1&&a[i].w>0)
{
h[v]=h[u]+1;
q.push(v);
}
}
}
return h[t]!=-1;
}
inline int dfs(int u,int low)
{
if (u==t||low==0) return low;
int used=0;
for (int i=p1[u];i;i=a[i].id)
{
int v=a[i].v;
if (h[v]==h[u]+1)//只搜下一层的边
{
int w=dfs(v,min(low-used,a[i].w));
a[i].w-=w;
a[i^1].w+=w;
used+=w;
p1[u]=i;
if (used==low) return used;
}
}
if (used==0) h[u]=-1;//u到终点走不了了下次就不要走了
return used;
}
int main()
{
d=1;
N=read();F=read();D=read();//编号:1~F,F+1~F+N,F+N+1~F+N+D,
//F+N+D+1~F+N+D+F, 2F+N+D+1~2F+2F+2N+D,2F+2N+D+1~2F+2N+2D
//点拆成边
int l=F+N+D;
for (int i=1;i<=F;i++)
{
add(i,l+i,1);
add(l+i,i,0);
}
for (int i=1;i<=N;i++)
{
add(F+i,l+F+i,1);
add(l+F+i,F+i,0);
}
for (int i=1;i<=D;i++)
{
add(F+N+i,l+F+N+i,1);
add(l+F+N+i,F+N+i,0);
}

for (int i=1;i<=N;i++)
{
A=read();B=read();
while (A--)
{
X=read();
add(l+X,i+F,1);add(i+F,l+X,0);
}
while (B--)
{
X=read();
add(l+F+i,F+N+X,1);add(F+N+X,l+F+i,0);
}
}
n=2*l+2;
s=2*l+1;
t=2*l+2;
for (int i=1;i<=F;i++)
{
add(s,i,1);
add(i,s,0);
}
for (int i=1;i<=D;i++)
{
add(l+F+N+i,t,1);
add(t,l+F+N+i,0);
}

// for (int i=1;i<=d;i++)
// printf("%d %d %d %d\n",a[i].u,a[i].v,a[i].w,a[i].id);
while (bfs())
{
for (int i=1;i<=n;i++) p1[i]=p[i];
ans+=dfs(s,10);
}
printf("%d",ans);
return 0;
}