poj 1459 Power Network (网络最大流)

本文探讨了如何使用网络流模型解决电力网络中最大电力消耗的问题。通过建立包含发电站、消费者和调度器的网络模型,引入源点和汇点,详细阐述了利用BFS算法寻找增广路径并计算最大流的过程。

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

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) <= 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.

 

采用网络流的模型来解决,在原图的基础上添加一个源点s和汇点t,对于每个发电站,从源点s引一条容量为pmax的弧;对于每个用电场所,引一条容量为cmax的弧到汇点t。

对于三元组(u,v,z),从u引一条容量为z的弧到v,最大电力消耗con就是这个网络的最大流。

采用相邻矩阵来存储网络中的流量(f[n][n])和容量(c[n][n]),采用BFS算法求增广路径时需要用一个队列q[n]和记录增广路径的数组fa[n],n是指网路中的节点个数,fa[i]为增广路径上节点i的前驱。

 

#include <iostream>
#include <stdio.h>
#include <cmath>
#include <string.h>
#include <queue>
using namespace std;
int n,np,nc,m,s,t;
int fa[104],f[104][104],c[104][104];
//fa[j]存储增广路径,为节点j在增广路径上的前驱节点,正数表示该弧为前向弧,负数表示该弧为后向弧
//f[][],c[][]记录网路中的流量和容量,存储方式为相邻矩阵
void pro()
{
    int i,j,d,d0,ans=0;
    fa[t]=1;   //汇点的前驱指针初始化
    while(fa[t]!=0)  //如果增广路径存在
    {
        queue<int> q;  //定义队列
        q.push(s);  //源点进入队尾
        memset(fa,0,sizeof(fa));  //增广路径初始化
        fa[s]=s; //源点的前驱指针指向自己
        while((!q.empty())&&(fa[t]==0)) //如果队列非空并且没有找到至汇点的增广路径
        {
            i=q.front(); q.pop(); //取出队首节点
            for(j=1;j<=t;j++)  //枚举未在增广路径上的节点j
                if(fa[j]==0)
                 if(f[i][j]<c[i][j]) //如果(i,j)的流量可增加,则(i,j)作为前向弧加入增广路径,j进入队列
                 {
                    fa[j]=i;
                    q.push(j);
                 }
                 else if(f[j][i]>0)//如果(i,j)可退流,则(i,j)作为后向弧加入增广路径,j进入队列
                 {
                     fa[j]=-i;
                     q.push(j);
                 }
        }
        if(fa[t]!=0)//如果找到一条从源点到汇点增广路径就改进当前流
        {
            d0=99999999;
            i=t; //从汇点出发倒推计算最大可改进量d0
            while(i!=s) //还没有倒推至源点
            {
                if(fa[i]>0) //i节点为尾的弧是前向弧
                  {if((d=c[fa[i]][i]-f[fa[i]][i])<d0)
                    d0=d;
                  }
                else if(f[i][-fa[i]]<d0)//i节点为尾的弧是后向弧
                    d0=f[i][-fa[i]];
                i=abs(fa[i]);//继续沿前驱指针倒推计算最大可改进量d0
            }
            ans=ans+d0; //总流量增加d0
            i=t;  //从汇点出发倒推调整增广路径上的流量
            while(i!=s)
            {
                if(fa[i]>0) //若i节点为尾的弧是前向弧,则该弧流量增加d0
                    f[fa[i]][i]+=d0;
                else
                    f[i][-fa[i]]-=d0;//若i节点为尾的弧是后向弧,则该弧流量减少d0
                i=abs(fa[i]); //继续沿前驱指针调整流量
            }
        }
    }
    cout<<ans<<endl;//输出最大流
}
int main()
{
    int i,u,v,cc;
    while(cin>>n>>np>>nc>>m)//反复输入节点数,发电站数目,用电场所数目,电力传输线数目
    {
        s=n+2;t=n+1;  //设置源点s和汇点t
        memset(f,0,sizeof(f));
        memset(c,0,sizeof(c));
        for(i=1;i<=m;i++)  //对于原图中的边(u,v)连一条容量是cc的弧
        {
            while(getchar()!='(');
            scanf("%d,%d)%d",&u,&v,&cc);
            c[u+1][v+1]=cc;
        }
        for(i=1;i<=np;i++)  //源点向每一个发电站连一条容量是cc的弧
        {
            while(getchar()!='(');
            scanf("%d)%d",&u,&cc);
            c[s][u+1]=cc;
        }
        for(i=1;i<=nc;i++) //每个用电场所向汇点连一条容量是cc的弧
        {
            while(getchar()!='(');
            scanf("%d)%d",&u,&cc);
            c[u+1][t]=cc;
        }
        pro();  //求最大流
    }
    return 0;
}

转载于:https://www.cnblogs.com/MisdomTianYa/p/6581859.html

内容概要:本文详细介绍了施耐德M580系列PLC的存储结构、系统硬件架构、上电写入程序及CPU冗余特性。在存储结构方面,涵盖拓扑寻址、Device DDT远程寻址以及寄存器寻址三种方式,详细解释了不同类型的寻址方法及其应用场景。系统硬件架构部分,阐述了最小系统的构建要素,包括CPU、机架和模块的选择与配置,并介绍了常见的系统拓扑结构,如简单的机架间拓扑和远程子站以太网菊花链等。上电写入程序环节,说明了通过USB和以太网两种接口进行程序下载的具体步骤,特别是针对初次下载时IP地址的设置方法。最后,CPU冗余部分重点描述了热备功能的实现机制,包括IP通讯地址配置和热备拓扑结构。 适合人群:从事工业自动化领域工作的技术人员,特别是对PLC编程及系统集成有一定了解的工程师。 使用场景及目标:①帮助工程师理解施耐德M580系列PLC的寻址机制,以便更好地进行模块配置和编程;②指导工程师完成最小系统的搭建,优化系统拓扑结构的设计;③提供详细的上电写入程序指南,确保程序下载顺利进行;④解释CPU冗余的实现方式,提高系统的稳定性和可靠性。 其他说明:文中还涉及一些特殊模块的功能介绍,如定时器事件和Modbus串口通讯模块,这些内容有助于用户深入了解M580系列PLC的高级应用。此外,附录部分提供了远程子站和热备冗余系统的实物图片,便于用户直观理解相关概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值