poj 3281 Dining //顶点限流(最大流)

本文介绍了一个典型的顶点限流最大流问题——《Dining》问题,该问题来源于POJ 3281。文章详细展示了如何通过构建图模型并运用Dinic算法求解,以最大化满足牛群食物和饮料偏好配对的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://poj.org/problem?id=3281

poj 3281 Dining //顶点限流(最大流)

 

Dining

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 24520 Accepted: 10819

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: NF, 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.

 注意一头牛只能对应一个pair<drink,food>  所以对于顶点,需要拆开限流。

上图满足一个food对于一个cow,一个cow对应一个drink,并且流过牛的只有一条路。

然后跑一下最大流。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<climits>
#include<cmath>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
#define LL long long
#define mod 1000000007
const int max_n=505;
struct no{int to,cap,rev;}; //arc
vector<no>g[max_n]; //图
int level[max_n]; //到起点的距离
int iter[max_n]; //当前弧,在其之前的边已经没用了
void addarc(int s,int e,int c){
	g[s].push_back((no){e,c,g[e].size()});
	g[e].push_back((no){s,0,g[s].size()-1});
}
//更新层次,即level
void bfs(int s){
	memset(level,-1,sizeof(level));
	level[s]=0;
	queue<int>q;
	q.push(s);
	while(!q.empty()){
		int now=q.front();q.pop();
		for(int i=0;i<(int)g[now].size();i++){
			no &arc=g[now][i];
			if(level[arc.to]!=-1||arc.cap<=0) continue;
			level[arc.to]=level[now]+1;
			q.push(arc.to);
		}
	}
}
//寻找增广路
int dfs(int v,int t,int f){
	if(v==t) return f;
	for(iter[v];iter[v]<(int)g[v].size();iter[v]++){
		no &arc=g[v][iter[v]];
		if(arc.cap<=0||level[arc.to]!=level[v]+1) continue;
		int d=dfs(arc.to,t,min(f,arc.cap));
		if(d>0) {
			arc.cap=arc.cap-d;
			g[arc.to][arc.rev].cap+=d;
			return d;
		}
	}
	return 0;
}
int Dinic(int s,int t){
	int re=0;
	while(1){
		bfs(s);
		memset(iter,0,sizeof(iter));
		if(level[t]==-1) return re;
		int f;
		while((f=dfs(s,t,INT_MAX))>0)
			re=re+f;
	}
	return re;
}
void slove(){

}
int main(){
    ios::sync_with_stdio(false); cin.tie(0);
    int F,D,N;
    cin>>N>>F>>D;
    for(int i=0;i<max_n;i++) g[i].clear();
    for(int i=1;i<=F;i++) addarc(0,i,1);  //s->Food
    for(int i=F+2*N+1;i<=F+2*N+D;i++) addarc(i,2*N+F+D+1,1);//Drink->t
    for(int i=1;i<=N;i++){
        int a,b;cin>>a>>b;
        while(a--){
            int x;cin>>x;
            addarc(x,F+i,1); //Food->cow1
        }
        while(b--){
            int x;cin>>x;
            addarc(F+i+N,F+2*N+x,1); //cow2->Drink
        }
    }
    for(int i=F+1;i<=F+N;i++) addarc(i,i+N,1); //cow1->cow2
    int ans=Dinic(0,F+2*N+D+1);
    cout<<ans<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值