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

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

确保每个f和d都只能经过一次,将牛拆点,分别建f到牛,牛到d的容量为1的路,设置一个总起点0和总汇点2*n+d+f+1,跑一遍网络流

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define eps 0.00000001
#define pn printf("\n")
using namespace std;
typedef long long ll; int n,f,d;
const int MAXN = ;
const int MAXM = ;
struct Edge
{
int to,next,cap,flow;
}edge[MAXM];
set <pair<int,int> > st;
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN]; void init()
{
tol = ;
memset(head,-,sizeof(head));
}
//加边,单向图三个参数,双向图四个参数
void addedge(int u,int v,int w,int rw=) {
edge[tol].to = v;edge[tol].cap = w;edge[tol].next = head[u];
edge[tol].flow = ;head[u] = tol++;
edge[tol].to = u;edge[tol].cap = rw;edge[tol].next = head[v];
edge[tol].flow = ;head[v]=tol++;
} int sap(int start,int end,int N) {
memset(gap,,sizeof(gap));
memset(dep,,sizeof(dep));
memcpy(cur,head,sizeof(head));
int u = start;
pre[u] = -;
gap[] = N;
int ans = ;
while(dep[start] < N)
{
if(u == end)
{
int Min = INF;
for(int i = pre[u];i != -; i = pre[edge[i^].to])
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
for(int i = pre[u];i != -; i = pre[edge[i^].to])
{
edge[i].flow += Min;
edge[i^].flow -= Min;
}
u = start;
ans += Min;
continue;
}
bool flag = false;
int v;
for(int i = cur[u]; i != -;i = edge[i].next)
{
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v]+ == dep[u])
{
flag = true;
cur[u] = pre[v] = i; break;
}
}
if(flag)
{
u = v;
continue;
}
int Min = N;
for(int i = head[u]; i != -;i = edge[i].next)
if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
{
Min = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]--;
if(!gap[dep[u]])return ans;
dep[u] = Min+;
gap[dep[u]]++;
if(u != start) u = edge[pre[u]^].to;
}
return ans;
}
int main()
{
init();
int fi, di, x;
cin >> n >> f >> d;
for(int i=;i<=f;i++) addedge(, *n+i, );
for(int i=;i<=d;i++) addedge(*n+f+i, *n+f+d+, );
for(int i=;i<=n;i++) addedge(i, n+i, );
for(int i=;i<=n;i++)
{
scanf("%d%d",&fi,&di);
for(int j=;j<fi;j++)
{
scanf("%d",&x);
addedge(*n+x, i, );
}
for(int j=;j<di;j++)
{
scanf("%d",&x);
addedge(i+n,*n+f+x,);
}
}
cout << sap(, *n+f+d+, *n+f+d+) << endl;
}
上一篇:mean shift聚类算法的MATLAB程序


下一篇:poj 3281 Dining 网络流-最大流-建图的题