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 Network / FZU 1161 (网络流,最大流)
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
Http
POJ:https://vjudge.net/problem/POJ-1459
HIT:https://vjudge.net/problem/HIT-1228
UVAlive:https://vjudge.net/problem/UVALive-2760
ZOJ:https://vjudge.net/problem/ZOJ-1734
FZU:https://vjudge.net/problem/FZU-1161
Source
网络流,最大流
题目大意
给定一个能量网络,这个网络中有供能的机械,也有接收能量的机械,还有用作能量中转的机械。机械之间有能量管道相连,每一个管道都有一个能量上限。现在给出每一个供能机械的最大供能和每一个接收能量机械的最多需要能量,求最多能给所有接收能量的机械提供多少能量。
解决思路
这种长的英文题是锻炼英语阅读能力最好的机会
首先解释一下题意。题目中给出了三种机械,内容如题目大意中所述,求最大能量供应。输入中,先输入的四个整数分别是总机械数,供能机械数,接收能量机械数,能量管道数。接着m个是能量管道(u,v)w代表从u到v有一条上限为w的单向管道。再接着是np个供能机械,(u)w代表编号为u的供能机械能最多提供w的能量。最后是nc个接收能量机械,(u)w代表编号为u的最多接收w能量。
我们设立源点和汇点。对于每一个供能机械,从源点连出一条边到它,容量就为其能提供的最大能量数。对于每一个接收能量的机械,连一条边到汇点,容量就为其要接收的最大能量。其它的边就是题中的能量管道,容量就是能量管道的上限。这样跑一边最大流即可。
关于最大流,这里使用Dinic实现,可以参考我的这篇文章。
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxN=300;
const int maxM=maxN*maxN*2;
const int inf=2147483647;
class Edge
{
public:
int u,v,flow;
};
int n,np,nc;//总机械数,供能机械数,收能机械数
int m;//能量管道数
int cnt=-1;
int Head[maxN];
int Next[maxM];
Edge E[maxM];
int depth[maxN];
int cur[maxN];
int Q[maxN];
void Add_Edge(int u,int v,int flow);
bool bfs();
int dfs(int u,int flow);
int main()
{
while (cin>>n>>np>>nc>>m)//多组数据
{
cnt=-1;//每次先要清空
memset(Head,-1,sizeof(Head));
while (m--)//读入边
{
int u,v,w;
while (getchar()!='(');//注意这里读入时要特殊处理一下
scanf("%d,%d)%d",&u,&v,&w);
Add_Edge(u+1,v+1,w);
}
while (np--)//读入供能机械
{
int u,maxflow;
while (getchar()!='(');
scanf("%d)%d",&u,&maxflow);
Add_Edge(0,u+1,maxflow);
}
while (nc--)//读入收能机械
{
int u,maxflow;
while (getchar()!='(');
scanf("%d)%d",&u,&maxflow);
Add_Edge(u+1,n+1,maxflow);
}
/*for (int i=0;i<=n+1;i++)
{
for (int j=Head[i];j!=-1;j=Next[j])
cout<<i<<" "<<E[j].v<<" "<<E[j].flow<<endl;
cout<<endl;
}
//*/
int Ans=0;//求解最大流,Dinic算法
while (bfs())
{
for (int i=0;i<=n+1;i++)
cur[i]=Head[i];
while (int di=dfs(0,inf))
Ans+=di;
}
printf("%d\n",Ans);
}
return 0;
}
void Add_Edge(int u,int v,int flow)
{
cnt++;
Next[cnt]=Head[u];
Head[u]=cnt;
E[cnt].u=u;
E[cnt].v=v;
E[cnt].flow=flow;
cnt++;
Next[cnt]=Head[v];
Head[v]=cnt;
E[cnt].u=v;
E[cnt].v=u;
E[cnt].flow=0;
}
bool bfs()
{
memset(depth,-1,sizeof(depth));
int h=1,t=0;
depth[0]=1;
Q[1]=0;
do
{
t++;
int u=Q[t];
for (int i=Head[u];i!=-1;i=Next[i])
{
int v=E[i].v;
if ((E[i].flow>0)&&(depth[v]==-1))
{
depth[v]=depth[u]+1;
h++;
Q[h]=v;
}
}
}
while (t!=h);
if (depth[n+1]==-1)
return 0;
return 1;
}
int dfs(int u,int flow)
{
if (u==n+1)
return flow;
for (int &i=cur[u];i!=-1;i=Next[i])
{
int v=E[i].v;
if ((depth[v]==depth[u]+1)&&(E[i].flow>0))
{
int di=dfs(v,min(flow,E[i].flow));
if (di>0)
{
E[i].flow-=di;
E[i^1].flow+=di;
return di;
}
}
}
return 0;
}
POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)的更多相关文章
- poj 1459 Power Network
题目连接 http://poj.org/problem?id=1459 Power Network Description A power network consists of nodes (pow ...
- poj 1459 Power Network : 最大网络流 dinic算法实现
点击打开链接 Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 20903 Accepted: ...
- poj 1459 Power Network【建立超级源点,超级汇点】
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 25514 Accepted: 13287 D ...
- POJ 1459 Power Network(网络流 最大流 多起点,多汇点)
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 22987 Accepted: 12039 D ...
- 2018.07.06 POJ 1459 Power Network(多源多汇最大流)
Power Network Time Limit: 2000MS Memory Limit: 32768K Description A power network consists of nodes ...
- Power Network (poj 1459 网络流)
Language: Default Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 23407 ...
- Power Network - poj 1459 (最大流 Edmonds-Karp算法)
Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24788 Accepted: 12922 Description A ...
- POJ 1459:Power Network 能源网络
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 25414 Accepted: 13247 D ...
- 网络流--最大流--POJ 1459 Power Network
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #incl ...
随机推荐
- JVM规范系列第4章:Class文件格式
这一章节讲的是字节码的整个组成格式,读懂了这一章,就读懂了字节码文件.对于这一章的学习,我更推荐作为工具书去查找.最好是找一个最简单的Hello World例子,一个字节一个字节去分析其含义.在分析过 ...
- ASPCMS_判断语句if标签的使用
这几天在仿个企业站,又用到了ASPCMS.这个CMS系统支持响应式模板——视访问设备而使用不同的模板,这样PC.手机都能兼顾. ▼官方给出的说明: 1.满足条件则显示 {if:条件语句} 显示内容 { ...
- Python_命名空间和作用域_25
# 函数进阶 a = def func(): print(a) func() # 命名空间和作用域 # print() # input() # list # #命名空间 有三种 #内置命名空间 —— ...
- 矩形A + B HDU2524
题意 给你n*m的棋盘问有多少个矩形 分析 先看只有一行或一列的情况有1+2+....+n个,因为矩形的类型有1个最小单位格子n个,2个最小单位格子n-1个,n个最小单位格子有一个 code #inc ...
- 2016-03-22 OneZero团队 Daily Scrum Meeting
会议时间: 2016-03-22 9:33-9:57am 会议内容: 一.在原有Sprint Backlog基础上,我们加了亮点(摇一摇功能:随机选取一条记录在界面显示,以提醒主页君回忆) 需求分析图 ...
- Pair Work:电梯调度算法的实现和测试 by 12061171 and 12061168
结队成员简介: 成员:牛强,学号12061171:刘文乔,学号120611683 我们之所以结对编程以完成所给课设要求,是因为我们互相了解彼此,能够更好更快地完成.下图是我们合作编程时的留影: 牛强是 ...
- Linux内核分析——可执行程序的装载
链接的过程 首先运行C预处理器cpp,将C的源程序(a.c)翻译成ASCII码的中间文件(a.i) 接着C编译器ccl,将a.i翻译成ASCII汇编语言文件a.s 接着运行汇编器as,将a.s翻译成可 ...
- 《蹭课神器》Alpha版使用说明
<蹭课神器>是一款方便大学生蹭课的软件,目前实现了查询课表的功能,还没有实现搜索和提醒的功能.有待进一步的开发! 登录之后点击查询操作,查询课表. 课表显示如下
- SQL之SELECT语句执行顺序及子句功能
1.select 语句的执行顺序 SELECT a.id,a.`product_name`,a.`agreement_copies` i,b.id as statusId from `opmp_pro ...
- David Silver强化学习Lecture2:马尔可夫决策过程
课件:Lecture 2: Markov Decision Processes 视频:David Silver深度强化学习第2课 - 简介 (中文字幕) 马尔可夫过程 马尔可夫决策过程简介 马尔可夫决 ...