当前位置: 代码迷 >> 综合 >> 拆点最大流-HDU-4292-Food
  详细解决方案

拆点最大流-HDU-4292-Food

热度:44   发布时间:2023-12-20 21:14:49.0

Food
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4539 Accepted Submission(s): 1536

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


题意:
已知有N位客人,F种食物,D种饮品,每种食物和饮品有自己对应的数量,每个客人有自己对应喜爱的食物和饮品,求最大能满足多少位客人。


题解:
这道题其实和POJ3281是一样的,将人拆点后放在食物和饮品之间就可以了。
从此也可以总结出,像这样一个有限的物体种类将另外两个物体种类联系起来的题,这个物体都放在两个物体中间建图。
顺便多嘴一句,这道题如果用cin,最好把同步关掉。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
using namespace std;
int N,F,D;
const int MAXN = 100010;//点数的最大值
const int MAXM = 400010;//边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge
{int to,next,cap,flow;
} edge[MAXM]; //注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];
void init()
{tol = 0;memset(head,-1,sizeof(head));
}
//加边,单向图三个参数,双向图四个参数
void addedge(int u,int v,int w,int rw=0)
{edge[tol].to = v;edge[tol].cap = w;edge[tol].next = head[u];edge[tol].flow = 0;head[u] = tol++;edge[tol].to = u;edge[tol].cap = rw;edge[tol].next = head[v];edge[tol].flow = 0;head[v]=tol++;
}
//输入参数:起点、终点、点的总数
//点的编号没有影响,只要输入点的总数
int sap(int start,int end,int N)
{memset(gap,0,sizeof(gap));memset(dep,0,sizeof(dep));memcpy(cur,head,sizeof(head));int u = start;pre[u] = -1;gap[0] = N;int ans = 0;while(dep[start] < N){if(u == end){int Min = INF;for(int i = pre[u]; i != -1; i = pre[edge[i^1].to])if(Min > edge[i].cap - edge[i].flow)Min = edge[i].cap - edge[i].flow;for(int i = pre[u]; i != -1; i = pre[edge[i^1].to]){edge[i].flow += Min;edge[i^1].flow -= Min;}u = start;ans += Min;continue;}bool flag = false;int v;for(int i = cur[u]; i != -1; i = edge[i].next){v = edge[i].to;if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u]){flag = true;cur[u] = pre[v] = i;break;}}if(flag){u = v;continue;}int Min = N;for(int i = head[u]; i != -1; i = edge[i].next)if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min){Min = dep[edge[i].to];cur[u] = i;}gap[dep[u]]--;if(!gap[dep[u]])return ans;dep[u] = Min+1;gap[dep[u]]++;if(u != start) u = edge[pre[u]^1].to;}return ans;
}
int main(void)
{ios::sync_with_stdio(false);int tmp;string str;while(cin >> N >> F >> D){init();for(int i=1;i<=F;i++){cin >> tmp;addedge(0,i,tmp);}for(int i=F+1;i<=F+D;i++){cin >> tmp;addedge(i,D+F+2*N+1,tmp);}for(int i=1;i<=N;i++){cin >> str;for(int j=0;j<F;j++)if(str[j]=='Y')addedge(j+1,D+F+i,INF);}for(int i=1;i<=N;i++){cin >> str;for(int j=0;j<D;j++)if(str[j]=='Y')addedge(D+F+N+i,F+j+1,INF);}for(int i=1;i<=N;i++)addedge(D+F+i,D+F+N+i,1);cout << sap(0,D+F+2*N+1,D+F+2*N+2) << endl;}return 0;
}