poj 1459 Power Network(最大流)

Power Network
Time Limit: 2000MS   Memory Limit: 32768K
Total Submissions: 23459   Accepted: 12284

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.

Source


看到此題時感覺頭大,感覺好亂的樣子,不知道,題目說的啥,靜下心來讀一下題目發現,基礎題目;
題意:給你n個節點,包括np個發電站,中轉站和nc個消費者;
其中,發電站的消耗c(u)爲0,消費者的p(U)爲0;中轉站的p(u)和c(U)都爲0;
在這n個點之間有m條電線連接u、v(可以都是電站或者消耗點)兩個站點,電線的最大容量爲lmax(u,v)。每個發電站的發電量爲p,耗電點的最大耗電量爲c.求消費者最大的消耗量;


思路:容易想到用網絡流,這算是多源多匯問題,根據白書上提到的建模方法,建立一個超級源,連接每個源(即發電廠),流量爲發電廠的發電量,並建立一個超級匯,將所有的消費者連接一條邊倒匯,流量爲消費者的消耗量,最後只要求s到tde最大流即可;

此外注意一下此題的輸入,空格可以隨便輸入,我用scanf(“%1s%d%1s”。。。);這種方式寫的,據說%1s能夠忽略空格,製表符回車等;
順便扯扯自己對於網絡流題目的認識:
1:一般一到網絡流的題目需要仔細讀題,然後建立模型(即構圖),構圖可以說是網絡流題目的關鍵了,除非有些題目卡時間比較緊還要注意算法優化問題。建立一個正確的模型,我感覺難度指數很高,需要多練;
本題套用了Dinic算法模板(遞歸求解、鄰接表儲存)
代碼:
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define N 110
#define inf 0x3f3f3f3f
using namespace std;
struct Edge
{
    int from,to,cap,flow;
};
struct Dinic
{
    int n,m,s,t;
    vector<Edge>edges;
    vector<int> G[N];
    bool vis[N];
    int d[N];
    int cur[N];
    bool bfs()
    {
        memset(vis,0,sizeof(vis));
        queue<int>Q;
        Q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!Q.empty())
        {
            int x=Q.front();
            Q.pop();
            for(int i=0; i<G[x].size(); i++)
            {
                Edge &e=edges[G[x][i]];
                if(!vis[e.to]&&e.cap>e.flow)
                {
                    vis[e.to]=1;
                    d[e.to]=d[x]+1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int dfs(int x,int a)
    {
        if(x==t||a==0)
            return a;
        int flow=0,f;
        for(int &i=cur[x]; i<G[x].size(); i++)
        {
            Edge &e=edges[G[x][i]];
            if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
            {
                e.flow+=f;
                edges[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(!a)
                    break;
            }
        }
        return flow;
    }
    int Maxflow(int s,int t)
    {
        this->s=s,this->t=t;
        int flow=0;
        while(bfs())
        {
            memset(cur,0,sizeof(cur));
            flow+=dfs(s,inf);
        }
        return flow;
    }
    void add_edge(int from,int to,int cap)
    {
        edges.push_back((Edge)
        {
            from,to,cap,0
        });
        edges.push_back((Edge)
        {
            to,from,0,0
        });
        int m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
};
int main()
{
    int n,np,nc,l,a,b,w,i;
    char c[10];
    while(~scanf("%d%d%d%d",&n,&np,&nc,&l))
    {
        Dinic D;
        D.s=n+1,D.t=n+2;
        for(i=1; i<=l; i++)
        {
            getchar();
            scanf("%1s%d,%d%1s%d",c,&a,&b,c,&w);
            D.add_edge(a,b,w);
        }
        for(i=0; i<np; i++)
        {
            getchar();
            scanf("%1s%d%1s%d",c,&a,c,&b);
            D.add_edge(D.s,a,b);
        }
        for(i=0; i<nc; i++)
        {
            getchar();
            scanf("%1s%d%1s%d",c,&a,c,&b);
            D.add_edge(a,D.t,b);
        }
        int flow=D.Maxflow(D.s,D.t);
        printf("%d\n",flow);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章