【dinic最大流】H - Food HDU - 4292

Food

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7398    Accepted Submission(s): 2479


 

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

 

Source

2012 ACM/ICPC Asia Regional Chengdu Online

#include <bits/stdc++.h>
using namespace std;

const int inf = 0x3f3f3f3f;
const int mn = 900, mm = 201000;  /// 反向边注意边的数组大小

int edge;
int fr[mn];
int lv[mn];
struct node
{
	int to, val, nx, fan;
} e[mm];

void init()
{
	edge = 0;
	memset(fr, -1, sizeof fr);
}

void addedge(int u, int v, int w)
{
	edge++;
	e[edge].to = v, e[edge].val = w, e[edge].nx = fr[u], e[edge].fan = edge + 1;
	fr[u] = edge;
	edge++;
	e[edge].to = u, e[edge].val = 0, e[edge].nx = fr[v], e[edge].fan = edge - 1;
	fr[v] = edge;
}

void bfs(int s)
{
	memset(lv, 0, sizeof lv);
	lv[s] = 1;
	queue<int> q;
	q.push(s);
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		for (int i = fr[t]; i != -1; i = e[i].nx)
		{
			if (e[i].val > 0 && !lv[e[i].to])
			{
				lv[e[i].to] = lv[t] + 1;
				q.push(e[i].to);
			}
		}
	}
}

int dfs(int s, int t, int f)
{
	if (s == t)
		return f;
	for (int i = fr[s]; i != -1; i = e[i].nx)
	{
		if (e[i].val > 0 && lv[s] < lv[e[i].to])
		{
			int d = dfs(e[i].to, t, min(f, e[i].val));
			if (d > 0)
			{
				e[i].val -= d;
				e[e[i].fan].val += d;
				return d;
			}
		}
	}
	return 0;
}

int max_flow(int s, int t)
{
	int flow = 0;
	while (1)
	{
		bfs(s);
		if (!lv[t])
			break;

		int f = 0;
		while ((f = dfs(s, t, inf)) > 0)
			flow += f;
	}
	return flow;
}

int main()
{
	//freopen("D:\\in.txt", "r", stdin);
	int n, f, d;
	int st = 0, ed = 801;
	while (scanf("%d %d %d", &n, &f, &d) != EOF)
	{
		init();

		for (int i = 1; i <= n; i++)  // 顾客拆点
			addedge(200 + i, 400 + i, 1);

		for (int i = 1; i <= f; i++)
		{
			int x;
			scanf("%d", &x);
			addedge(st, i, x);
		}
		for (int i = 1; i <= d; i++)
		{
			int x;
			scanf("%d", &x);
			addedge(600 + i, ed, x);
		}

		for (int i = 1; i <= n; i++)
		{
			char ch[210];
			scanf("%s", ch);
			for (int j = 0; j < f; j++)
			{
				if (ch[j] == 'Y')
					addedge(j + 1, 200 + i, inf);
			}
		}
		for (int i = 1; i <= n; i++)
		{
			char ch[210];
			scanf("%s", ch);
			for (int j = 0; j < d; j++)
			{
				if (ch[j] == 'Y')
					addedge(400 + i, 600 + j + 1, inf);
			}
		}
		printf("%d\n", max_flow(st, ed));
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/ummmmm/article/details/81346625