poj3281 dining 网络流最大流算法

来源:互联网 发布:网络育人工作心得体会 编辑:程序博客网 时间:2024/05/16 14:14

Dining
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 18614 Accepted: 8308

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 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 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个cow,,每个cow喜欢不同的食物和水,你要为他们分配食物和水,每种食物或水只能被分配一次,每个cow分配了食物和水之后就满足这个cow的需求,问你最大能满足几个cow的需求。

人思路:把每个cow分成两个点,并连接起来,食物做为一端,饮料作为另一端,然后加上一个源点和一个汇点,两点之间最大容量是1,就转化成了一个最大流的问题,然后我用的dinic算法解答的。

#include<cstdio>#include<string>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<map>#include<set>#include<stack>#define ll long long#define read(a) scanf("%d",&a);using namespace std;const int maxn=405;const int inf=99999999;int f,d,n;int c[maxn][maxn];int dis[maxn];bool bfs(){memset(dis,-1,sizeof(dis));queue<int>que;que.push(0);dis[0]=0;while(!que.empty()){int num=que.front();que.pop();for(int i=0;i<=n;i++){if(c[num][i]>0&&dis[i]==-1){dis[i]=dis[num]+1;que.push(i);}}}if(dis[n]!=-1)return true;return false;}int find(int x,int low){int a=0;if(x==n)return low; for(int i=0;i<=n;i++){if(c[x][i]>0&&dis[i]==dis[x]+1&&(a=find(i,min(low,c[x][i])))){c[x][i]-=a;c[i][x]+=a;return a;}}return 0;}int main(){freopen("test.txt","r",stdin);int fi,di;int num;int ans;while(~scanf("%d %d %d",&n,&f,&d)){//printf("%d %d %d\n",n,f,d);memset(c,0,sizeof(c));for(int i=1;i<=n;i++){scanf("%d %d",&fi,&di);//printf("%d %d ",fi,di);for(int j=1;j<=fi;j++){//foodscanf("%d",&num);//printf("%d ",num);c[num][i+f]=1;c[0][num]=1;}for(int j=1;j<=di;j++){//drinkscanf("%d",&num);//printf("%d ",num);c[f+n+i][num+f+n+n]=1;c[num+f+n+n][f+n+n+d+1]=1;}c[i+f][i+f+n]=1;//printf("\n");}n=n+n+f+d;n++;ans=0;while(bfs()){//printf(">>>>>>>>>>>>>>>");int tmp;while(tmp=find(0,inf)){ans+=tmp;//printf("%d %d\n",ans,tmp);}}printf("%d\n",ans);}return 0;}

EK算法:

#include<cstdio>#include<string>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<map>#include<set>#include<stack>#define ll long long#define read(a) scanf("%d",&a);using namespace std;const int maxn=405;const int inf=99999999;int f,d,n;int c[maxn][maxn];int dis[maxn];int pre[maxn];int main(){freopen("test.txt","r",stdin);int fi,di;int num;int ans;while(~scanf("%d %d %d",&n,&f,&d)){memset(c,0,sizeof(c));for(int i=1;i<=n;i++){scanf("%d %d",&fi,&di);for(int j=1;j<=fi;j++){//foodscanf("%d",&num);c[num][i+f]=1;c[0][num]=1;}for(int j=1;j<=di;j++){//drinkscanf("%d",&num);c[f+n+i][num+f+n+n]=1;c[num+f+n+n][f+n+n+d+1]=1;}c[i+f][i+f+n]=1;}n=n+n+f+d;n++;ans=0;while(1){memset(pre,-1,sizeof(pre));queue<int>que;que.push(0);while(!que.empty()){int num=que.front();que.pop();if(num==n) break;for(int i=0;i<=n;i++){if(c[num][i]>0&&pre[i]==-1){pre[i]=num;que.push(i);}}}if(pre[n]==-1)break;ans+=1;int tmp=n;while(tmp!=0){c[pre[tmp]][tmp]-=1;c[tmp][pre[tmp]]+=1;tmp=pre[tmp];}}printf("%d\n",ans);}return 0;}


原创粉丝点击