Maximum flow algorithm Dinic HDU 1532

Source: Internet
Author: User

#include <iostream> #include <cstring> #include <queue> #include <cstdio>using namespace std;# Define v 205#define E 205#define INF 0x7ffffffstruct edge{int u, V, C, next;} e[e<<2];int N, m, S, T;int D[v], h    EAD[V], cnt;void adde (int u, int V, int c) {e[cnt].u = u;    E[CNT].V = v;    E[CNT].C = C;    E[cnt].next = Head[u];    Head[u] = cnt++;    e[cnt].u = v;    E[CNT].V = u;    e[cnt].c = 0;    E[cnt].next = Head[v]; HEAD[V] = cnt++;}    int BFs () {memset (d,-1, (n+3) *sizeof (int));    D[s] = 0;    Queue<int> Q;    Q.push (s); while (!        Q.empty ()) {int u = q.front ();        Q.pop ();            for (int i=head[u]; i!=-1; i=e[i].next) {int v = E[I].V;                if (d[v]==-1 && e[i].c>0) {D[v] = D[u] + 1;            Q.push (v); }}} return d[t]! =-1;}    int dfs (int x, int a) {if (x==t | | a==0) return A;    int flow = 0, F; for (int i=head[x]; I!=-1 && flow<a;        I=e[i].next) {int v = E[I].V;            if (d[v]==d[x]+1 && e[i].c>0) {int t = min (e[i].c, a-flow);            f = Dfs (V, t);            Flow + + F;            E[I].C-= f;        E[i^1].c + = f;    }} if (!flow) d[x] =-2; return flow;}    int dinic (int s, int t) {int flow = 0, F;    while (BFS ()) {while (f = DFS (S, INF)) flow + = f; } return flow;}    int main () {int u, V, c;        while (~SCANF ("%d%d", &m, &n)) {cnt = 0;        Memset (Head,-1, (n + 3) * sizeof (int));        s = 1;        t = N;            for (int i=0; i<m; i++) {scanf ("%d%d%d", &u, &v, &c);        Adde (U, V, c);    } printf ("%d\n", Dinic (1, N)); } return 0;}

Maximum flow algorithm Dinic HDU 1532

Related Article

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.