ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HDU-4292-Food(最大流,Dinic)

2019-08-09 21:57:57  阅读:400  来源: 互联网

标签:HDU Food res cap int que Dinic include Dis


链接:

https://vjudge.net/problem/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.

思路:

最大流,建图,模板题.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;

const int MAXN = 200+10;
const int INF = 1e9;

struct Edge
{
    int from, to, cap;
};
vector<int> G[MAXN*4];
vector<Edge> edges;
int Dis[MAXN*4];
int Fo, Dr;
int n, f, d, s, t;

void Init()
{
    for (int i = s;i <= t;i++)
        G[i].clear();
    edges.clear();
}

void AddEdge(int from, int to, int cap)
{
    edges.push_back(Edge{from, to, cap});
    edges.push_back(Edge{to, from, 0});
    G[from].push_back(edges.size()-2);
    G[to].push_back(edges.size()-1);
}

bool Bfs()
{
    memset(Dis, -1, sizeof(Dis));
    queue<int> que;
    que.push(s);
    Dis[s] = 0;
    while (!que.empty())
    {
        int u = que.front();
        que.pop();
        for (int i = 0;i < G[u].size();i++)
        {
            Edge &e = edges[G[u][i]];
            if (e.cap > 0 && Dis[e.to] == -1)
            {
                Dis[e.to] = Dis[u]+1;
                que.push(e.to);
            }
        }
    }
    return Dis[t] != -1;
}

int Dfs(int u, int flow)
{
    if (u == t)
        return flow;
    int res = 0;
    for (int i = 0;i < G[u].size();i++)
    {
        Edge &e = edges[G[u][i]];
        if (e.cap > 0 && Dis[u]+1 == Dis[e.to])
        {
            int tmp = Dfs(e.to, min(flow, e.cap));
            e.cap -= tmp;
            flow -= tmp;
            res += tmp;
            edges[G[u][i]^1].cap += tmp;
            if (flow == 0)
                break;
        }
    }
    if (res == 0)
        Dis[u] = -1;
    return res;
}

int MaxFlow()
{
    int res = 0;
    while (Bfs())
        res += Dfs(s, INF);
    return res;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    while (cin >> n >> f >> d)
    {
        s = 0, t = n*2+f+d+1;
        Init();
        for (int i = 1;i <= f;i++)
        {
            cin >> Fo;
            AddEdge(0, 2*n+i, Fo);
        }
        for (int i = 1;i <= d;i++)
        {
            cin >> Dr;
            AddEdge(2*n+f+i, t, Dr);
        }
        for (int i = 1;i <= n;i++)
            AddEdge(i*2-1, i*2, 1);
        char ok;
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= f;j++)
            {
                cin >> ok;
                if (ok == 'Y')
                    AddEdge(2*n+j, 2*i-1, 1);
            }
        }
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= d;j++)
            {
                cin >> ok;
                if (ok == 'Y')
                    AddEdge(2*i, 2*n+f+j, 1);
            }
        }
        int res = MaxFlow();
        cout << res << endl;
    }

    return 0;
}

标签:HDU,Food,res,cap,int,que,Dinic,include,Dis
来源: https://www.cnblogs.com/YDDDD/p/11329754.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有