https://vjudge.net/contest/402729#problem/F
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 NNYSample Output
3Sponsor
代码:
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> #include<time.h> using namespace std; typedef long long ll; const ll maxn=200000+10; ll n,m,k,s,t,h[maxn],cur[maxn],cnt=1,vis[maxn]; ll F,D; char x[maxn]; struct node{ ll to,nt,w; }e[maxn]; void add(ll u,ll v,ll w) { e[++cnt]={v,h[u],w}; h[u]=cnt; } queue<ll>q; bool bfs() { memset(vis,0,sizeof(vis)); vis[s]=1; q.push(s); while(!q.empty()) { ll u=q.front(); q.pop(); cur[u]=h[u]; for(ll i=h[u];i;i=e[i].nt) { ll v=e[i].to,w=e[i].w; if(w&&!vis[v]) { vis[v]=vis[u]+1; q.push(v); } } } return vis[t]; } ll dfs(ll u,ll flow) { if(u==t) return flow; ll res=flow; for(ll i=cur[u];i;i=e[i].nt) { ll v=e[i].to,w=e[i].w; if(w&&vis[u]+1==vis[v]) { ll now=dfs(v,min(res,w)); if(!now) vis[v]=1; else { e[i].w-=now; e[i^1].w+=now; res-=now; } } if(!res) return flow; } return flow-res; } int main() { while(~scanf("%lld %lld %lld",&n,&F,&D)) { memset(cur,0,sizeof(cur)); memset(h,0,sizeof(h)); s=0; t=n*2+F+D+1; cnt=1; for(int j=1;j<=F;j++) { int u; scanf("%d",&u); add(0,j,u); add(j,0,0); } for(int j=1;j<=D;j++) { int u; scanf("%d",&u); add(j+F+n*2,t,u); add(t,j+F+n*2,0); } for(int i=1;i<=n;i++) { scanf("%s",x); int len=strlen(x); for(int j=0;j<len;j++) { if(x[j]=='Y') { add(j+1,i+F,1); add(i+F,j+1,0); } } add(i+F,i+F+n,1); add(i+F+n,i+F,0); } for(int i=1;i<=n;i++) { scanf("%s",x); int len=strlen(x); for(int j=0;j<len;j++) { if(x[j]=='Y') { add(i+F+n,F+n*2+j+1,1); add(F+n*2+j+1,i+F+n,0); } } } ll ans=0; while(!q.empty()) q.pop(); while(bfs()) { ans+=dfs(s,0x7fffffff); //cout<<ans<<endl; } printf("%lld\n",ans); } return 0; }