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;
}

原文地址:https://www.cnblogs.com/YDDDD/p/11324760.html

时间: 2024-08-03 01:09:12

POJ-1459-Pwoer Network(最大流Dinic, 神仙输入)的相关文章

POJ 1459 Power Network 最大流

建模不难,就读入有点麻烦,无脑拍完dinic 1A happy- #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #i

poj 1459 Power Network, 最大流,多源多汇

点击打开链接 多源多汇最大流,虚拟一个源点s'和一个汇点t',原来的源点.汇点向它们连边. #include<cstdiO> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> using namespace std; const int maxn = 500 + 5; const int INF = 100

POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

POJ 1459 Power Network(ISAP 裸最大流)

题目链接:http://poj.org/problem?id=1459 注意输入格式就行,还是ISAP #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; const int maxn = 300; const int ma

poj 1459 Power Network (dinic)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23059   Accepted: 12072 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

POJ 1459 Power Network 经典网络流构图问题 最大流,EK算法

题目链接:POJ 1459 Power Network Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23347   Accepted: 12231 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport line

初涉网络流 POJ 1459 Power Network

怒搞一下午网络流,又去我一块心病. 从2F到SAP再到Dinic终于过掉了.可是书上说Dinic的时间复杂度为v*v*e.感觉也应该超时的啊,可是过掉了,好诡异. 后两种算法都是在第一种的基础上进行优化.第一种方法就是不停的寻找增广路,后两种引进了层次网络的概念,第三种又改善了寻找增广路的方法. 现在只能理解到这里了... #include <algorithm> #include <iostream> #include <cstring> #include <c

POJ 1459 Power Network

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 27831   Accepted: 14469 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

POJ 1459 Power Network (网络流最大流基础 多源点多汇点 Edmonds_Karp算法)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24056   Accepted: 12564 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied