码迷,mamicode.com
首页 > 其他好文 > 详细

POJ_3281_Dining && NYOJ _326_Dining

时间:2016-05-12 22:25:36      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

Dining
Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 13314
Accepted: 6092

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 的边,就保证了一头牛不会被分配多组食物和饮料。所以,直接计算最大流,就是答案。

源点  s --> 0;
      food --> (1,F)
      左牛 --> (F+1,F+N)
      右牛 (F+N+1,F+N+N)
      drink --> (F+N+N+1,F+N+N+D)
汇点  t = F+N+N+D+1

建完图,直接套 挑战上 模板

#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <algorithm>
#define Max_N 1005
#define inf 0x3f3f3f3f
using namespace std;

//用于表示结构体,终点,容量,反向边 
struct edge{ int to, cap ,rev; };
vector<edge> G[Max_N];     //图的邻接表表示 
int level[Max_N];          //顶点到源点的距离标号 
int iter[Max_N];           //当前弧,在其之前的边已经没有用了 
int N,F,D;

//向图中增加一条从from到to的容量为cap的边 
void add_edge(int from, int to, int cap)
{
	G[from].push_back((edge) { to, cap, G[to].size() } );
	G[to].push_back((edge) { from, 0, G[from].size() - 1 } );
}

//通过bfs计算从源点出发的距离标号 
void bfs(int s)
{
	memset(level,-1,sizeof(level));
	queue<int> Q;
	level[s] = 0;
	Q.push(s);
	while(!Q.empty())
	{
		int i;
		int v = Q.front(); Q.pop();
		for(i=0;i<G[v].size();i++)
		{
			edge &e = G[v][i];
			if(e.cap>0 && level[e.to]<0)
			{
				level[e.to] = level[v] + 1;
				Q.push(e.to);
			} 
		}
	} 
}

//通DFS寻找增广路
int dfs(int v,int t,int f)
{
	if(v==t)
	{
		return f;
	}
	for(int &i = iter[v];i<G[v].size();i++)
	{
		edge &e = G[v][i];
		if(e.cap > 0 && level[v] < level[e.to])
		{
			int d = dfs(e.to,t,min(f,e.cap));
			if(d>0)
			{
				e.cap -= d;
				G[e.to][e.rev].cap += d;
				return d;
			}
		}
	}
	return 0;
} 

//求解s到t的最大流 
int max_flow(int s,int t)
{
	int flow = 0;
	while(1)
	{
		bfs(s);
		if(level[t]<0)
		{
			return flow;
		}
		memset(iter,0,sizeof(iter));
		int f;
		while((f = dfs(s, t, inf)) >0 )
		{
			flow += f;
		}
	}
} 

int main()
{
    while(scanf("%d%d%d",&N,&F,&D)!=EOF)
    {
        int i,j; 
        int food,drink,v;
        for(i=0;i<Max_N;i++)
        {
        	G[i].clear();
        }
        for(i=1;i<=N;i++)
        {
            scanf("%d%d",&food,&drink);
            for(j=1;j<=food;j++)
            {
                scanf("%d",&v);
                add_edge(v,F+i,1);//食物和左边的牛 连一条 边 
            }
            for(j=1;j<=drink;j++)
            {
                scanf("%d",&v);
                add_edge(F+N+i,F+N+N+v,1);//右边的牛 和饮料连一条 边 
            }
        }      
		int s = 0,t = N+N+F+D+1;//超源,超汇 
        for(i=1;i<=F;i++)
        {
			add_edge(s,i,1); //源点 和所有 的食物连一条边 
		}
		for(i=1;i<=N;i++)
		{ 
			add_edge(F+i,F+N+i,1);//左边的牛 和 右边的牛连一条 边 
		}
        for(i=1;i<=D;i++)
        {  
			add_edge(F+N+N+i,t,1);//所有的饮料和 汇点连一条边 
		} 
        printf("%d\n",max_flow(s,t));
    }
    return 0;
}


POJ_3281_Dining && NYOJ _326_Dining

标签:

原文地址:http://blog.csdn.net/huangshuai147/article/details/51354673

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!