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 2Sample 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
一个模板题……Orz 神犇a
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int SZ = 20010;
const int INF = 214748364;
struct Edge
{
int f, t, cap;
}es[SZ];
int first[SZ], nxt[SZ], tot = 1, cur[SZ];
int s, t;
void build(int F, int T, int D)
{
es[++tot] = (Edge){F, T, D};
nxt[tot] = first[F];
first[F] = tot;
}
int dis[SZ];
queue<int> q;
bool bfs()
{
q.push(s);
memset(dis, 0, sizeof(dis));
dis[s] = 1;
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = first[u]; i; i = nxt[i])
{
int v = es[i].t;
if(es[i].cap > 0 && !dis[v])
{
dis[v] = dis[u] + 1;
q.push(v);
}
}
}
if(dis[t]) return true;
return false;
}
int dfs(int u, int num)
{
if(u == t || num == 0)
return num;
int ans = 0;
int &i = cur[u];
for(; i; i = nxt[i])
{
int v = es[i].t;
if(dis[v] == dis[u] + 1 && es[i].cap > 0)
{
int f = dfs(v, min(num, es[i].cap));
ans += f;
es[i].cap -= f;
es[i ^ 1].cap += f;
num -= f;
if(num == 0) break;
}
}
return ans;
}
void insert(int ss, int tt, int dd)
{
build(ss, tt, dd);
build(tt, ss, 0);
}
int Max_flow()
{
int flow = 0;
while(bfs())
{
for(int i = 1; i <= t; i++)
cur[i] = first[i];
flow += dfs(s, INF);
}
return flow;
}
int main()
{
int n, f, d;
scanf("%d%d%d", &n, &f, &d);
s = n * 2 + f + d + 1;
t = s + 1;
int x = 2 * n + 1;
for(int i = x; i < x + f; i++)
insert(s, i, 1);
x += f;
for(int i = x; i < x + d; i++)
insert(i, t, 1);
for(int i = 1; i <= n; i++)
insert(i, i + n, 1);
int fi, di, ff;
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &fi, &di);
for(int j = 1; j <= fi; j++)
{
scanf("%d", &ff);
insert(ff + 2 * n, i ,1);
}
for(int j = 1; j <= di; j++)
{
scanf("%d", &ff);
insert(i + n, ff + 2 * n + f, 1);
}
}
printf("%d", Max_flow());
return 0;
}
本文详细介绍了一个基于最大流算法解决的食物分配问题。通过构建一个复杂的网络流模型,文章深入浅出地解释了如何利用最大流算法来寻找最优的食物与饮品分配方案,确保尽可能多的奶牛能够得到它们喜欢的食物和饮品。
359

被折叠的 条评论
为什么被折叠?



