点击打开链接

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. string 字符串的分隔处理与list的相互转换

    在指定 String 数组的每个元素之间串联指定的分隔符 String,从而产生单个串联的字符串.(来源于MSDN) 有两个重载函数:[C#]public static string Join(   ...

  2. Spring和SpringMVC的区别

    spring 是是一个开源框架,是为了解决企业应用程序开发,功能如下◆目的:解决企业应用开发的复杂性◆功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能◆范围:任何Java应用简单 ...

  3. mac安装IE浏览器

    1.首先得下载一个WineBottler for mac. 2.下载完毕之后,打开dmg文件后将WineBottler Combo里面的Wine和WineBottler这两个程序拖拉进应用程序. 3. ...

  4. java.sql.SQLException: Io 异常: Connection reset

    当数据库连接池中的连接被创建而长时间不使用的情况下,该连接会自动回收并失效,但客户端并不知道,在进行数据库操作时仍然使用的是无效的数据库连接,这样,就导致客户端程序报“ java.sql.SQLExc ...

  5. Android ImageButton android:scaleType

    ImageView的属性android:scaleType,即 ImageView.setScaleType(ImageView.ScaleType). android:scaleType是控制图片如 ...

  6. bzoj1346: [Baltic2006]Coin

    Description 有一个国家,流通着N种面值的硬币,其中包括了1分硬币.另外,有一种面值为K分的纸币,它超过了所有硬币的面值. 有一位硬币收藏家,他想收集每一种面值的硬币样本.他家里已经有一些硬 ...

  7. read,for,case,while,if简单例子

    Read多用于从某文件中取出每行进行处理 $ cat read.sh #!/bin/bash echo "using read" cat name.txt | while read ...

  8. can't run as root without the -u switch

    [root@localhost ~]# /usr/local/memcache/bin/memcached can't run as root without the -u switch [root@ ...

  9. 【log】log4j

    常用log4j.properties配置文件 log4j.rootLogger = info,console #指定serviceImpl层 日志输出 log4j.logger.com.sms.ser ...

  10. item30,最小的k个数

    剑指offer给出两类方法: 1,借助快排的思想,需要修改输入数组的元素,时间复杂度O(n) 2,借助STL中set或者multiset,因为它们的底层数据结构是红黑树实现的,插入数据时间复杂度为O( ...