Network flow HDU 3549 flow problem

Source: Internet
Author: User

Network flow HDU 3549 flow problem


Title: http://acm.hdu.edu.cn/showproblem.php?pid=3549


Using the augmented path algorithm to solve the problem, there are two points to note:

1. Each time the augmentation, the reverse flow will be more line, at first did not notice, WA a few times ORZ

2. For the input data, the capacity should be updated incrementally.


Adjacency Matrix Storage # include <bits/stdc++.h>using namespace std;const int INF = 0x7fffffff;const int M = 15+5;int C[m][m];    Capacityint F[m][m];    Flowint Flow[m];       Current Flowsint p[m];    path//augmented path algorithm long long maxflow (int n) {long Long cnt = 0;        while (true) {queue<int> q;        Q.push (1);        memset (flow, 0, sizeof (flow));        FLOW[1] = INF;            while (!q.empty ()) {int u = q.front ();            Q.pop (); for (int v=1; v<=n; ++v) {if (U = v && c[u][v] > F[u][v] &&!flow[v]/* Avoid                    Non-loopback */) {Q.push (v);                    Flow[v] = min (Flow[u], c[u][v]-f[u][v]);                P[V] = u;  }}} if (flow[n]==0) break;        No augmentation, exit//For augmented//cout<<flow[n]<<endl;            for (int v=n; v! = 1; V=p[v]) {F[p[v]][v] + = Flow[n]; F[v][p[v]]-= flow[n]; Reverse update, there is inflow, outflow naturally should also increase!    WA a few times to find Orz} cnt + = Flow[n]; } return CNT;}    int main (void) {//freopen ("In.txt", "R", stdin);    int icase = 1;    int t = 0;    cin>>t;        while (t--) {memset (c, 0, sizeof (c));        memset (f, 0, sizeof (f));        int n, m;        scanf ("%d%d", &n, &m);            for (int i=0; i<m; ++i) {int s,e,v;            scanf ("%d%d%d", &s, &e, &v);       C[s][e] + = V; Incoming capacity to accumulate}//special case Direct judgment if (n <= 1 | | m < 1) {printf ("Case%d:0\n", icase            ++);        Continue        A long long cnt = Maxflow (n);    printf ("Case%d:%lld\n", icase++, CNT); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Http://blog.csdn.net/core__code

Network flow HDU 3549 flow 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.