POJ1459 Power Network —— 最大流

题目链接:https://vjudge.net/problem/POJ-1459

 

Power Network
Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 29270 Accepted: 15191

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

 

 

题意:

有np个供电站(只供电不用电)、nc个用电站(只用电不供电),以及n-np-nc个中转站(既不供电也不用电),且已经知道这些站的连接关系,问单位时间最多能消耗多少的电?

 

题解:

最大流问题。

1.建立超级源点,超级源点与每个供电站相连,且边的容量为供电站的最大供电量,表明流经此供电站的电量最多只能为自身的供电量。

2.建立超级汇点,每个用电站与超级汇点相连,且边的容量为用电站的最大用电量,表明流经此用电站的电量最多只能为自身的消耗量。

 

 

代码如下:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <vector>
  6 #include <cmath>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 using namespace std;
 13 typedef long long LL;
 14 const int INF = 2e9;
 15 const LL LNF = 9e18;
 16 const int mod = 1e9+7;
 17 const int MAXN = 1e2+10;
 18 
 19 int maze[MAXN][MAXN];
 20 int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
 21 int flow[MAXN][MAXN];
 22 
 23 int sap(int start, int end, int nodenum)
 24 {
 25     memset(cur, 0, sizeof(cur));
 26     memset(dis, 0, sizeof(dis));
 27     memset(gap, 0, sizeof(gap));
 28     memset(flow, 0, sizeof(flow));
 29     int u = pre[start] = start, maxflow = 0, aug = INF;
 30     gap[0] = nodenum;
 31 
 32     while(dis[start]<nodenum)
 33     {
 34         loop:
 35         for(int v = cur[u]; v<nodenum; v++)
 36         if(maze[u][v]-flow[u][v]>0 && dis[u] == dis[v]+1)
 37         {
 38             aug = min(aug, maze[u][v]-flow[u][v]);
 39             pre[v] = u;
 40             u = cur[u] = v;
 41             if(v==end)
 42             {
 43                 maxflow += aug;
 44                 for(u = pre[u]; v!=start; v = u, u = pre[u])
 45                 {
 46                     flow[u][v] += aug;
 47                     flow[v][u] -= aug;
 48                 }
 49                 aug = INF;
 50             }
 51             goto loop;
 52         }
 53 
 54         int mindis = nodenum-1;
 55         for(int v = 0; v<nodenum; v++)
 56             if(maze[u][v]-flow[u][v]>0 && mindis>dis[v])
 57             {
 58                 cur[u] = v;
 59                 mindis = dis[v];
 60             }
 61         if((--gap[dis[u]])==0) break;
 62         gap[dis[u]=mindis+1]++;
 63         u = pre[u];
 64     }
 65     return maxflow;
 66 }
 67 
 68 int main()
 69 {
 70     int n, np, nc, m;
 71     while(scanf("%d%d%d%d", &n,&np,&nc,&m)!=EOF)
 72     {
 73         int start = n, end = n+1;
 74         memset(maze, 0, sizeof(maze));
 75         for(int i = 1; i<=m; i++)
 76         {
 77             int u, v, w;
 78             while(getchar()!='(');
 79             scanf("%d,%d)%d",&u,&v,&w);
 80             maze[u][v] = w;
 81         }
 82 
 83         for(int i = 1; i<=np; i++)
 84         {
 85             int id, p;
 86             while(getchar()!='(');
 87             scanf("%d)%d", &id,&p);
 88             maze[start][id] = p;
 89         }
 90 
 91         for(int i = 1; i<=nc; i++)
 92         {
 93             int id, p;
 94             while(getchar()!='(');
 95             scanf("%d)%d", &id,&p);
 96             maze[id][end] = p;
 97         }
 98 
 99         cout<< sap(start, end, n+2) <<endl;
100     }
101 }
View Code

 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/8081368.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/415049.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android PopupWindow使用,下拉式PopupWindow,底部式PopupWindow

1、实现方法1 仿微信盆友圈弹出点赞、评论 demo连接&#xff1a;android开发PopupWindow实现跟随试弹出框-Android文档类资源-CSDN下载 实现步骤 1、下载module并引入项目 引入module步骤&#xff1a;Android studio 导入module方法…

Android 微信登录

/1、首先你得到微信.开放平台申请开发权限&#xff1a;https://open.weixin.qq.com/ 申请通过效果如下即可 2、其次&#xff0c;阅读开发文档&#xff1a; https://open.weixin.qq.com/cgi-bin/showdocument?actiondir_list&tresource/res_list&verify1&idopen1…

解决ubuntu16.04 qt5.9.1无法输入中文

1. 安装 fcitx-frontend-qt5 sudo apt-get install fcitx-frontend-qt5 2. 确认该路径下存在的文件 /usr/lib/x86_64-linux-gnu/qt5/plugins/platforminputcontexts/libfcitxplatforminputcontextplugin.so 3. 将libfcitxplatforminputcontextplugin.so 复制到以下两个路径下&…

Bitmap添加文字水印

private static Bitmap AddTimeWatermark(Bitmap mBitmap) {//获取原始图片与水印图片的宽与高int mBitmapWidth mBitmap.getWidth();int mBitmapHeight mBitmap.getHeight();Bitmap mNewBitmap Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ARGB_8888)…

工作200:视频上传和图片编辑功能

1眼睛一定要看清 本地接口 稳得很

highcharts x轴 按照时间 datetime排序

1、我的配置文件代码&#xff1a; var chart Highcharts.chart(warningCharts, {chart: {type: line,},style: {fontSize: 12px,color: #006cee,padding: 10rpx,},title: {text: null,},subtitle: {text: null,},series: data,xAxis: {type: datetime,dateTimeLabelFormats: …

B. 重载技术(overloading)

转载于:https://www.cnblogs.com/youyuanjuyou/p/8120968.html

highcharts默认选中最后一个点数据

效果图如下&#xff1a; 核心代码如下&#xff1a; chart: {type: line,events: {load: function () {let chart thisconst points []Highcharts.each(chart.series, function (s) {if (s.visible) {points.push(s.points[s.points.length - 1])}})chart.tooltip.refresh(p…

上传Android应用到腾讯应用宝,乐固加固应用使用

当我们开发完安卓系统APP之后。需要上传到应用市场 在上传到腾讯应用宝是&#xff0c;需要使用腾讯加固工具 乐固 加固apk。才能正常上架。 如没有加固应用会提示&#xff1a; 加固步骤: 1、下载乐固包&#xff1a;https://download.csdn.net/download/meixi_android/107534…

工作203:实现预览效果

1子组件 成功 ChangeRest(event, file){/* console.log(event)console.log(file)*//* console.log(URL.createObjectURL(file.raw))*/this.imageUrl URL.createObjectURL(file.raw);/* this.imageUrlevent.tmp_urlconsole.log(this.imageUrl)*/console.log(1)this.eventSav…

【深入Java虚拟机】之一:Java内存区域与内存溢出

内存区域 Java虚拟机在执行Java程序的过程中会把他所管理的内存划分为若干个不同的数据区域。Java虚拟机规范将JVM所管理的内存分为以下几个运行时数据区&#xff1a;程序计数器、Java虚拟机栈、本地方法栈、Java堆、方法区。下面详细阐述各数据区所存储的数据类型。 程序计数器…

Android应用安装apk版本升级,适配Android 8.0和Android 10.0下载安装,shell命令安装APK

安装失败&#xff0c;gradle.properties文件下添加 即可 android.injected.testOnly false shell命令安装 /*** 安装apk** param path apk文件路径*/ public void installAPK(String path) {Log.i(TAG, "installAPK:" path);com.dlc.xiaohaitun.utils.ShellUtil…

Android 自定义Switch,仿微信开关键Switch

switch (changeFragmentEvent.getViewId()) {//启动设备&#xff08;安卓接收&#xff09;case 1:textView.setText("vvvvv555"changeFragmentEvent.getstring());break;case 2:textView.setText("vvvvv555WWW22");break; } 效果 实现方法 1、drawable下创…

工作207:修改表头按钮样式

<el-card><div style"display: flex;justify-content: space-between"><h1 style"float: left;margin-top: 34px;margin-left: 32px;">我的任务</h1><el-button style"float: right;margin-top: 14px;margin-right: 10p…

Android 带阴影背景图片

1、添加依赖 compile com.dingmouren.paletteimageview:paletteimageview:1.0.7 2、引用 <com.dingmouren.paletteimageview.PaletteImageViewandroid:id"id/palette"android:layout_width"400dp"android:layout_height"400dp"android:lay…

Android画板控件,可以写字,签名,画画并生成图片

1效果图 实现步骤 1、添加画板控件module 画板控件module下载&#xff1a;https://download.csdn.net/download/meixi_android/10774781 2、xml文件 <?xml version"1.0" encoding"utf-8"?> <LinearLayoutandroid:id"id/content_main&q…