您的位置:首页 > 其它

Power Network (最大流增广路算法模板题)

2013-08-12 16:55 381 查看
Time Limit: 2000MSMemory Limit: 32768K
Total Submissions: 20754Accepted: 10872
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.

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

const int MAX = 150;
const int INF = 0x3f3f3f3f;
int n,np,nc,m,mf,s,t;
int cap[MAX][MAX],flow[MAX][MAX],a[MAX];
int pre[MAX];
char str[20];
queue <int> que;
void maxflow()
{
memset(flow,0,sizeof(flow));//初始化,所有的边的流量初始为0;
mf = 0;//记录最大流
for(;;)
{
memset(a,0,sizeof(a));//s到每个节点路径上的最小残量
a[s] = INF;
que.push(s);
//bfs找增广路
while(!que.empty())
{
int u = que.front();
que.pop();
for(int v = 0; v <= n+1; v++)
{
if(!a[v] && cap[u][v] > flow[u][v])//找到新的节点v
{
pre[v] = u;//记录前驱并加入队列
que.push(v);
if(a[u] < cap[u][v]-flow[u][v])
a[v] = a[u];
else a[v] = cap[u][v]-flow[u][v];//s到v路径上的最小残量
}
}
}
if(a[t] == 0) break;//找不到最小残量,当前流已经是最大流;
for(int u = t; u!= s;u = pre[u])//从汇点往回走
{
flow[pre[u]][u] += a[t];//更新正向流量
flow[u][pre[u]] -= a[t];//更新反向流量
}
mf += a[t];//更新从s流出的总流量
}
}

int main()
{
int u,v,z;
while(~scanf("%d %d %d %d",&n,&np,&nc,&m))
{
memset(cap,0,sizeof(cap));
while(m--)
{
scanf("%s",str);
sscanf(str,"(%d,%d)%d",&u,&v,&z);
cap[u][v] = z;
}

while(np--)//有多个起点
{
scanf("%s",str);
sscanf(str,"(%d)%d",&v,&z);
cap
[v] = z;//将多个起点连接到一个新的顶点作为起点;
}

while(nc--)//有多个终点
{
scanf("%s",str);
sscanf(str,"(%d)%d",&u,&z);
cap[u][n+1] = z;//将多个终点连接到一个新的终点作为终点;
}
s = n;
t = n+1;
maxflow();
printf("%d\n",mf);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: