程序员人生 网站导航

poj1459,网络流,最大流,多源点多汇点

栏目:php教程时间:2015-05-04 10:33:15

题意:
给几个发电站,给几个消耗站,再给几个转发点。
发电站只发电,消耗站只消耗电,转发点只是转发电,再给各个传送线的传电能力。
问你消耗站能取得的最多电是多少。

思路:增加1个超级源点,和超级汇点。。把所给的发电站都和超级源点相连,把所给的消耗战都和超级汇点相连。。用EK求最大流。


模板有几个地方要注意。

1:start是编号最前的点,last是编号最后的点

模板默许last是m,根据需要要把m都改成编号最后的点的号码

#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> #include<math.h> #include<queue> #include<stdlib.h> #define inf 0x3f3f3f3f using namespace std; int map[1000][1000], path[1000], flow[1000], n, m,nc,np,start; int last; queue<int>que; int bfs() { int i, t; while (que.size())que.pop(); memset(path, ⑴, sizeof(path)); path[start] = 0; flow[start] = inf; que.push(start); while (que.size()) { int t = que.front(); que.pop(); if (t == last) break; for (i = 0; i <= n+1; i++) { if (i != start && path[i] == ⑴ && map[t][i]) { flow[i] = min(flow[t], map[t][i]); path[i] = t; que.push(i); } } } if (path[last] == ⑴)return ⑴; else return flow[n+1]; } int liu() { int maxflow = 0, temp, now, pre; while ((temp = bfs()) != ⑴) { maxflow += temp; now = last; while (now != start) { pre = path[now]; map[pre][now] -= temp; map[now][pre] += temp; now = pre; } } return maxflow; } char temp[20]; int main() { int i, j, k, from, to, cost,u,v,z; while (~scanf("%d%d%d%d", &n,&np,&nc,&m)) { memset(map, 0, sizeof(map)); for (i = 0; i < m; i++) { scanf("%s", temp); sscanf(temp, "(%d,%d)%d", &u, &v, &z); map[u+1][v+1] += z; } for (i = 0; i < np; i++) { scanf("%s", temp); sscanf(temp, "(%d)%d", &u, &z); map[0][u+ 1] += z; } for (i = 0; i < nc; i++) { scanf("%s", temp); sscanf(temp, "(%d)%d", &u, &z); map[u+1][n+1] += z; } last=n + 1; start = 0; cout << liu() << endl; } }


------分隔线----------------------------
------分隔线----------------------------

最新技术推荐