POJ-1459-PWOERネットワーク(最大流量Dinic、妖精入力)

リンク:

https://vjudge.net/problem/POJ-1459

質問の意味:

電力網は、電力輸送ラインによって接続されたノード(発電所、消費者およびディスパッチャ)から成ります。uは量s(U)を供給することができるノードは> =電力の0が、電力量0 <= P(U)<= P maxの(U)を生成することができる、0 <= Cの(U量を消費します)<=分(S(U)のC max(U))パワーの、及び(U)= sの(U)+ P(U)-C(U)パワーの量dを送達することができます。任意の消費者のための任意の発電所、P(U)= 0 C(U)= 0、および任意のディスパッチャのP(U)= Cの(U)= 0:以下の制限が適用されます。ノードUからネット内のノードvに対して最大1つの電力輸送ライン(V uが、)があります。それは量0 <= Lを搬送(U、V)<=リットル最大(U、V)Vにuで供給される電力の。ネットで消費コン=ΣUC(U)も電源をしてみましょう。問題は、コンの最大値を計算することです。

例では、図1にuがP(U)= xおよびP maxは(u)はYを=ことを示している発電所のラベルX / Yです。消費者のラベルX / Yは、Uは、C(U)= xおよびC maxが(u)はYを=ことを示しています。電力搬送ライン(U、V)のラベルのx / yがL(U、V)= xおよびL maxの(u、v)はYを=ことを示しています。消費される電力は、コン= 6です。そこにネットワークの他の可能な状態であるが、コンの値が6を超えることができないことに注意してください。

アイデア:

質問の意味は、それが...愚か入力することで、非常に明確である
ダイレクトDinicの構成図を完成します。

コード:

#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>

#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e3+10;
const int INF = 1e9;

struct Edge
{
    int from, to, flow, cap;
};

vector<int> G[MAXN];
vector<Edge> edges;
int Pow[MAXN], Con[MAXN];
int Vis[MAXN], Dis[MAXN];
int n, np, nc, m;
int s, t;

void AddEdge(int from, int to, int cap)
{
    edges.push_back(Edge{from, to, 0, cap});
    edges.push_back(Edge{to, from, 0, 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)
            {
                que.push(e.to);
                Dis[e.to] = Dis[u]+1;
            }
        }
    }
    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));
            flow -= tmp;
            e.cap -= 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()
{
    while (~scanf("%d%d%d%d", &n, &np, &nc, &m))
    {
        int u, v, x;
        s = 0, t = n+1;
        for (int i = s;i <= t;i++)
            G[i].clear();
        edges.clear();
//        cout << n << ' ' << np << ' ' << nc << ' ' << m << endl;
        getchar();
        for (int i = 1;i <= m;i++)
        {
            scanf(" (%d,%d)%d", &u, &v, &x);
//            cout << u << ' ' << v << ' ' << x << endl;
            u++, v++;
            AddEdge(u, v, x);
            getchar();
        }
        for (int i = 1;i <= np;i++)
        {
            scanf(" (%d)%d", &u, &x);
            u++;
            AddEdge(0, u, x);
            getchar();
        }
        for (int i = 1;i <= nc;i++)
        {
            scanf(" (%d)%d", &u, &x);
            u++;
            AddEdge(u, t, x);
        }
        int res = MaxFlow();
        cout << res << endl;
    }

    return 0;
}

おすすめ

転載: www.cnblogs.com/YDDDD/p/11324760.html