HDU 4292 Food 网络流建图

来源:互联网 发布:scdma是什么网络 编辑:程序博客网 时间:2024/06/07 01:48

http://acm.hdu.edu.cn/showproblem.php?pid=4292

Food

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4827    Accepted Submission(s): 1632


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

题意:有f种食物,d种饮料,给出每种食物饮料的食量,现在有n个人,告诉你每个人对每种食物饮料的喜爱与否,求最多有多少个人能够同时拥有自己喜欢的食物和饮料。

思路:建立源点连接所有食物,边权值为食物数量,同理建立汇点连接所有饮料。构成图之后就是一道最大流模板题了。

#include<stdio.h>#include<algorithm>#include<string.h>#include<vector>using namespace std;const int maxm=1000;const int inf=10000000;struct node{int to,cap,rev;};vector<node>v[1000];bool used[1000];char ch[500][500];int max_flow(int s,int t);int dfs(int s,int t,int ff);int main(){int i,j,k,sum,a[maxm],b[maxm],n,f,d;while(scanf("%d%d%d",&n,&f,&d)!=EOF){memset(v,0,sizeof(v));for(i=1;i<=f;i++)scanf("%d",&a[i]);for(i=1;i<=d;i++)scanf("%d",&b[i]);for(i=1;i<=n;i++){scanf("%s",ch[i]+1);for(j=1;j<=f;j++){if(ch[i][j]=='Y'){int t=2*n+j;v[t].push_back((node){i,1,v[i].size()});v[i].push_back((node){t,0,v[t].size()-1});}}}for(i=1;i<=n;i++){scanf("%s",ch[i]+1);for(j=1;j<=d;j++){if(ch[i][j]=='Y'){int t=2*n+f+j;v[i+n].push_back((node){t,1,v[t].size()});v[t].push_back((node){i+n,0,v[i+n].size()-1});}}}for(i=1;i<=n;i++){v[i].push_back((node){i+n,1,v[i+n].size()});v[i+n].push_back((node){i,0,v[i].size()-1});}for(i=2*n+1;i<=2*n+f;i++){v[0].push_back((node){i,a[i-2*n],v[i].size()});v[i].push_back((node){0,0,v[0].size()-1});}int e=2*n+f+d+1;for(i=2*n+f+1;i<=2*n+f+d;i++){v[i].push_back((node){e,b[i-2*n-f],v[e].size()});v[e].push_back((node){i,0,v[i].size()-1});} printf("%d\n",max_flow(0,e));}return 0;}int max_flow(int s,int t){int i,j,ans=0;while(1){memset(used,false,sizeof(used));int d=dfs(s,t,inf);if(d==0)return ans;ans+=d;}}int dfs(int s,int t,int ff){int i,j;if(s==t)return ff;used[s]=true;for(i=0;i<v[s].size();i++){node &temp=v[s][i];if(!used[temp.to] && temp.cap>0){int d=dfs(temp.to,t,min(ff,temp.cap));if(d>0){temp.cap-=d;v[temp.to][temp.rev].cap+=d;return d;}}}return 0;}


0 0
原创粉丝点击