Power Network

Time Limit: 2000MS Memory Limit: 32768K

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

Southeastern Europe 2003

一眼题,就是多源多汇的模板题,我们只需要选出来一个超级源点和一个超级汇点分别与其它的源点和汇点连边就行了。建完图之后直接跑最大流。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define N 300
#define M 100005
using namespace std;
inline int read(){
    int ans=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
    return ans;
}
int n,m,ns,nt,s,t,first[N],d[N],cnt;
struct edge{int v,c,next;}e[M];
inline void add(int u,int v,int c){
    e[++cnt].v=v;
    e[cnt].c=c;
    e[cnt].next=first[u];
    first[u]=cnt;
    e[++cnt].v=u;
    e[cnt].c=0;
    e[cnt].next=first[v];
    first[v]=cnt;
}
inline bool bfs(){
    queue<int>q;
    memset(d,-1,sizeof(d));
    d[s]=0,q.push(s);
    while(!q.empty()){
        int x=q.front();
        q.pop();
        for(int i=first[x];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(d[v]!=-1||e[i].c<=0)continue;
            d[v]=d[x]+1;
            if(v==t)return true;
            q.push(v);
        }
    }
    return false;
}
inline int dfs(int x,int f){
    if(x==t||!f)return f;
    int flow=f;
    for(int i=first[x];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(e[i].c>0&&d[v]==d[x]+1&&flow){
            int tmp=dfs(v,min(flow,e[i].c));
            if(!tmp)d[v]=-1;
            e[i].c-=tmp;
            e[i^1].c+=tmp;
            flow-=tmp;
        }
    }
    return f-flow;
}
int main(){
    while(scanf("%d%d%d%d",&n,&ns,&nt,&m)!=EOF){
        s=0,t=n+1,cnt=-1;
        memset(first,-1,sizeof(first));
        for(int i=1;i<=m;++i){
            int u=read()+1,v=read()+1,c=read();
            add(u,v,c);
        }
        for(int i=1;i<=ns;++i){
            int v=read()+1,c=read();
            add(s,v,c);
        }
        for(int i=1;i<=nt;++i){
            int u=read()+1,c=read();
            add(u,t,c);
        }
        int ans=0;
        while(bfs())ans+=dfs(s,0x3f3f3f3f);
        printf("%d\n",ans);
    }
    return 0;
}

2018.07.06 POJ 1459 Power Network(多源多汇最大流)的更多相关文章

  1. poj1459 Power Network (多源多汇最大流)

    Description A power network consists of nodes (power stations, consumers and dispatchers) connected ...

  2. [poj1459]Power Network(多源多汇最大流)

    题目大意:一个网络,一共$n$个节点,$m$条边,$np$个发电站,$nc$个用户,$n-np-nc$个调度器,每条边有一个容量,每个发电站有一个最大负载,每一个用户也有一个最大接受量.问最多能供给多 ...

  3. POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)

    POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...

  4. poj 1459 Power Network

    题目连接 http://poj.org/problem?id=1459 Power Network Description A power network consists of nodes (pow ...

  5. poj 1459 Power Network : 最大网络流 dinic算法实现

    点击打开链接 Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 20903   Accepted:  ...

  6. poj 1459 Power Network【建立超级源点,超级汇点】

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 25514   Accepted: 13287 D ...

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

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 D ...

  8. 网络流--最大流--POJ 1459 Power Network

    #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #incl ...

  9. poj 1459 Power Network(增广路)

    题目:http://poj.org/problem?id=1459 题意:有一些发电站,消耗用户和中间线路,求最大流.. 加一个源点,再加一个汇点.. 其实,过程还是不大理解.. #include & ...

随机推荐

  1. Docker-删除untagged docker images

    故障描述 [root@entel1 ~]# docker rmi entel_zmc_images:zmc_base Untagged: entel_zmc_images:zmc_base 操作步骤 ...

  2. mysql 内存占用过多的解决方法

    以下是5.6默认的设置performance_schema_max_table_instances 12500table_definition_cache 1400table_open_cache 2 ...

  3. Access denied for user 'root'@'localhost' (using password:YES)解决方法

    Access denied for user 'root'@'localhost' (using password:YES)解决方法 在MySQL的使用过程中,我们可能会碰到“Access denie ...

  4. array numpy 模块

    高级用法:http://www.jb51.net/article/87987.htm from array import * 调用 array 与 import numpy as np  调用 np. ...

  5. lnmp上传文件

    LAMP环境: Linux Mint 16 32bits xfce apache 2.4.6 Ubuntu php 5.5.3 默认www是/var/www,我用符号连接到了/home/tony/ww ...

  6. lrzsz的安装与配置

    1)下载http://freshmeat.sourceforge.net/projects/lrzsz/ 2)tar zxvf lrzsz-0.12.20.tar.gz 3)mv lrzsz-0.12 ...

  7. 我在JS上解惑之路1

    1.为什么既然存在等号(==)非等号  (!=),又会有全等号(===)非全等号(!==)? *唯一的不同是后者判断时不进行类型转换. 例:var sNum = "66"; var ...

  8. My Goal For SE

    2016年2月份,我开始进入软件工程的学习.作为一名大三学生,我对于此项课程,希望我自己能够通过制定学习目标来学好它. 首先,当我们在执行软件开发的时候,我们应该明确每个人的负责模块,我们应该明确自己 ...

  9. css3文字截断

    width:200px; height:14px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; text-overflow ...

  10. 没有无线路由,如何让手机使用电脑的网络xyytit

    前言: 智能手机已经越来越普遍,但国内的无线网络的步伐还是没有跟上智能机的脚步.纵使3G,4G已经相继推出,但国内的资费价格着实有点不接地气,所以无线wifi无疑是智能机使用最多的.各大软件上.设备商 ...