点击打开链接

Power Network
Time Limit: 2000MS   Memory Limit: 32768K
Total Submissions: 20903   Accepted: 10960

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.

题目大意就是说,供电站向用户供电,图中节点有3中,分别是供电站,中转站,用户,供电站有供电量,但是没有消耗量,用户没有供电量,但是有消耗量。中转站什么也没有,只是负责转送

题目主要是建图。,因为有多个源点和汇点,所以需要人为添加一个超级源点和一个超级汇点,超级源点指向所有源点,最大流量就是供电站的供电量,所有汇点指向超级汇点,流量就是用户的用点量,这样就构建了一个图,然后求出最大流就好了,说明一下输入格式,免得以后看不懂输入:

多组数据,一开始是4个整形,分别代表节点总数、供电站个数、用户个数、供电线路数,然后后面的m个(u,v)z格式的数据代表从 u 指向 v 的输电线最大流量为 z。

接下来的是供电站的数据,(v)z 代表 v 号是供电站,供电量为z,接下来是用户数据,(v)z ,代表v号是用户,用电量为z,输出最大输电量

AC代码,dinic算法实现的,可是讨论区里都说dinic算法时间可以跑到100以内,可是我的用了200+,还有哪能优化的希望有大神能指点

time 200+ms

#include<stdio.h>
#include<stack>
#include<queue>
#include<string.h>
using namespace std;
#define max 300//总最大点数
#define source max - 1 //题目中给出多个源点,添加超级源点
#define target max - 2 //题目中给出多个汇点,添加超级汇点
int map[max][max];
int layer[max];
int n;//题目中输入的实际节点个数
//广搜标记层次layer
bool bfs()
{
queue<int> q;
q.push(source);
bool used[max] = {0};
memset(layer, 0, sizeof(layer));
used[source] = 1;
while(!q.empty())
{
int top = q.front();
q.pop();
int i;
if(map[top][target] > 0)
return true;
for(i = 0; i < n; i++)
{
if(map[top][i] > 0 && !used[i])
{
layer[i] = layer[top] + 1;
q.push(i);
used[i] = 1;
}
}
}
return false;
}
int dinic()
{
int max_flow = 0;
int prev[max] = {0};
int used[max] = {0}; while(bfs())
{
//广搜如果返回true说明可以增广
stack<int> s;
memset(prev, 0, sizeof(prev));
memset(used, 0, sizeof(used));
//把超级源点放入栈
prev[source] = source;
s.push(source);
while(!s.empty())
{
int top = s.top();
//如果当前节点可以通向汇点
if(map[top][target] > 0)
{
int j = top;
int min = map[top][target];
int mark = top;
//通过prev数组找当前增广路径上的瓶颈路径
while(prev[j] != j)
{
if(map[prev[j]][j] < min)
{
min = map[prev[j]][j];
mark = prev[j];//记录下最小流量的起点
}
j = prev[j];
}//找到最小的流量以后把所有路径的流量都减去这个最小值,max_flow加上这个最小值,表示找到了一条增广路径
j = top;
map[top][target] -= min;
map[target][top] += min;
while(prev[j] != j)
{
map[prev[j]][j] -= min;
map[j][prev[j]] += min;
j = prev[j];
}
max_flow += min;//弹栈到mark位置
while(!s.empty() && s.top() != mark)
s.pop();
}
else// 如果不能指向汇点,那么模拟递归的方式向下找深搜
{
int i;
for(i = 0; i < n; i++)
{
if(map[top][i] > 0 && layer[i] == layer[top] + 1 && !used[i])
{
s.push(i);
used[i] = 1;
prev[i] = top;
break;
}
}
if(i == n)
s.pop();
}
}
}
return max_flow;
}
int main()
{
int np, nc, m;
// freopen("in.txt", "r", stdin);
while(scanf("%d%d%d%d", &n, &np, &nc, &m) != EOF)
{
memset(map, 0, sizeof(map));
int i;
int s, t, f;
for(i = 0; i < m; i++)
{
scanf(" (%d,%d)%d", &s, &t, &f);
map[s][t] += f;
}
for(i = 0; i < np; i++)
{
scanf(" (%d)%d", &s, &f);
map[source][s] += f;
}
for(i = 0; i < nc; i++)
{
scanf(" (%d)%d", &t, &f);
map[t][target] += f;
}
printf("%d\n", dinic());
}
return 0;
}

这是用的别人的模板 时间 100-ms

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int min(int a, int b)
{
return a > b ? b : a;
} const int inf = 0xfffffff;
#define clr(arr,v) memset(arr,v,sizeof(arr)) template<int MaxV,int MaxE>
class MaxFlow{
public:
int GetMaxFlow(int s,int t,int n) //s为源点,t为汇点,n为总点数
{
int maxflow = 0,minflow = inf,cur = s;
Cnt[0] = n;
memcpy(Cur,H,sizeof(H));
while(Gap[cur] <= n)
{
int &i = Cur[cur];
for(;i != -1;i = Next[i])
{
if(Cap[i]-Flow[i] > 0 && Gap[cur]-Gap[ Num[i] ] == 1)
{
pre_edge[ Num[i] ] = i;
cur = Num[i];
minflow = min(minflow,Cap[i]-Flow[i]);
if(cur == t)
{
maxflow += minflow;
while(cur != s)
{
Flow[ pre_edge[cur] ] += minflow;
Flow[ pre_edge[cur]^1 ] -= minflow;
cur = Num[ pre_edge[cur]^1 ];
}
minflow = inf;
}
break;
}
}
if(i == -1)
{
if(--Cnt[ Gap[cur] ] == 0) return maxflow;
Gap[cur] = inf;
i = H[cur];
for(int j = H[cur];j != -1;j = Next[j])
if(Cap[j] > Flow[j] && Gap[ Num[j] ] < Gap[cur])
Gap[cur] = Gap[ Num[j] ];
if(Gap[cur] != inf) ++Cnt[ ++Gap[cur] ];
cur = s;
}
}
return maxflow;
}
void add(int u,int v,int flow)
{
Num[pos] = v;
Cap[pos] = flow;
Next[pos] = H[u];
H[u] = pos++; Num[pos] = u;
Cap[pos] = 0;
Next[pos] = H[v];
H[v] = pos++;
}
void clear()
{
clr(H,-1); clr(Flow,0); clr(Cnt,0);
clr(Gap,0); pos = 0;
}
private:
int H[MaxV],Cur[MaxV],Num[MaxE],Next[MaxE];
int Cap[MaxE],Flow[MaxE],Cnt[MaxV];
int Gap[MaxV],pre_edge[MaxE],pos;
};
//MaxFlow<点的最大个数,边的最大个数> g;
MaxFlow<300, 90000> g;
int main()
{
int np, nc, m, n;
// freopen("in.txt", "r", stdin);
while(scanf("%d%d%d%d", &n, &np, &nc, &m) != EOF)
{
g.clear();
int i;
int s, t, f;
for(i = 0; i < m; i++)
{
scanf(" (%d,%d)%d", &s, &t, &f);
g.add(s, t, f);
}
for(i = 0; i < np; i++)
{
scanf(" (%d)%d", &s, &f);
g.add(299, s, f);
}
for(i = 0; i < nc; i++)
{
scanf(" (%d)%d", &t, &f);
g.add(t, 298, f);
}
printf("%d\n", g.GetMaxFlow(299, 298, n + 2));
}
return 0;
}

poj 1459 Power Network : 最大网络流 dinic算法实现的更多相关文章

  1. 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 Networ ...

  2. poj 1459 Power Network

    题目连接 http://poj.org/problem?id=1459 Power Network Description A power network consists of nodes (pow ...

  3. 网络流--最大流--POJ 1459 Power Network

    #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #incl ...

  4. POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 D ...

  5. POJ 1273 Drainage Ditches(网络流dinic算法模板)

    POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...

  6. poj 1459 Power Network【建立超级源点,超级汇点】

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 25514   Accepted: 13287 D ...

  7. 2018.07.06 POJ 1459 Power Network(多源多汇最大流)

    Power Network Time Limit: 2000MS Memory Limit: 32768K Description A power network consists of nodes ...

  8. POJ 1459 Power Network(网络最大流,dinic算法模板题)

    题意:给出n,np,nc,m,n为节点数,np为发电站数,nc为用电厂数,m为边的个数.      接下来给出m个数据(u,v)z,表示w(u,v)允许传输的最大电力为z:np个数据(u)z,表示发电 ...

  9. POJ 1459 Power Network 最大流(Edmonds_Karp算法)

    题目链接: http://poj.org/problem?id=1459 因为发电站有多个,所以需要一个超级源点,消费者有多个,需要一个超级汇点,这样超级源点到发电站的权值就是发电站的容量,也就是题目 ...

随机推荐

  1. 【转】C#综合揭秘——通过修改注册表建立Windows自定义协议

    引言 本文主要介绍注册表的概念与其相关根项的功能,以及浏览器如何通过连接调用自定义协议并与客户端进行数据通信.文中讲及如何通过C#程序.手动修改.安装项目等不同方式对注册表进行修改.其中通过安装项目对 ...

  2. MyBatis SQL xml处理小于号与大于号

    MyBatis SQL xml处理小于号与大于号 当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台 ...

  3. 【mysql】MySQL存储IP地址

    为什么要问如何存储IP 首先就来阐明一下部分人得反问:为什么要问IP得怎样存,直接varchar类型不就得了吗? 其实做任何程序设计都要在功能实现的基础上最大限度的优化性能.而数据库设计是程序设计中不 ...

  4. Linux dirname $0 source if

    $SHELL gives the full path to your default shell. $0 gives the name of your current shell. dirname是一 ...

  5. [转]Hibernate3如何解决n+1 selects

    摘自: http://blog.chinaunix.net/uid-20586655-id-287959.html     Hibernate3中取得多层数据的所产生的n+1 selects问题的解决 ...

  6. IOS开发-UITextField代理常用的方法总结

    1.//当用户全部清空的时候的时候 会调用 -(BOOL)textFieldShouldClear:(UITextField *)textField: 2.//可以得到用户输入的字符 -(BOOL)t ...

  7. Xshell远程连接Linux时无法使用小键盘的解决方式

    我在用xshell连接远程的centos时,每次使用vi/vim的时候而NumLock明明在开启着,小键盘都不能正确输入数字,其实这是时按小而是出现一个字母然后换行(实际上是命令模式上对应上下左右的键 ...

  8. windows下制作u盘启动的工具

    推荐rufus,快捷方便

  9. SVN设置实例

    D:\scmserver\SVNROOT\safeControl,该SVN项目下,有erSystem和hcSystem两个项目.现在人员有两种类型的人,一个内部人员,一个是佰钧成人员. 设置要求: 1 ...

  10. 关于ORALCE一个表空间的数据导入到另一个表空间的方法(原创)

    用户:   whnaproject     所属表空间: whnaproject 新用户   : wniec            所属新表空间: wniec 要求:将用户whnaproject中的数 ...