POJ-1459-Pwoer Network(最大流Dinic, 神仙输入)

链接:

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

题意:

A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0 <= p(u) <= p max(u) of power, may consume an amount 0 <= c(u) <= min(s(u),c max(u)) of power, and may deliver an amount d(u)=s(u)+p(u)-c(u) of power. The following restrictions apply: c(u)=0 for any power station, p(u)=0 for any consumer, and p(u)=c(u)=0 for any dispatcher. There is at most one power transport line (u,v) from a node u to a node v in the net; it transports an amount 0 <= l(u,v) <= l max(u,v) of power delivered by u to v. Let Con=Σ uc(u) be the power consumed in the net. The problem is to compute the maximum value of Con.

An example is in figure 1. The label x/y of power station u shows that p(u)=x and p max(u)=y. The label x/y of consumer u shows that c(u)=x and c max(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and l max(u,v)=y. The power consumed is Con=6. Notice that there are other possible states of the network but the value of Con cannot exceed 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