Food
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6626 Accepted Submission(s): 2250
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.
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).
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
思路:将0定为超级源点,n*2+d+f+1定为超级汇点,然后运用dinic算法即可。
代码:
#include<iostream> #include<stdio.h> #include<string.h> #include <queue> #include <vector> #include <algorithm> using namespace std; const int inf=2147483647; const int maxn=1001; const int maxm=maxn*maxn*2; int n,f,d; int s,t; int cnt; char c[maxn]; int head[maxn]; int Next[maxm]; struct edge{ int u,v,f; }e[maxm]; int depth[maxn]; void addedge(int u,int v,int w) { cnt++; Next[cnt]=head[u]; head[u]=cnt; e[cnt].u=u; e[cnt].v=v; e[cnt].f=w; cnt++; Next[cnt]=head[v]; head[v]=cnt; e[cnt].u=v; e[cnt].v=u; e[cnt].f=0; } int bfs() { queue<int>q; memset(depth,-1,sizeof(depth)); depth[s]=1; q.push(s); while(!q.empty()) { int k=q.front(); q.pop(); int i; for(i=head[k];i!=-1;i=Next[i]) { int v=e[i].v; if((depth[v]==-1)&&(e[i].f>0)) { depth[v]=depth[k]+1; q.push(v); } } } if(depth[t]==-1) return 0; return 1; } int dfs(int u,int f) { if(u==t) return f; for(int i=head[u];i!=-1;i=Next[i]) { int v=e[i].v; if((depth[v]==depth[u]+1)&&(e[i].f>0)) { int d=dfs(v,min(f,e[i].f)); if(d>0) { e[i].f-=d; e[i^1].f+=d; return d; } } } return 0; } int main() { while(~scanf("%d%d%d",&n,&f,&d)) { int i; s=0,t=2*n+d+f+1; int w; cnt=-1; memset(head,-1,sizeof(head)); for(i=1;i<=f;i++) { scanf("%d",&w); addedge(0,n*2+i,w); } for(i=1;i<=d;i++) { scanf("%d",&w); addedge(2*n+f+i,t,w); } for(i=1;i<=n;i++) { scanf("%s",c); for(int j=0;j<f;j++) { if(c[j]=='Y') { addedge(n*2+j+1,i,1); } } } for(i=1;i<=n;i++) { scanf("%s",c); for(int j=0;j<d;j++) { if(c[j]=='Y') { addedge(n+i,n*2+f+j+1,1); } } } for(i=1;i<=n;i++) { addedge(i,n+i,1); } int Ans=0; while (bfs()) { while (int di=dfs(0,inf)) Ans+=di; } cout<<Ans<<endl; } return 0; }