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

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

思路:增加一个超级源点,和超级汇点。。把所给的发电站都和超级源点相连,把所给的消耗战都和超级汇点相连。。用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 0x3f3f3f3fusing 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, -1, 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] == -1 && map[t][i]){flow[i] = min(flow[t], map[t][i]);path[i] = t;que.push(i);}}}if (path[last] == -1)return -1;else return flow[n+1];}int liu(){int maxflow = 0, temp, now, pre;while ((temp = bfs()) != -1){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;}}

也只有懂的接受自己的失败,才能更好的去发挥自身优势,也才能够更好的去实现自我;

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

相关文章:

你感兴趣的文章:

标签云: