Food
Time Limit: 1000 MS Memory Limit: 32768 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
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
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
Sample Input
4 3 3 1 1 1 1 1 1 YYN NYY YNY YNY YNY YYN YYN NNY
Sample Output
3
Source
2012 ACM/ICPC Asia Regional Chengdu Online
题意:有n个人,F种食物(各Fi个),D种饮料(各Di个)。各人都有喜爱的食物和饮料,每个人想要一个食物和一个饮料,问最大匹配是多少。最大流模板,注意让人与人连一次就行。G:yuan-food-ren-ren-drink-hui。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
struct edge{
int to,cap,rev;
edge(int a=0,int b=0,int c=0):to(a),cap(b),rev(c){}
};
const int maxn = 802;
const int inf = 2147483647;
vector<edge> G[maxn];
int iter[maxn];
int level[maxn];
void Add(int f,int t,int c)
{
G[f].push_back(edge{t,c,G[t].size() } );
G[t].push_back(edge{f,0,G[f].size()-1} );
}
void bfs(int s)
{
memset(level,-1,sizeof(level));
queue<int> que;
level[s] = 0;
que.push(s);
while(!que.empty()){
int v = que.front();
que.pop();
for(int i = 0;i < G[v].size();i++){
edge &e = G[v][i];
if(e.cap > 0 && level[e.to] == -1){
level[e.to] = level[v]+1;
que.push(e.to);
}
}
}
}
int dfs(int v,int t,int f)
{
if(v == t) return f;
for(int &i = iter[v];i < G[v].size();i++){
edge &e = G[v][i];
if(e.cap > 0 && level[v] < level[e.to]){
int d = dfs(e.to,t,min(e.cap,f));
if(d > 0){
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int mflow(int s,int t,int fmax)
{
int flow = 0;
while(true){
bfs(s);
if(level[t] == -1) return flow;
memset(iter,0,sizeof(iter));
int f = 0;
while((f = dfs(s,t,inf)) > 0)
flow += f;
}
}
void init()
{
for(int i = 0;i < maxn;i++){
G[i].clear();
}
}
int main()
{
int n,food,drink;
while(scanf("%d%d%d",&n,&food,&drink) != EOF){
init();
int yuan = 0;
int hui = n*2+food+drink+1;
int num;
for(int i = 1;i <= n;i++){
Add(food+i,food+n+i,1);
}
for(int i = 1;i <= food;i++){
scanf("%d",&num);
Add(yuan,i,num);
}
for(int i = 1;i <= drink;i++){
scanf("%d",&num);
Add(n*2+food+i,hui,num);
}
char chi;
for(int i = 1;i <= n;i++){
getchar();
for(int j = 1;j <= food;j++){
scanf("%c",&chi);
if(chi == 'Y'){
Add(j,food+i,1);
}
}
}
for(int i = 1;i <= n;i++){
getchar();
for(int j = 1;j <= drink;j++){
scanf("%c",&chi);
if(chi == 'Y'){
Add(n+food+i,n*2+food+j,1);
}
}
}
int ans = mflow(yuan,hui,inf);
printf("%d\n",ans);
}
return 0;
}