POJ1459——最大流模板题(EK,多方法)

本文解析了一个关于电力网络的最大流问题,通过构建一个包含多个源点和汇点的网络模型,采用Edmonds-Karp算法解决电力网络中如何达到最大消费功率的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://poj.org/problem?id=1459

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) <= p max(u) of power, may consume an amount 0 <= c(u) <= min(s(u),c max(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) <= l max(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 p max(u)=y. The label x/y of consumer u shows that c(u)=x and c max(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and l max(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 l max(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of p max(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 c max(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.

题目翻译

电力网络由由电力传输线路连接的节点(发电站、消费者和调度员)组成。节点 u 可以随附功率为 s(u) >= 0,可产生功率量 0 <= p(u)‎‎最大功率,可消耗功率量 0 <= c(u) <= 最小(s)(u),最大(u)功率‎‎,并可提供数量 d(u)\s(u)\p(u)-c(u)的权力。以下限制适用:c(u)=0 适用于任何发电站,p(u)=0 适用于任何使用者,p(u)\c(u)=0 适用于任何调度程序。最多有一个电源传输线 (u,v) 从节点到网络中的节点 v;它将您向 v 传输的最大功率‎‎数‎‎0 <* l(u,v) <= l 最大(u,v) 功率。让 Con=u c(u) 是网络中消耗的功率。 ‎‎问题是计算 Con 的最大值。‎


‎图 1 中举例说。电站 u 的标签 x/y 显示 p(u)=x 和 p‎‎最大值‎‎(u)=y。使用者 u 的标签 x/y 显示 c(u)=x 和 c‎‎最大值‎‎(u)=y。电力传输线 (u,v) 的标签 x/y 显示 l(u,v)=x 和 l‎‎最大值‎‎(u,v)=y。消耗的功率为 Con=6。请注意,网络还有其他可能状态,但 Con 的值不能超过 6。‎

‎输入‎

‎输入中有多个数据集。每个数据集对电网进行编码。它以四个整数开头:0 <= n <= 100 (节点),0 <= np <= n (发电站),0 <= nc (使用者)和 0 <= m <= n_2(电力传输线)。遵循 m 数据三元数 (u,v) z,其中您和 v 是节点标识符(从 0 开始)和 0 <= z <= 1000 是‎‎l 最大值‎‎(u,v)。遵循 np doublets (u) z,其中 u 是发电站的标识符,0 <= z <= 10000 是 p‎‎最大值‎‎(u) 的值。数据集以 nc doublets (u) z 结尾,其中 u 是使用者的标识符,0 <= z <= 10000 是 c‎‎最大值‎‎(u) 的值。所有输入数字都是整数。除了(u,v)z三胞胎和(u)z双精度,不包含空格,空白可以在输入中自由出现。输入数据以文件结尾终止,并且是正确的。‎

‎输出‎

‎对于来自输入的每个数据集,程序在标准输出上打印可在相应网络中消耗的最大功率。每个结果都有一个积分值,并从单独的行的开头打印出来。‎

 

其实题目的意思就是给你n个点,np个源点,nc个汇点,m条边,然后让你求所有汇点的最大流。

比较经典的网络流问题,在刘汝佳的白书(P365页)里也有介绍,其实就是多个源点,多个汇点的最大流问题。

书里讲的方法就是找一个超级源点和超级汇点,超级源点和每个源点连一条边,容量为无穷大,同样的每个汇点和超级汇点连一条边,容量也为无穷大,至于为什么无穷大,自己想一下应该就懂了。

最后跑一个最大流就行了。求最大流的方法很多,都应该可以过!!!这里给出最菜的EK算法。

#include <iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e4+7;
int pre[maxn],visited[maxn],G[maxn][maxn];
int S,T;
int n,np,nc,m;
struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct EdmondsKarp{
    int n,m;
    vector<Edge>edges;//边数的两倍
    vector<int>G[maxn];//邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
    int a[maxn];//当起点到i的可改进量
    int p[maxn];//最短路树上p的入弧编号

    void init(int n){
        for(int i=0;i<n;i++)G[i].clear();
        edges.clear();
    }

    void AddEdge(int from,int to,int cap){
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    int Maxflow(int s,int t){
        int flow=0;
        for(;;){
            memset(a,0,sizeof(a));
            queue<int>Q;
            Q.push(s);
            a[s]=INF;
            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(!a[e.to]&&e.cap>e.flow){
                        p[e.to]=G[x][i];
                        a[e.to]=min(a[x],e.cap-e.flow);
                        Q.push(e.to);
                    }
                }
                if(a[t])break;
            }
            if(!a[t])break;
            for(int u=t;u!=s;u=edges[p[u]].from){
                edges[p[u]].flow+=a[t];
                edges[p[u]^1].flow-=a[t];
            }
            flow+=a[t];
        }
        return flow;
    }
}EK;
int main(int argc, char** argv) {
	while(~scanf("%d%d%d%d",&n,&np,&nc,&m)){
		EK.init(n+2);
		for(int i = 1;i<=m;++i){
			int x,y,w;
			scanf("%*1s%d%*1s%d%*1s%d",&x,&y,&w);
			EK.AddEdge(x,y,w);
		}
		S=n++;
		for(int i = 1;i<=np;++i){
			int x,w;
			scanf("%*1s%d%*1s%d",&x,&w);
			EK.AddEdge(S,x,w);
		}
		T=n++;
		for(int i = 1;i<=nc;++i){
			int x,w;
			scanf("%*1s%d%*1s%d",&x,&w);
			EK.AddEdge(x,T,w);
		}
		printf("%d\n",EK.Maxflow(S,T));
	} 
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值