点击打开链接

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. VB的if和elseif

    VB中if和elseif的用法是: if...then...elseif...then...else...endif 切记在then的后面不要加冒号,加了冒号出现else没有if的错误,因为加了冒号表 ...

  2. Android 广播大全 Intent Action 事件

    Intent.ACTION_AIRPLANE_MODE_CHANGED; //关闭或打开飞行模式时的广播 Intent.ACTION_BATTERY_CHANGED; //充电状态,或者电池的电量发生 ...

  3. sqlserver快照,启用基于行版本控制的隔离级别

    在sqlserver标准的已提交读(read committed)隔离级别下,读写操作相互阻塞.未提交读(read uncommitted)虽然不会有这种阻塞,但是读操作可能会读到脏数据,这是大部分用 ...

  4. Jfinal中定时器的初步探索(一)

    1.添加包引用:/jfinal_demo/WebContent/WEB-INF/lib/quartz-all-1.6.1.jar 注意版本号,这个版本是现在项目中使用的,已经有更高版本了,但这版比较稳 ...

  5. SQL SERVER 生成MYSQL建表脚本

    /****** Object: StoredProcedure [dbo].[GET_TableScript_MYSQL] Script Date: 06/15/2012 13:05:14 ***** ...

  6. ArrayList和LinkedList遍历方式及性能对比分析

    ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayLis ...

  7. Android 服务端开发之开发环境配置

    Android 服务端开发之开发环境配置 这里是在Eclipse的基础上安装PhpEclipse插件方法,PHPEclipse是Eclipse的 一个用于开发PHP的插件.当然也可以采用Java开发a ...

  8. 【Executor】配置ThreadPoolExecutor

    来自为知笔记(Wiz) 附件列表

  9. DateGridView中添加下拉框列并实现数据绑定、更改背景色

    1.添加下拉框 代码实现==> using System; using System.Collections.Generic; using System.Windows.Forms; names ...

  10. 推荐一个golang的json库

    生成json的库 https://github.com/bennyscetbun/jsongo https://github.com/donnie4w/json4g