Power Network

来源:互联网 发布:网络机柜检验报告 编辑:程序博客网 时间:2024/06/14 01:21

链接:

  http://poj.org/problem?id=1459


题目:

Description

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) <= pmax(u) of power, may consume an amount 0 <= c(u) <= min(s(u),cmax(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) <= lmax(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 pmax(u)=y. The label x/y of consumer u shows that c(u)=x and cmax(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and lmax(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.

Input

There are several data sets in the input. Each data set encodes a power network. It starts with four integers: 0 <= n <= 100 (nodes), 0 <= np <= n (power stations), 0 <= nc <= n (consumers), and 0 <= m <= n^2 (power transport lines). Follow m data triplets (u,v)z, where u and v are node identifiers (starting from 0) and 0 <= z <= 1000 is the value of lmax(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of pmax(u). The data set ends with nc doublets (u)z, where u is the identifier of a consumer and 0 <= z <= 10000 is the value of cmax(u). All input numbers are integers. Except the (u,v)z triplets and the (u)z doublets, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct.
Output

For each data set from the input, the program prints on the standard output the maximum amount of power that can be consumed in the corresponding network. Each result has an integral value and is printed from the beginning of a separate line.

Sample Input

2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7
(3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5
(0)5 (1)2 (3)2 (4)1 (5)4

Sample Output

15
6

Hint

The sample input contains two data sets. The first data set encodes a network with 2 nodes, power station 0 with pmax(0)=15 and consumer 1 with cmax(1)=20, and 2 power transport lines with lmax(0,1)=20 and lmax(1,0)=10. The maximum value of Con is 15. The second data set encodes the network from figure 1.


题意:

  前4个数据就是给你n个点,其中有np个是源点,nc个是汇点,剩下的点(n-np-nc)是中转点,点与点之间有边存在的,有m条边,每条边有最大可通过流量。

  接下来题目先给出的是m条线路的数据,然后是np个源点的数据,接着就是nc个汇点的数据,题目要我们求出给定的图中汇点最多能收到的总流量(就是求最大流)。


思路:

  由于这题有多个源点和汇点,我们可以增加两个点,一个总源点和一个总汇点。

  把所有的源点都当成是由总源点提供的源,所有的汇点都将流转移到总汇点上,这样就相当于转换成一个基本的网络流求最大流的题。

  总源点与每个源点之间都建一条边,边的值为源点最大提供源,每个汇点与总汇点有一条边,边的值为汇点最大接收流。

  然后跑最基础的网络流。


实现:

#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <queue>void read(int &x){ x=0;char c=getchar();while(c<'0' || c>'9')c=getchar();while(c>='0' && c<='9'){ x=x*10+c-'0';c=getchar(); }}void write(int x){ int y=10,len=1;while(y<=x)   {y*=10;len++;}while(len--){y/=10;putchar(x/y+48);x%=y;}}using namespace std;const int INF = 0x3f3f3f3f, maxn = 157;int N, NP, NC, M;struct E {    int u, v, flow;    E(int u = 0, int v = 0, int flow = 0): u(u), v(v), flow(flow) {}} edg[maxn * maxn];int cnt_edg, S, T;vector<int> edge[maxn]; // 边集int dis[maxn];//距源点距离,分层图int current[maxn];//当前弧void addedg(int u, int v, int flow) {    edge[u].push_back(cnt_edg);    edg[cnt_edg++] = E(u, v, flow); // 正向边    edge[v].push_back(cnt_edg);    edg[cnt_edg++] = E(v, u, 0); // 反向边容量为0    // 正向边下标通过异或就得到反向边下标, 2 ^ 1 == 3 ; 3 ^ 1 == 2}bool bfs() {    queue<int> q;    q.push(S);    memset(dis, -1, sizeof(dis));    dis[S] = 0;    while (!q.empty()) {        int index = q.front();        q.pop();        int sz = int(edge[index].size());        for (int i = 0; i < sz; i++) {            E &e = edg[edge[index][i]];            if (e.flow > 0) {                if (dis[e.v] < 0) {                    dis[e.v] = dis[index] + 1;                    q.push(e.v);                }            }        }    }    return bool(~dis[T]); // 返回是否能够到达汇点}int dfs(int index, int maxflow) {    if (index == T)        return maxflow;    // i = current[index] 当前弧优化    int sz = int(edge[index].size());    for (int i = current[index], number; number = edge[index][i], i < sz; i++) {        current[index] = i;        E &e = edg[number];        if (dis[e.v] == dis[index] + 1 && e.flow > 0) {            int flow = dfs(e.v, min(maxflow, e.flow));            if (flow != 0) {                e.flow -= flow; // 正向边流量降低                edg[number ^ 1].flow += flow; // 反向边流量增加                return flow;            }        }    }    return 0; // 找不到增广路 退出}int dinic() {    int ans = 0;    while (bfs()) {// 建立分层图        int flow;        memset(current, 0, sizeof(current)); // BFS后应当清空当前弧数组        while (bool(flow = dfs(S, INF))) // 一次BFS可以进行多次增广            ans += flow;    }    return ans;}int main(){#ifndef ONLINE_JUDGE    freopen("in.txt","r",stdin);#endif    while (cin >> N >> NP >> NC >> M) {        cnt_edg = 0, S = N, T = N + 1;        for (int i = 0; i <= T; i++) edge[i].clear();        for (int i = 0, u, v, flow ; i < M; i++) {            read(u),read(v),read(flow);            addedg(u, v, flow);        }        for (int i = 0, u, p; i < NP; i++) {            read(u),read(p);            addedg(S, u, p);        }        for (int i = 0, u, c; i < NC; i++) {            read(u),read(c);            addedg(u, T, c);        }        write(dinic());        cout << endl;    }    return 0;}
原创粉丝点击