poj1459

Power Network

Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 16422 Accepted: 8712

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.


哎,纠结死了,我对网络流这方面理解的还不行

如果自己写代码的话还是有点难度,所以找个好的模版还是很重要的

额,模版也比较纠结,好多中算法

找了个比较简单的算法 Edmonds_karp 

时间复杂度为O(V*E^2)

Edmonds-Karp算法就是利用宽度优先不断地找一条从s到t的可改进路,然后改进流量,一直到找不到可改进路为止。

由于用宽度优先,每次找到的可改进路是最短的可改进路,通过分析可以知道其复杂度为O(VE^2)。

代码好丑
  1#include<stdio.h>
  2#include<string.h>
  3#include<math.h>
  4#define MAX 105
  5int map[MAX][MAX],flow[MAX][MAX],c[MAX][MAX];
  6int n,nc,np,nt,s,t;
  7int sum;
  8int min(int a,int b)
  9{
 10    if (a<b) return a;else return b;
 11}

 12void Edmonds_Karp()
 13{
 14    int l1[MAX],l2[MAX],q[MAX];
 15    int u,v,head,tail;
 16    do 
 17    {
 18        memset(l1,0,sizeof(l1));
 19        memset(l2,0,sizeof(l2));//初始化所有标号为0
 20        l1[s]=0;l2[s]=0x7fffffff;
 21        head=0;tail=1;
 22        q[tail]=s;
 23        while (head<tail&&l2[t]==0)//q未空且汇点未标号
 24        {
 25            head++;
 26            u=q[head];
 27            for (v=1;v<=n ;v++ )
 28            {
 29                if (flow[u][v]<c[u][v]&&l2[v]==0)//未标号且有可行流
 30                {
 31                    tail++;
 32                    q[tail]=v;
 33                    l2[v]=min(c[u][v]-flow[u][v],l2[u]);
 34                    //l2[v]记录s到v增广路中最小的可改进流
 35                    l1[v]=u;//记录前驱
 36                }

 37            }

 38        }

 39        if (l2[t]>0)//汇点未标号
 40        {
 41            v=t;
 42            u=l1[v];
 43            while (v!=s)
 44            {
 45                flow[u][v]+=l2[t];
 46                flow[v][u]=-flow[u][v];
 47                v=u;
 48                u=l1[v];
 49            }

 50        }

 51    }

 52    while (l2[t]!=0);//直到汇点未标号
 53}

 54void init()
 55{
 56    int i,j,a,b,w,x;
 57    char ch1;
 58    s=1;t=n+2;
 59    memset(map,0,sizeof(map));
 60    for (i=1;i<=nt ;i++ )
 61    {
 62        scanf("%c",&ch1);
 63        while (ch1!='(')
 64        {
 65            scanf("%c",&ch1);
 66        }

 67        scanf("%d",&a);a=a+2;
 68        scanf("%c",&ch1);scanf("%d",&b);b=b+2;
 69        scanf("%c",&ch1);scanf("%d",&w);
 70        map[a][b]=w;
 71    }

 72    for (i=1;i<=np ;i++ )
 73    {
 74        scanf("%c",&ch1);
 75        while (ch1!='(')
 76        {
 77            scanf("%c",&ch1);
 78        }

 79        scanf("%d",&a);a=a+2;
 80        scanf("%c",&ch1);scanf("%d",&w);
 81        map[s][a]=w;//map[a][s]=-w;
 82    }

 83    for (i=1;i<=nc ;i++ )
 84    {
 85        scanf("%c",&ch1);
 86        while (ch1!='(')
 87        {
 88            scanf("%c",&ch1);
 89        }

 90        scanf("%d",&a);a=a+2;
 91        scanf("%c",&ch1);scanf("%d",&w);
 92        map[a][t]=w;//map[t][a]=-w;
 93    }

 94    n=n+2;
 95    /*/for (i=1;i<=n ;i++ )
 96    {
 97        for (j=1;j<=n;j++ )
 98        {
 99            printf("%d ",map[i][j]);
100        }
101        printf("\n");
102    }*/

103    for (i=1;i<=n ;i++ )
104    {
105        for (j=1;j<=n;j++ )
106        {
107            c[i][j]=map[i][j];
108        }

109    }

110}

111int main()
112{
113    int i;
114    while (scanf("%d%d%d%d",&n,&np,&nc,&nt)!=EOF)
115    {
116        memset(flow,0,sizeof(flow));
117        init();
118        Edmonds_Karp(1,n);
119        sum=0;
120        for (i=1;i<=n;i++ )
121        {
122            sum+=flow[1][i];
123        }

124        printf("%d\n",sum);
125    }

126    return 0;
127}

128



posted on 2012-02-23 17:34 jh818012 阅读(122) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

常用链接

留言簿

文章档案(85)

搜索

最新评论