Poj1459-power network-Network Stream-Maximum flow (EK template problem)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=1459

Well, it's actually a template problem ...

But it's such a long story. Orz ... Various speculate topics mean ....

For the benefit of everyone, I put the meaning of the topic data, you do not have to look at this hateful English ...

The topic meaning: gives several power stations, gives several consumption stations, gives again 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.

First enter four data, indicating the number of nodes, the number of power stations, the number of consumption stations, and the number of forwarding points.

Next is the M forwarding point, (A, B) c. Represents the maximum traffic that can transfer c from point A to

Then is the NP power station, (a) B, which indicates that the power station a max can generate B capacity.

Finally, the data NC consumes a station. (a) B. Indicates that the consumption station a can consume a maximum of B capacity.

Finally ask you, from the power station, to the consumption stations, the maximum can transfer how much traffic.

Obviously, this is a maximum flow template problem, we just add the super source and meeting point. Connect the given power station to the super source and connect the consumed war to the super sink. The problem will be solved.

#include <iostream> #include <string> #include <cstdio> #include <cstring> #include <map> #include <queue> #include <cmath> #include <stack> #include <set> #include <vector># Include<algorithm> #define LL Long long#define inf 1<<29#define s (a) scanf ("%d", &a) #define CL (A, B)  memset (A,b,sizeof (a)) using namespace Std;const int n=210;int n,m,np,nc,a,b,c;int start,endd;int map[n][n];    rated maximum capacity; int path[n];    The path of the transmission; int Flow[n];    The current traffic that can be transmitted by this path; int BFs () {//wide search, looking for feasible road strength; queue<int>q;    CL (path,-1);    Path[start]=0;flow[start]=inf;    Q.push (start);        while (!q.empty ()) {int T=q.front ();        Q.pop ();        if (t==endd) break; for (int i=1;i<=n;i++) {if (I!=start&&path[i]==-1&&map[t][i]) {flow[i]=flow[t]& Gt Map[t][i]?                MAP[T][I]:FLOW[T];                Q.push (i);            path[i]=t;   }}} if (Path[endd]==-1) return-1; Indicates no viablepath; else return FLOW[ENDD];}    int Edmonds_karp () {//Adds reverse edge to each path; int max_flow=0,step,now,pre;        while ((Step=bfs ())!=-1) {//did not find a feasible path to update the maximum flow and his opposite edge; max_flow+=step;        NOW=ENDD;           while (Now!=start) {Pre=path[now];           Map[pre][now]-=step;           Map[now][pre]+=step;        Now=pre; }} return max_flow;}    int main () {Cin.sync_with_stdio (false);        while (cin>>n>>np>>nc>>m) {CL (map,0);        Char D;            for (int i=0;i<m;i++) {cin>>d>>a>>d>>b>>d>>c;        Map[a+1][b+1]=c;        } int tmp=n+1;           Super meeting point; n+=2;     Total number of nodes U; m+= (NP+NC);            Total number of sides V; for (int i=0;i<np;i++) {cin>>d>>a>>d>>b;        Map[0][a+1]=b;            } for (int i=0;i<nc;i++) {cin>>d>>a>>d>>b;        Map[a+1][tmp]=b; } start=0;endd=tmp    Cout<<edmonds_karp () <<endl; } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Poj1459-power network-Network Stream-Maximum flow (EK template problem)

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.