POJ 3281 Dining(最大流dinicSAP)

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 cookedF(1 ≤F≤ 100) types of foods and preparedD(1 ≤D≤ 100) types of drinks. Each of hisN(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, andDLines 2..N+1: Each lineistarts with a two integersFiandDi, the number of dishes that cowilikes and the number of drinks that cowilikes. The nextFiintegers denote the dishes that cowiwill eat, and theDiintegers following that denote the drinks that cowiwill 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 mealCow 2: Food #2, Drink #2Cow 3: Food #1, Drink #1Cow 4: Food #3, Drink #3The 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.

题意:每头牛都要一种食物和一种饮料,最多多少牛能够满足。

网络流拆点求最大流。建图,S->食物->牛->牛->饮料->T

因为牛的数量是一定的,为了保证每头牛用一次要拆点,A->A’之间连一条容量为1的边。

跑一下最大流即可。

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int mod = 1000000007;const int INF=0x3f3f3f3f;const int maxn=440;int mp[maxn][maxn];int dis[maxn],q[maxn];int n,f,d,N,maxflow,s,t;bool BFS(){CLEAR(dis,-1);dis[s]=0;int h=0,r=0;q[++r]=s;while(h<r){int x=q[++h];for(int i=0;i<N;i++){if(dis[i]<0&&mp[x][i]){dis[i]=dis[x]+1;q[++r]=i;}}}if(dis[t]>0) return true;else return false;}int dfs(int x,int low){int a=0;if(x==t) return low;for(int i=0;i<N;i++){if(mp[x][i]>0&&dis[i]==dis[x]+1&&(a=dfs(i,min(low,mp[x][i])))){mp[x][i]-=a;mp[i][x]+=a;return a;}}return 0;}void dinic(){maxflow=0;while(BFS())maxflow+=dfs(s,INF);}int main(){int s1,s2,x;while(~scanf("%d%d%d",&n,&f,&d)){CLEAR(mp,0);s=0;t=2*n+f+d+1;for(int i=1;i<=f;i++) mp[s][i]=1;for(int i=1;i<=d;i++) mp[2*n+f+i][t]=1;for(int i=1;i<=n;i++) mp[f+i][f+n+i]=1;for(int i=1;i<=n;i++){scanf("%d%d",&s1,&s2);for(int j=1;j<=s1;j++){scanf("%d",&x);mp[x][f+i]=1;}for(int j=1;j<=s2;j++){scanf("%d",&x);mp[f+n+i][f+2*n+x]=1;}}N=t+1;dinic();printf("%d\n",maxflow);}return 0;}HDU 4292 :

Problem Description

  You, a part-time dining service worker in your college’s dining hall, are now confused with a new problem: serve as many people as possible.  The issue comes up as people in your college are more and more difficult to serve with meal: They eat only some certain kinds of food and drink, and with requirement unsatisfied, go away directly.  You have prepared F (1 <= F <= 200) kinds of food and D (1 <= D <= 200) kinds of drink. Each kind of food or drink has certain amount, that is, how many people could this food or drink serve. Besides, You know there’re N (1 <= N <= 200) people and you too can tell people’s personal preference for food and drink.  Back to your goal: to serve as many people as possible. So you must decide a plan where some people are served while requirements of the rest of them are unmet. You should notice that, when one’s requirement is unmet, he/she would just go away, refusing any service.

Input

  There are several test cases.  For each test case, the first line contains three numbers: N,F,D, denoting the number of people, food, and drink.  The second line contains F integers, the ith number of which denotes amount of representative food.  The third line contains D integers, the ith number of which denotes amount of representative drink.  Following is N line, each consisting of a string of length F. e jth character in the ith one of these lines denotes whether people i would accept food j. “Y” for yes and “N” for no.  Following is N line, each consisting of a string of length D. e jth character in the ith one of these lines denotes whether people i would accept drink j. “Y” for yes and “N” for no.  Please process until EOF (End Of File).

Output

  For each test case, please print a single line with one integer, the maximum number of people to be satisfied.

Sample Input

4 3 31 1 11 1 1YYNNYYYNYYNYYNYYYNYYNNNY

Sample Output

3

题意差不多,,建图差不多。

只不过食物和牛以及牛与饮料的关系用图表示了。我的没优化的dinic跑TLE了。

SAP:

如果前世五百次眸回,才换来今生的擦肩而过。

POJ 3281 Dining(最大流dinicSAP)

相关文章:

你感兴趣的文章:

标签云: