poj 3281 Dining 【拆点网络流】【最大流经典问题 关键建图】

Dining
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10755 Accepted: 4930

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。求最大流。

 

当然改动点时看个人爱好,我的是左牛为1到n,右牛n+1到2*n,食物2*n+1到2*n+f,饮料2*n+f+1到2*n+f+d。超级源为0,超级汇为2*n+f+d+1。

dinic算法:32ms

 

 

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#define MAXN 400+10//食物从2*n+1开始 到 2*n+f 饮料从2*n+f+1 开始 到2*n+1+f+d 
#define MAXM 200000+100
#define INF 200000+10
using namespace std;
struct Edge
{
	int from, to, cap, flow, next;
}edge[MAXM];
int dist[MAXN], vis[MAXN], head[MAXN], cur[MAXN], top;
int n, f, d;
void init()
{
	top = 0;
	memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int w)
{
	Edge E1 = {u, v, w, 0, head[u]};
	edge[top] = E1;
	head[u] = top++;
	Edge E2 = {v, u, 0, 0, head[v]};
	edge[top] = E2;
	head[v] = top++;
}
void getmap()
{
	int i, j;
	int fnum, dnum;
	for(i = 1; i <= n; i++)
	{
		scanf("%d%d", &fnum, &dnum);
		while(fnum--)//食物 编号2*n+j 
		{
			scanf("%d", &j);
			addedge(2*n+j, i, 1);//食物和对应奶牛连通 
		}
		while(dnum--)//饮料 编号2*n+f+j 
		{
			scanf("%d", &j);
			addedge(n+i, 2*n+f+j, 1);//奶牛和对应饮料连通 
		}
		addedge(i, n+i, 1);//奶牛自己和自己连一次 
	}
	for(i = 1; i <= f; i++)
	addedge(0, 2*n+i, 1);//超级源点连通所有食物
	for(i = 1; i <= d; i++)
	addedge(2*n+f+i, 2*n+f+d+1, 1);//所有饮料连通超级汇点 
}
bool BFS(int start, int end)
{
	queue<int> Q;
	memset(dist, -1, sizeof(dist));
	memset(vis, 0, sizeof(vis));
	while(!Q.empty()) Q.pop();
	Q.push(start);
	dist[start] = 0;
	vis[start] = 1;
	while(!Q.empty())
	{
		int u = Q.front();
		Q.pop();
		for(int i = head[u]; i != -1; i = edge[i].next)
		{
			Edge E = edge[i];
			if(!vis[E.to] && E.cap > E.flow)
			{
				vis[E.to] = 1;
				dist[E.to] = dist[u] + 1;
				if(E.to == end) return true;
				Q.push(E.to); 
			}
		}
	}
	return false;
}
int DFS(int x, int a, int end)
{
	if(x == end || a == 0) return a;
	int flow = 0, f;
	for(int& i = cur[x]; i != -1; i = edge[i].next)
	{
		Edge& E = edge[i];
		if(dist[E.to] == dist[x]+1 && (f = DFS(E.to, min(a, E.cap-E.flow), end)) > 0)
		{
			E.flow += f;
			edge[i^1].flow -= f;
			flow += f;
			a -= f;
			if(a == 0) break;
		}
	}
	return flow;
}
int Maxflow(int start, int end)
{
	int flow = 0;
	while(BFS(start, end))
	{
		memcpy(cur, head, sizeof(head));
		flow += DFS(start, INF, end);
	}
	return flow;
}
int main()
{
	while(scanf("%d%d%d", &n, &f, &d) != EOF)
	{
		init();
		getmap();
		printf("%d\n", Maxflow(0, 2*n+f+d+1));
	}
	return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值