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,一经查实,立即删除!

相关文章

Python 异步操作文件 aiofiles

# 异步文件操作 # pip install aiofiles# 基本用法 import asyncio import aiofilesasync def wirte_demo():# 异步方式执行with操作,修改为 async withasync with aiofiles.open("text.txt","w",encoding"utf-8") as fp:await fp.write("h…

工作196:注意接收数据的格式

<!--获取投放权限的数据--><el-form-item label"投放权限" :label-width"formLabelWidth"><el-selectv-model"form.publish_permission"multipleplaceholder"请选择投放权限"><el-option v-for"(publish,in…

uvicorn 更改fastapi 运行host和port

在命令行输入uvicorn --help可以显示参数介绍&#xff0c;主要两个参数: --host TEXT Bind socket to this host. [default:127.0.0.1] --port INTEGER Bind socket to this port. [default: 8000]所以运行命令可以改成&#xff1a; uvicorn …

mysql问题处理积累

1.mysql errors:message from server: "Host xxx is blocked because of many connection errors; unblock 数据库连接抛了异常&#xff1a;null, message from server: "Host PC-20130201IBXI is blocked because of many connection errors; unblock with mysqlad…

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

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

工作197:判断对象里面是否有0和1

<el-form-item v-if"property.includes(0)" prop"business_module" label"所属栏目" :label-width"formLabelWidth"><!--注意用户的返回值--><el-select v-model"form.business_module" placeholder"…

Python __call__()方法

Python 类中一个非常特殊的实例方法&#xff0c;即 call()。该方法的功能类似于在类中重载 () 运算符&#xff0c;使得类实例对象可以像调用普通函数那样&#xff0c;以“对象名()”的形式使用。 举个例子&#xff1a; class CLanguage:# 定义__call__方法def __call__(self,n…

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 复制到以下两个路径下&…

工作198:无法选中的状态一定要绑定在select上面

<el-form-item prop"department_id" label"所属部门" :label-width"formLabelWidth"><select-form change"DepartmentList" v-model"form.department_id" /></el-form-item><el-form-item v-show&quo…

惊天大神坑 关于 python-socketio 与 socket.io-client 版本兼容问题

在使用fastapi进行开发中&#xff0c;使用python-socketio作为socketi库&#xff0c;前端项目使用vue开发&#xff0c;配合socket.io-client作为client端。 我的python-socketio版本号为4.6.0 前端socket.io-client我默认安装的最新的&#xff0c;此时最新的是3.0.4,但是发现…

vscode 1.9.11 和pycharm 5.0.4 输入os.getcwd()后输出不相同

各位&#xff0c;vscode的坑&#xff1b; os.getcwd()获取当前文件的位置 例如文件目录级&#xff1a; g:\test\123 vscode 输出的是父目录的位置 实际输出的结果为&#xff1a;g:\test 预期结果:g:\test\123&#xff08;这个才是正确的&#xff09; pycharm os.getcwd() 输出…

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)…

工作199:获取接口token

1第一步设置token getAction("/user/info").then(res>{Vue.ls.set(Set_Token,res.data.access_token)}) 2第二步 设置token serviceXing.interceptors.request.use(function (config) {// Do something before request is sent//window.localStorage.getItem(&…

mongoose查找若存在,则什么都不做,若不存在,则插入

直接上代码&#xff1a; ModelSchema.findOneAndUpdate( {id: searchId}, { $setOnInsert: {id: searchId,foo: foo,bar: bar,} }, { upsert: true }) .catch(error > console.error(error));

Android 启动白屏,简单解决方法

1、打开styles、找到ThemeSplash&#xff0c;或者AppTheme 添加windowBackground即可/windowFullscreen是满屏属性。可去掉 也就是添加启动背景图 <style name"ThemeSplash" parent"Theme.AppCompat.Light.NoActionBar"><item name"android…

工作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: …

Android 起调系统功能,打开系统浏览器,拨打电话,发送短信,手机震动,跳转到设置通知开关页面

1、打开系统浏览器 try {startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/jaredrummler/MaterialSpinner"))); } catch (ActivityNotFoundException ignored) { } 2、拨打电话 Intent intent new Intent(Intent.ACTION_DIAL, Uri.pa…