zoukankan      html  css  js  c++  java
  • poj 1459 Power Network 最大流

    Power Network
    Time Limit: 2000MS   Memory Limit: 32768K
    Total Submissions: 19147   Accepted: 10099

    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

     
    解题思路: 建立一个源点S,一个汇点T . 源点S与所有电站连边,容量为电站Pmax. 所有消费者与汇点T连边,容量为Cmax. 源点S到汇点T的最大流即为所有答案.
     
    (一般预流推进算法)Preflow Push Mathed     时间复杂度O(n^2*m)
    算法过程:
      ps:每个节点有一个属性( 盈余, 高度标号 )
      1.  构建残留网络,源点S盈余为无穷大,高度标号为顶点个数N, 汇点T盈余0,高度标号为0. 对源点S出发的每条<S,u>,令F(S,u) = C(S,u);
    对任意的定点v 属于 V, 计算精确的距离标号d(v); 令d(S) = N;      { ps: 初始时,这里其实可以除源点外所有顶点距离标号为0 }
      2.  如果残留网络 G( V, E ) 中不存在活跃定顶点, 则算法结束, 已经求得最大流, 否则进入 3 .
      3.  在残留网络中选取活跃顶点 u; 如果存在定点u的某条出弧<u,v>为允许弧,则将min{ e[u], c(u,v) 流量的流从顶点u推进到顶点v;
    否则令d(u) = min{ d(v)+1 | <u,v> 属于 E, 且 c(u,v) > 0 }. 转第2步. 
    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 #include<queue>
     5 #include<algorithm>
     6 
     7 using namespace std;
     8 #define MIN(a,b) (a)<(b)?(a):(b)
     9 #define MAX(a,b) (a)>(b)?(a):(b)
    10 const int N = 110;
    11 const int inf = 0x3fffffff;
    12 
    13 int n, np, nc, m; //顶点个数,电站个数,消费者个数,边数
    14 int remain[N][N]; //残留网络
    15 int ef[N], h[N];  //盈余,高度标号
    16 int s, t, V;      //源点,汇点,总顶点数量
    17 bool used[N];
    18 queue<int> Q;
    19 
    20 void CreateGraph(){
    21     int u, v, z;
    22     s = n+1; t = n+2; V = n+2; //定点编号从1开始
    23     memset( remain, 0, sizeof(remain));
    24     for(int i = 0; i < m; i++)
    25     {
    26         while( getchar() != '(' );
    27         scanf("%d,%d)%d",&u,&v,&z);
    28         remain[u+1][v+1] = z;
    29     }
    30     for(int i = 0; i < np; i++)
    31     {
    32         while( getchar() != '(' );
    33         scanf("%d)%d",&u,&z);
    34         remain[s][u+1] = z;
    35     }
    36     for(int i = 0; i < nc; i++)
    37     {
    38         while( getchar() != '(' );
    39         scanf("%d)%d",&u,&z);
    40         remain[u+1][t] = z;
    41     }
    42 }
    43 
    44 void push_relabel(){
    45     int maxflow = 0;
    46     memset( used, 0, sizeof(used));    
    47     memset( ef, 0, sizeof(ef));
    48     memset( h, 0, sizeof(h));
    49     ef[s] = inf;h[s] = V; //初始源点盈余为无穷大,高度标号为V 
    50     while( !Q.empty() ) Q.pop();
    51     Q.push( s ); used[s] = true;
    52     while( !Q.empty() )
    53     {
    54         int u = Q.front(); Q.pop(); used[u] = false;
    55         for(int v = 1; v <= V; v++)
    56         {
    57             int p = MIN( ef[u], remain[u][v] );
    58             if( p > 0 && (u == s || h[u] == h[v]+1) )
    59             { //初始时源点向其相邻节点推进时,不要求低一层限定
    60                 remain[u][v] -= p; remain[v][u] += p; //更新残留网络
    61                 if( v == t ) maxflow += p;
    62                 ef[u] -= p; ef[v] += p; //更新节点盈余情况
    63                 if( !used[v] && v != s && v != t ) //源点,汇点不应再作为活跃定点了
    64                     used[v] = true, Q.push(v);
    65             }
    66         }
    67         if( u != s && u != t && ef[u] > 0 )
    68         { //当U点盈余大于0时,无法向其他点推进时,增加其高度标号
    69             h[u]++;
    70             used[u] = true;
    71             Q.push( u );
    72         }    
    73     }
    74     printf("%d\n", maxflow );
    75 }
    76 
    77 int main()
    78 {
    79     while( scanf("%d%d%d%d", &n,&np,&nc,&m) != EOF)
    80     {
    81         CreateGraph();
    82         push_relabel();
    83     }
    84     return 0;
    85 }

    (最高标号预流推进算法)Highest-Label Proflow-Push Algorithm    时间复杂度 O(n^2*m^(1/2) )  

    最高标号预流推进算法的思想是从具有最大距离标号的活跃节点开始预流推进。直观想法是:    此题跑了 110ms

    使得距离标号较小的活跃定点累积尽可能多地来自距离标号大的活跃顶点的流量,然后对累积的盈余进行推进,可能会减少非饱和推进的次数。

    View Code
      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<stdlib.h>
      4 #include<queue>
      5 #include<algorithm>
      6 
      7 using namespace std;
      8 #define MIN(a,b) (a)<(b)?(a):(b)
      9 #define MAX(a,b) (a)>(b)?(a):(b)
     10 const int N = 110;
     11 const int inf = 0x3fffffff;
     12 
     13 int n, np, nc, m; //顶点个数,电站个数,消费者个数,边数
     14 int remain[N][N]; //残留网络
     15 int ef[N], h[N];  //盈余,高度标号
     16 int s, t, V;      //源点,汇点,总顶点数量
     17 bool used[N];
     18 
     19 struct node{
     20     int x;
     21     bool operator < (node tmp) const
     22     {
     23         return h[x] > h[tmp.x];    
     24     }
     25 }info, nxt;
     26 
     27 
     28 priority_queue<node> Q;
     29 
     30 void CreateGraph(){
     31     int u, v, z;
     32     s = n+1; t = n+2; V = n+2; //定点编号从1开始
     33     memset( remain, 0, sizeof(remain));
     34     for(int i = 0; i < m; i++)
     35     {
     36         while( getchar() != '(' );
     37         scanf("%d,%d)%d",&u,&v,&z);
     38         remain[u+1][v+1] = z;
     39     }
     40     for(int i = 0; i < np; i++)
     41     {
     42         while( getchar() != '(' );
     43         scanf("%d)%d",&u,&z);
     44         remain[s][u+1] = z;
     45     }
     46     for(int i = 0; i < nc; i++)
     47     {
     48         while( getchar() != '(' );
     49         scanf("%d)%d",&u,&z);
     50         remain[u+1][t] = z;
     51     }
     52 }
     53 
     54 void push_relabel(){
     55     int maxflow = 0;
     56     memset( used, 0, sizeof(used));    
     57     memset( ef, 0, sizeof(ef));
     58     memset( h, 0, sizeof(h));
     59     ef[s] = inf;h[s] = V; //初始源点盈余为无穷大,高度标号为V 
     60     while( !Q.empty() ) Q.pop();
     61     info.x = s;
     62     Q.push( info ); used[s] = true;
     63     while( !Q.empty() )
     64     {
     65         int u = (Q.top()).x; Q.pop(); used[u] = false;
     66         for(int v = 1; v <= V; v++)
     67         {
     68             int p = MIN( ef[u], remain[u][v] );
     69             if( p > 0 && (u == s || h[u] == h[v]+1) )
     70             { //初始时源点向其相邻节点推进时,不要求低一层限定
     71                 remain[u][v] -= p; remain[v][u] += p; //更新残留网络
     72                 if( v == t ) maxflow += p;
     73                 ef[u] -= p; ef[v] += p; //更新节点盈余情况
     74                 if( !used[v] && v != s && v != t ) //源点,汇点不应再作为活跃定点了
     75                 {
     76                     used[v] = true; 
     77                     nxt.x = v;    Q.push(nxt);
     78                 }
     79             }    
     80         }
     81         if( u != s && u != t && ef[u] > 0 )
     82         { //当U点盈余大于0时,无法向其他点推进时,增加其高度标号
     83             h[u]++;
     84             used[u] = true;
     85             nxt.x = u;    
     86             Q.push( nxt );
     87         }    
     88     }
     89     printf("%d\n", maxflow );
     90 }
     91 
     92 int main()
     93 {
     94     while( scanf("%d%d%d%d", &n,&np,&nc,&m) != EOF)
     95     {
     96         CreateGraph();
     97         push_relabel();
     98     }
     99     return 0;
    100 }

     (连续最短增广路算法) Dinic Successive Shortest AugMenting path Algorithm  时间复杂度 O( n^2*m)

    每次使用BFS构建层次网络,然后使用DFS连续求增广路   这题跑了660ms  

    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 #include<iostream>
     5 #include<queue>
     6 #include<algorithm>
     7 using namespace std;
     8 #define MAX(a,b) (a)>(b)?(a):(b)
     9 #define MIN(a,b) (a)<(b)?(a):(b)
    10 const int N = 110;
    11 const int inf = 0x3fffffff;
    12 int n, np, nc, m, V;
    13 int s, t;
    14 int remain[N][N];
    15 bool vis[N], sign[N][N];
    16 
    17 queue<int>Q;
    18 
    19 void CreateGraph(){
    20     int u, v, z;
    21     s = 0; t = n+1; V = n+2; //顶点编号为(1,n) 源点为0,汇点为n+1
    22     memset( remain, 0, sizeof(remain));
    23     for(int i = 0; i < m; i++)
    24     {
    25         while( getchar() != '(' );
    26         scanf("%d,%d)%d", &u,&v,&z);
    27         remain[u+1][v+1] = z;
    28     }
    29     for(int i = 0; i < np; i++)
    30     {
    31         while( getchar() != '(' );
    32         scanf("%d)%d", &u, &z);
    33         remain[s][u+1] = z; //源点到电站流量
    34     }
    35     for(int i = 0; i < nc; i++)
    36     {
    37         while( getchar() != '(');
    38         scanf("%d)%d",&u,&z);
    39         remain[u+1][t] = z; //消费者到汇点流量
    40     }
    41 }
    42 bool BFS()
    43 {
    44     memset( sign, 0, sizeof(sign));
    45     memset( vis, 0, sizeof(vis));
    46     while( !Q.empty() ) Q.pop();
    47     Q.push( s ); vis[s] = true;
    48     while( !Q.empty() && !vis[t] )
    49     {
    50         int u = Q.front(); Q.pop();    
    51         for(int v = 0; v < V; v++)
    52         {
    53             if( remain[u][v] && !vis[v] )
    54             {
    55                 sign[u][v] = true;  //构建层次网络    
    56                 vis[v] = true;
    57                 Q.push(v);        
    58             }
    59         }
    60     }
    61     return vis[t];
    62 }
    63 int DFS( int u , int flow )
    64 {
    65     int sum = 0;
    66     if( u == t ) return flow;
    67     for(int v = 0; v < V; v++)
    68     {
    69         if( sign[u][v]  ){ //在层次网络中
    70              int    p = DFS( v, MIN( flow, remain[u][v] ) );    
    71             remain[u][v] -= p;    remain[v][u] += p; //此为残留网络
    72             sum += p;    
    73         }    
    74     }
    75     return sum;
    76 }
    77 void Denic(){
    78     int maxflow = 0;
    79     while( BFS() )
    80         maxflow += DFS( s , inf ); //源点出发流量为无穷大
    81     printf("%d\n", maxflow);
    82 }
    83 
    84 int main(){
    85     while( scanf("%d%d%d%d", &n,&np,&nc,&m) != EOF)
    86     {
    87         CreateGraph();
    88         Denic();
    89     }
    90     return 0;
    91 }
  • 相关阅读:
    Python数据结构之列表
    前端html表单与css样式
    Http协议基本知识
    PHP-CGI远程任意代码执行漏洞(CVE-2012-1823)修复方案
    云计算定义
    编译最新linux内核(version 4.4.2)
    nginx负载均衡
    Json转换利器Gson之实例一-简单对象转化和带泛型的List转化
    WebService--使用 CXF 开发 REST 服务
    WebService- 使用 CXF 开发 SOAP 服务
  • 原文地址:https://www.cnblogs.com/yefeng1627/p/2810138.html
Copyright © 2011-2022 走看看