HDU 4292 FOOD 2012 ACM/ICPC Asia Regional Chengdu Online

                      Food

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


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 3
1 1 1
1 1 1
YYN
NYY
YNY
YNY
YNY
YYN
YYN
NNY
 
Sample Output
3
 
Source

 

题意:

一个餐馆有F种食物和D种饮料,每种食物和饮料都有固定的量,和N个顾客。

每个顾客会同时点饮料和食物,而且只会点他们喜欢的食物和饮料。

若顾客没有得到他喜欢的饮料和食物,则这位顾客不满意,会马上离开。

问要怎么分配食物和饮料给顾客,使得尽量多的顾客满意。

 

网络流

建图:S-食物F—顾客N—顾客N—饮料D—T

S和F,边的容量为食物的量

F和N,若顾客喜欢该种食物,则他们之间有一条容量为1的边

N和N,相应的点加边,容量为1(保证了每个顾客只点一份饮料+食物)

N和D,若顾客喜欢该种饮料,则加边,容量为1

D和T,边的容量为饮料的量

然后跑一遍dinic,即可。

问题:

1.刚开始,数组edge忘记初始化了

2.初始化的时候从下标1开始,而下标0(即源点)忘记初始化了

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<vector>
  5 #include<queue>
  6 
  7 using namespace std;
  8 
  9 const int MAXN=810;
 10 const int INF=0x3f3f3f3f;
 11 
 12 struct Edge
 13 {
 14     int to,cap,rev;
 15 };
 16 vector<Edge>edge[MAXN];
 17 int level[MAXN];
 18 int iter[MAXN];
 19 const int S=0;
 20 int T;
 21 char str[205];
 22 
 23 void addedge(int u,int v,int cap)
 24 {
 25     edge[u].push_back((Edge){v,cap,edge[v].size()});
 26     edge[v].push_back((Edge){u,0,edge[u].size()-1});
 27 }
 28 
 29 void bfs()
 30 {
 31     memset(level,-1,sizeof(level));
 32     queue<int>que;
 33     while(!que.empty())
 34         que.pop();
 35 
 36     level[S]=0;
 37     que.push(S);
 38     while(!que.empty())
 39     {
 40         int u=que.front();
 41         que.pop();
 42         for(int i=0;i<edge[u].size();i++)
 43         {
 44             Edge &e=edge[u][i];
 45             if(e.cap>0&&level[e.to]<0)
 46             {
 47                 level[e.to]=level[u]+1;
 48                 que.push(e.to);
 49             }
 50         }
 51     }
 52 }
 53 
 54 int dfs(int u,int f)
 55 {
 56     if(u==T)
 57         return f;
 58     for(int &i=iter[u];i<edge[u].size();i++)
 59     {
 60         Edge &e=edge[u][i];
 61         if(e.cap>0&&level[e.to]>level[u])
 62         {
 63             int d=dfs(e.to,min(f,e.cap));
 64             if(d>0)
 65             {
 66                 e.cap-=d;
 67                 edge[e.to][e.rev].cap+=d;
 68                 return d;
 69             }
 70         }
 71     }
 72     return 0;
 73 }
 74 
 75 int max_flow()
 76 {
 77     int flow=0;
 78     while(true)
 79     {
 80         bfs();
 81         if(level[T]<0)
 82             return flow;
 83         memset(iter,0,sizeof(iter));
 84         int f;
 85         while(f=dfs(S,INF)>0)
 86         {
 87             flow+=f;
 88         }
 89     }
 90 }
 91 
 92 int main()
 93 {
 94     int N,F,D;
 95     while(~scanf("%d%d%d",&N,&F,&D))
 96     {
 97         for(int i=0;i<MAXN;i++)
 98             edge[i].clear();
 99 
100         T=F+2*N+D+1;
101         for(int i=1;i<=F;i++)
102         {
103             int w;
104             scanf("%d",&w);
105             addedge(S,i,w);
106         }
107         for(int i=1;i<=D;i++)
108         {
109             int w;
110             scanf("%d",&w);
111             addedge(F+2*N+i,T,w);
112         }
113         for(int i=1;i<=N;i++)
114         {
115             addedge(i+F,i+N+F,1);
116         }
117         for(int i=1;i<=N;i++)
118         {
119             scanf("%s",str);
120             for(int j=1;j<=F;j++)
121             {
122                 if(str[j-1]=='Y')
123                     addedge(j,F+i,1);
124             }
125         }
126         for(int i=1;i<=N;i++)
127         {
128             scanf("%s",str);
129             for(int j=1;j<=D;j++)
130             {
131                 if(str[j-1]=='Y')
132                     addedge(F+N+i,F+2*N+j,1);
133             }
134         }
135 
136         int flow=max_flow();
137 
138         printf("%d
",flow);
139     }
140 
141     return 0;
142 }
View Code
原文地址:https://www.cnblogs.com/-maybe/p/4657130.html