HDU4292 網絡流 2012 ACM/ICPC Asia Regional Chengdu Online1005
Food
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 555 Accepted Submission(s): 247
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
Source
Recommend
liuyiding
題解:比賽的時候沒看 現在看了一下很簡單的網絡流問題 開一個F+D+2*N+2的一個網絡 源點與各個食物點F相連 權值為各個食物的權值 再將食物點按照字符條件 將食物點與前N個顧客點對應條件一一相連 權值為1 為了保證顧客不會獲得兩種或者兩種以上的食物或飲料 將前N個顧客點與後N個顧客點相連 權值為1 再將後N個顧客點與飲料點相連 權值為1 再將飲料點與匯點相連 就可以找最大值了 這題起初我還考慮有的顧客隻要食物
或者有的顧客隻要飲料 但是這種測試數據裏好像沒有 所以我程序也沒加
#include <iostream> #include<cstdio> #include<cstring> using namespace std; const int oo=1e9; /**oo表示無窮大*/ const int mm=111111111; /**mm表示邊的最大數量,記住要是原圖的兩倍,在加邊的時候都是雙向的*/ const int mn=9999999; /**mn表示點的最大數量*/ int node,src,dest,edge; /**node表示節點數,src表示源點,dest表示匯點,edge統計邊數*/ int ver[mm],flow[mm],next[mm]; /**ver 邊指向的節點,flow 邊的容量 ,next 鏈表的下一條邊*/ int head[mn],work[mn],dis[mn],q[mn]; /**head 節點的鏈表頭,work 用於算法中的臨時鏈表頭,dis 計算距離*/ /**初始化鏈表及圖的信息*/ void prepare(int _node,int _src,int _dest) { node=_node,src=_src,dest=_dest; for(int i=0; i<node; ++i)head[i]=-1; edge=0; } /**增加一條u到v容量為c的邊*/ void addedge(int u,int v,int c) { ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++; ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++; } /**廣搜計算出每個點與源點的最短距離,如果不能到達匯點說明算法結束*/ bool Dinic_bfs() { int i,u,v,l,r=0; for(i=0; i<node; ++i)dis[i]=-1; dis[q[r++]=src]=0; for(l=0; l<r; ++l) for(i=head[u=q[l]]; i>=0; i=next[i]) if(flow[i]&&dis[v=ver[i]]<0) { /**這條邊必須有剩餘容量*/ dis[q[r++]=v]=dis[u]+1; if(v==dest)return 1; } return 0; } /**尋找可行流的增廣路算法,按節點的距離來找,加快速度*/ int Dinic_dfs(int u,int exp) { if(u==dest)return exp; /**work是臨時鏈表頭,這裏用i引用它,這樣尋找過的邊不再尋找*/ for(int &i=work[u],v,tmp; i>=0; i=next[i]) if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0) { flow[i]-=tmp; flow[i^1]+=tmp; /**正反向邊容量改變*/ return tmp; } return 0; } /**求最大流,直到沒有可行流*/ int Dinic_flow() { int i,ret=0,delta; while(Dinic_bfs()) { for(i=0; i<node; ++i)work[i]=head[i]; while(delta=Dinic_dfs(src,oo))ret+=delta; } return ret; } int main() { int n,f,d,ef,ed,ans; string sf,sd; while(cin>>n>>f>>d) { prepare(f+d+2*n+2,0,f+d+2*n+1); for(int i=1; i<=f; i++) { cin>>ef; addedge(src,i,ef); } for(int i=1; i<=d; i++) { cin>>ed; addedge(f+n+n+i,dest,ed); } for(int i=1; i<=n; i++) { addedge(f+i,f+n+i,1); cin>>sf; for(int j=0; j<f; j++) if(sf[j]=='Y') addedge(j+1,f+i,1); } for(int i=1; i<=n; i++) { cin>>sd; for(int j=0; j<d; j++) if(sd[j]=='Y') addedge(f+n+i,f+n+n+j+1,1); } ans=Dinic_flow(); cout<<ans<<endl; } return 0; }
最後更新:2017-04-02 15:28:28