poj 1459 Power Network : 最大网络流 dinic算法实现
| Time Limit: 2000MS | Memory Limit: 32768K | |
| Total Submissions: 20903 | Accepted: 10960 |
Description
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
(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
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
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算法实现的更多相关文章
- 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 ...
- poj 1459 Power Network
题目连接 http://poj.org/problem?id=1459 Power Network Description A power network consists of nodes (pow ...
- 网络流--最大流--POJ 1459 Power Network
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #incl ...
- POJ 1459 Power Network(网络流 最大流 多起点,多汇点)
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 22987 Accepted: 12039 D ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- poj 1459 Power Network【建立超级源点,超级汇点】
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 25514 Accepted: 13287 D ...
- 2018.07.06 POJ 1459 Power Network(多源多汇最大流)
Power Network Time Limit: 2000MS Memory Limit: 32768K Description A power network consists of nodes ...
- POJ 1459 Power Network(网络最大流,dinic算法模板题)
题意:给出n,np,nc,m,n为节点数,np为发电站数,nc为用电厂数,m为边的个数. 接下来给出m个数据(u,v)z,表示w(u,v)允许传输的最大电力为z:np个数据(u)z,表示发电 ...
- POJ 1459 Power Network 最大流(Edmonds_Karp算法)
题目链接: http://poj.org/problem?id=1459 因为发电站有多个,所以需要一个超级源点,消费者有多个,需要一个超级汇点,这样超级源点到发电站的权值就是发电站的容量,也就是题目 ...
随机推荐
- Android Wear(手表)开发 - 学习指南
版权声明:欢迎自由转载-非商用-非衍生-保持署名.作者:Benhero,博客地址:http://www.cnblogs.com/benhero/ Android Wear开发 - 学习指南 http: ...
- 对自己的文件使用keystore签名
keytool 对jar包签名步骤: 1.将程序打成jar包. 2.生成keystore数字证书keytool -genkey -keystore xxx.keystore -alias xxx -v ...
- 无需添加引用执行JS,发布无需带DLL、例子:QQMD5 QQGTK 13位时间戳 取随机数
javascriptDemo.rar 本人写POST经常会遇到用JS来加密的一些网站,然后又不想用C#重写.在百度和论坛里找的JS执行不是64位不支持就是要带个DLL神马的.很讨厌.然后自己就写了个不 ...
- 【linux】linux启动过程
- 未在本地计算机上注册"Microsoft.Jet.OLEDB.4.0"解决方案
可以到http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=c06b8369-60dd-4b64-a44b-84b371ede1 ...
- 【转】如何使用PhoneGap打包Web App
如何使用PhoneGap打包Web App 最近做了一款小游戏,定位是移动端访问,思来想去最后选择了jQuery mobile最为框架,制作差不多以后,是否可以打包成App,恰好以前对PhoneGap ...
- tespeed-测试网速的Python工具
1.安装(环境CentOS7) #pip install lxml #wget wget http://sourceforge.net/projects/socksipy/files/socksipy ...
- Python-While刷博爬虫
仅用于测试 #!/usr/bin/python import webbrowser as web import time import os url = 'www.abc.com' while Tru ...
- 解决 SQLite数据库 no current row
场景: SQLite数据库,在查询数据时,提示 标题错误异常.查看堆栈,是在SQLiteDataReader.CheckValidRow 时报错. 数据查询是通过 adapter.Fill(dt) 进 ...
- jQuery getJSON() 能给外部变量赋值
//getJSON 内部已经赋值给count,alert出数据来看看是不是0 var count=0; $.getJSON(sUrl,{"ran": new Date().getD ...