Power Network (最大流增广路算法模板题)
2013-08-12 16:55
381 查看
Time Limit: 2000MS | Memory Limit: 32768K | |
Total Submissions: 20754 | Accepted: 10872 |
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
相关文章推荐
- 最大流ISAP(距离标号最短增广路算法)模板
- 【二分匹配】【匈牙利算法即由增广路求最大匹配模板】
- EK(BFS)求最大流的算法模板(邻接表)
- 透彻解析二分图最大匹配匈牙利算法模板
- 模板---图论:网络流增广路算法 Dinic
- 网络流初步 增广路算法求最大流 hdoj3549
- 最大流 - Edmonds-Karp 增广路算法
- 网络最大流最短增广路Dinic算法模板
- 二分图最大匹配算法模板
- 算法模板——sap网络最大流 1(非递归+邻接矩阵)
- 最大匹配算法(匈牙利算法)模板
- 对数组进行排序、求最大值和求元素和的算法都编写为函数模板,采用相关数据进行测试。
- 6.对数组进行排序、求最大值和求元素和的函数采用静态成员函数的方式封装成数组算法类模板ArrayAlg,并采用相关数据进行测试。
- 算法模板——Dinic网络最大流 1
- 二分图最大匹配匈牙利算法模板两种
- 【二分图匹配入门专题1】F - COURSES poj1469【最大匹配--匈牙利算法模板题】
- Kuhn_Munkres最大权匹配算法C++模板
- bzoj 1497: [NOI2006]最大获利-----------算法模板[最大权闭合子图]&省选计划系列
- 求一个矩阵最大子矩阵的算法模板
- 二分图最大匹配算法-Hopcroft-Karp模板