您的位置:首页 > 其它

poj 3281 Dining(最大流+拆点)

2013-10-13 15:57 225 查看
Dining

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 7941Accepted: 3622
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.

题意:有n头牛,f个食物,d个饮料。给出每头牛喜欢哪些食物和饮料,要求使尽量多牛都有一件食物和一个饮料,一件食物或一个饮料最多只能分配给一头牛。输出最多有几头牛拥有一件食物和一个饮料。
思路:最大流问题。构图如下:
1.源点到每件食物连一条边,容量为1。表示该种食物只有一件。
2.每个饮料到汇点连一条边,容量也为1。表示该种饮料只有一件。
3.将每头牛i拆成两个点i和i+n,该牛喜欢的食物连一条边到i,容量为1,i+n连一条边到该牛喜欢的饮料,容量也为1。最后i连一条边到i+n,保证每头牛只有一个食物和一个饮料。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
#define eps 1e-6
using namespace std;

const int INF=1000000000;
const int maxn=505;
struct Edge{
int u,v,cap,flow,next;
}et[maxn*maxn];
int low[maxn],dis[maxn],cnt[maxn],pre[maxn],cur[maxn],eh[maxn];
int s,t,num,n,f,d;
void init(){
memset(eh, -1, sizeof(eh));
num = 0;
}
void add(int u, int v, int cap, int flow){
Edge e = {u, v, cap, flow, eh[u]};
et[num] = e;
eh[u] = num ++;
}
void addedge(int u, int v, int cap){
add(u, v, cap, 0);
add(v, u, 0, 0);
}
int isap(int s,int t,int nv){
int u, v, now, flow = 0;
memset(cnt, 0, sizeof(cnt));
memset(dis, 0, sizeof(dis));
memset(low, 0, sizeof(low));
for(u = 0; u <= nv; u ++) cur[u] = eh[u];
low[s] = INF, cnt[0] = nv, u = s;
while(dis[s] < nv){
for(now = cur[u]; now != -1; now = et[now].next)
if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break;
if(now != -1){
cur[u] = pre[v] = now;
low[v] = min(et[now].cap - et[now].flow, low[u]);
u = v;
if(u == t){
for(; u != s; u = et[pre[u]].u){
et[pre[u]].flow += low[t];
et[pre[u]^1].flow -= low[t];
}
flow += low[t];
low[s] = INF;
}
}
else{
if(--cnt[dis[u]] == 0) break;
dis[u] = nv, cur[u] = eh[u];
for(now = eh[u]; now != -1; now = et[now].next)
if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1)
dis[u] = dis[et[now].v] + 1;
cnt[dis[u]] ++;
if(u != s) u = et[pre[u]].u;
}
}
return flow;
}
int main()
{
int fi,di,a;
while(~scanf("%d%d%d", &n, &f, &d)){
init();
s = 0, t = n*2+f+d+1;
for(int i = 1; i <= n; i ++){
scanf("%d%d", &fi, &di);
while(fi --){
scanf("%d",&a);
addedge(a+2*n, i, 1);
}
while(di --){
scanf("%d",&a);
addedge(i+n, a+2*n+f, 1);
}
addedge(i, i+n, 1);
}
for(int i = 1; i <= f; i ++) addedge(s, i+2*n, 1);
for(int i = 1; i <= d; i ++) addedge(i+2*n+f, t, 1);
printf("%d\n",isap(s, t, t+1));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: