您的位置:首页 > 理论基础 > 计算机网络

hdoj 4292 Food 【拆点 网络流】

2015-08-28 22:36 549 查看

Food

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3851 Accepted Submission(s): 1289



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种饮料,已经给出每种食物、饮料的数目。输入下面有N*F和N*D两个矩阵,

N*F矩阵的第i行第j列表示第i个人是否喜欢第j种食物,若该位置的元素为Y表示喜欢否则不喜欢。

[b]N*D矩阵的第i行第j列表示第i个人是否喜欢第j种饮料,若该位置的元素为Y表示喜欢否则不喜欢。

[/b]

每个人的需求是——选择一份食物和一份饮料(必须是他们喜欢的),问最多可以满足几个人的需求。



简单题,直接说下建图吧。

建图:超级源点source,超级汇点sink

1,source向每一种食物建边,容量为当前食物的数目;

2,每一种饮料向sink建边,容量为当前饮料的数目;

3,把每个人 i 拆成 左点i到右点i+N的一条边,容量为1,表示每个人只能选择一份食物和一份饮料。

4,每一种食物向喜欢它的人建边,食物所对应的点——>拆点后人的左点,容量为1;

5,每一个人向他喜欢的饮料建边,拆点后人的右点——>饮料所对应的点,容量为1;

最后跑一次最大流就ok了。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAXN 1000
#define MAXM 200000+10
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
    int from, to, cap, flow, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int dist[MAXN], cur[MAXN];
bool vis[MAXN];
int N, F, D;
int source, sink;//超级源点 超级汇点
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w)
{
    Edge E1 = {u, v, w, 0, head[u]};
    edge[edgenum] = E1;
    head[u] = edgenum++;
    Edge E2 = {v, u, 0, 0, head[v]};
    edge[edgenum] = E2;
    head[v] = edgenum++;
}
void getMap()
{
    int a;
    source = 0, sink = 2*N + F + D + 1;
    //人拆点后 编号从1到2*N
    for(int i = 1; i <= N; i++)//对人拆点
        addEdge(i, i+N, 1);//只能选一种食物和一种饮料
    //食物编号从2*N+1到2*N+F
    for(int i = 1; i <= F; i++)
    {
        scanf("%d", &a);
        addEdge(source, 2*N+i, a);//源点 到 食物建边
    }
    //饮料编号2*N+F+1到2*N+F+D
    for(int i = 1; i <= D; i++)
    {
        scanf("%d", &a);
        addEdge(2*N+F+i, sink, a);//饮料 到 汇点建边
    }
    char str[300];
    for(int i = 1; i <= N; i++)
    {
        scanf("%s", str);
        for(int j = 0; j < F; j++)
        {
            if(str[j] == 'Y')//第i个人 接受 第j+1种食物
                addEdge(2*N+j+1, i, 1);
        }
    }
    for(int i = 1; i <= N; i++)
    {
        scanf("%s", str);
        for(int j = 0; j < D; j++)
        {
            if(str[j] == 'Y')//第i个人 接受 第j+1种饮料
                addEdge(i+N, 2*N+F+j+1, 1);
        }
    }
}
bool BFS(int s, int t)
{
    queue<int> Q;
    memset(dist, -1, sizeof(dist));
    memset(vis, false, sizeof(vis));
    dist[s] = 0;
    vis[s] = true;
    Q.push(s);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            Edge E = edge[i];
            if(!vis[E.to] && E.cap > E.flow)
            {
                dist[E.to] = dist[u] + 1;
                if(E.to == t) return true;
                vis[E.to] = true;
                Q.push(E.to);
            }
        }
    }
    return false;
}
int DFS(int x, int a, int t)
{
    if(x == t || a == 0) return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next)
    {
        Edge &E = edge[i];
        if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap - E.flow), t)) > 0)
        {
            edge[i].flow += f;
            edge[i^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}
int Maxflow(int s, int t)
{
    int flow = 0;
    while(BFS(s, t))
    {
        memcpy(cur, head, sizeof(head));
        flow += DFS(s, INF, t);
    }
    return flow;
}
int main()
{
    while(scanf("%d%d%d", &N, &F, &D) != EOF)
    {
        init();
        getMap();
        printf("%d\n", Maxflow(source, sink));
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: