POJ 1459 Power Network, maximum flow, multi-source multi-sinks

Source: Internet
Author: User

Click to open link



Multi-source multi-sink maximum flow, virtual One source point s ' and a meeting point T ', the original source point, the sink point to their edge.


#include <cstdiO> #include <cstring> #include <iostream> #include <algorithm> #include < Queue> #include <vector>using namespace Std;const int maxn = + 5;const int INF = 100000000;struct edge{int From, to, cap, flow;};    struct dinic{int n, m, S, t;    Vector<edge> edges;    Vector<int> G[MAXN];    BOOL VIS[MAXN];    int D[MAXN];    int CUR[MAXN];        void init (int n) {this->n = n;        for (int i=0; i<=n; ++i) g[i].clear ();    Edges.clear ();        } void Addedge (int from, int to, int caps) {Edges.push_back (Edge) {from, to, Cap, 0});        Edges.push_back (Edge) {To, from, 0, 0});        m = Edges.size ();        G[from].push_back (m-2);    G[to].push_back (m-1);        } bool BFS () {memset (Vis, 0, sizeof vis);        Queue<int> Q;        Q.push (s);        Vis[s] = 1;        D[s] = 0; while (! Q.empty ()) {int x = Q.front ();            Q.pop (); for (int i=0; i<g[x].sizE ();                ++i) {edge& e = edges[g[x][i]];                    if (!vis[e.to] && e.cap > E.flow) {vis[e.to] = 1;                    D[e.to] = d[x] + 1;                Q.push (e.to);    }}} return vis[t];        } int DFS (int x, int a) {if (x = = T | | a = = 0) return A;        int flow = 0, F;            for (int& i = cur[x]; i<g[x].size (); ++i) {edge& e = edges[g[x][i]];                if (d[x] + 1 = = D[e.to] && (F=dfs (e.to, Min (a,e.cap-e.flow))) >0) {e.flow + = f;                Edges[g[x][i]^1].flow-= f;                Flow + + F;                A-= f;            if (a==0) break;    }} return flow;        } int Maxflow (int s, int t) {this->s = s; this->t =t;        int flow = 0;            while (BFS ()) {memset (cur, 0, sizeof cur);     Flow + = DFS (s, INF);   } return flow;    }};D inic Solver;int main () {int n, NP, NC, M;    Char ch;    int x, y, Z;        while (~SCANF ("%d%d%d%d", &n, &AMP;NP, &AMP;NC, &m)) {int s = 0, t = n+1;        Solver.init (t+2);            for (int i=1; i<=m; ++i) {cin>>ch>>x>>ch>>y>>ch>>z;            if (x==y) continue; Solver.        Addedge (x+1, y+1, z);            } for (int i=1; i<=np; ++i) {cin>>ch>>x>>ch>>z; Solver.        Addedge (S, x+1, z);            } for (int i=1; i<=nc; ++i) {cin>>ch>>x>>ch>>z; Solver.        Addedge (x+1, T, z); } int ans = solver.        Maxflow (s,t);    printf ("%d\n", ans); } return 0;}


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.