H - Food HDU - 4292 网络流

 
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




题目大意:
这个就是一个牛吃草问题





#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <cstring>
#include <string>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + 5;
const int INF = 0x3f3f3f3f;
struct edge
{
    int u, v, c, f;
    edge(int u, int v, int c, int f) :u(u), v(v), c(c), f(f) {}
};
vector<edge>e;
vector<int>G[maxn];
int level[maxn];//BFS分层,表示每个点的层数
int iter[maxn];//当前弧优化
int m, s, t;
void init(int n)
{
    for (int i = 0; i <= n; i++)G[i].clear();
    e.clear();
}
void add(int u, int v, int c)
{
    e.push_back(edge(u, v, c, 0));
    e.push_back(edge(v, u, 0, 0));
    m = e.size();
    G[u].push_back(m - 2);
    G[v].push_back(m - 1);
}
void BFS(int s)//预处理出level数组
//直接BFS到每个点
{
    memset(level, -1, sizeof(level));
    queue<int>q;
    level[s] = 0;
    q.push(s);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int v = 0; v < G[u].size(); v++)
        {
            edge& now = e[G[u][v]];
            if (now.c > now.f && level[now.v] < 0)
            {
                level[now.v] = level[u] + 1;
                q.push(now.v);
            }
        }
    }
}
int dfs(int u, int t, int f)//DFS寻找增广路
{
    if (u == t)return f;//已经到达源点,返回流量f
    for (int &v = iter[u]; v < G[u].size(); v++)
        //这里用iter数组表示每个点目前的弧,这是为了防止在一次寻找增广路的时候,对一些边多次遍历
        //在每次找增广路的时候,数组要清空
    {
        edge &now = e[G[u][v]];
        if (now.c - now.f > 0 && level[u] < level[now.v])
            //now.c - now.f > 0表示这条路还未满
            //level[u] < level[now.v]表示这条路是最短路,一定到达下一层,这就是Dinic算法的思想
        {
            int d = dfs(now.v, t, min(f, now.c - now.f));
            if (d > 0)
            {
                now.f += d;//正向边流量加d
                e[G[u][v] ^ 1].f -= d;
                //反向边减d,此处在存储边的时候两条反向边可以通过^操作直接找到
                return d;
            }
        }
    }
    return 0;
}
int Maxflow(int s, int t)
{
    int flow = 0;
    for (;;)
    {
        BFS(s);
        if (level[t] < 0)return flow;//残余网络中到达不了t,增广路不存在
        memset(iter, 0, sizeof(iter));//清空当前弧数组
        int f;//记录增广路的可增加的流量
        while ((f = dfs(s, t, INF)) > 0)
        {
            flow += f;
        }
    }
    return flow;
}

int main()
{
    int n, fo, di;
    while(scanf("%d%d%d", &n, &fo, &di)!=EOF)
    {
        init(2*n+fo+di+1);
        s = 0, t = 2 * n + fo + di + 1;
        for (int i = 1; i <= fo; i++)
        {
            int num;
            scanf("%d", &num);
            add(s, i, num);
        }
        for (int i = 1; i <= di; i++)
        {
            int num;
            scanf("%d", &num);
            add(i + fo + 2 * n, t, num);
        }
        for (int i = 1; i <= n; i++)
        {
            add(i + fo, i + n + fo, 1);
        }
        for (int i = 1; i <= n; i++)
        {
            char qw[500];
            scanf("%s", qw);
            for (int j = 0; j < fo; j++)
            {
                if (qw[j] == 'Y') add(j + 1, i + fo, 1);
            }
        }
        for (int i = 1; i <= n; i++)
        {
            char qw[500];
            scanf("%s", qw);
            for (int j = 0; j < di; j++)
            {
                if (qw[j] == 'Y') add(i + fo + n, j + fo + 2 * n + 1, 1);
            }
        }
        int ans = Maxflow(s, t);
        printf("%d\n", ans);
    }
    return 0;
}




猜你喜欢

转载自www.cnblogs.com/EchoZQN/p/10800423.html