poj 1459 Power Network : 最大网络流 dinic算法实现

时间:2024-09-13 20:36:44

点击打开链接

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. 

poj 1459 Power Network : 最大网络流 dinic算法实现


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;
}