poj1459, network flow, maximum flow, multi-source point multi-Meeting point

Source: Internet
Author: User

Test instructions
Give several power stations, give several consumption stations, and give several forwarding points.
Power stations only generate electricity, consumption stations only consume electricity, the forwarding point is just to turn the power, and then give the conduct electricity ability of each transmission line.
Ask you what the maximum amount of electricity you can get from the consumption station.

Idea: Add a super source point, and a super sink point. Connect the given power station to the super source and connect the consumed war to the super sink. Use EK to find the maximum flow.


There are several places to watch out for templates.

1:start is the first point of the number, last is the final point of the number

Template default Last is M, according to the need to change m to the number of the final point number

#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));p Ath[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]);p ath[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", &amp(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, network flow, maximum flow, multi-source point multi-Meeting point

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.