HDU 3549 Flow problem (Max Stream)

Source: Internet
Author: User
Tags cas

Flow Problem

Time limit:5000/5000 MS (java/others) Memory limit:65535/32768 K (java/others)
Total submission (s): 12625 Accepted Submission (s): 6004


problem DescriptionNetwork Flow is a well-known difficult problem for acmers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
InputThe first line of input contains an integer T, denoting the number of the test cases.
For each test case, the first line contains integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <=, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is a edge from X to Y and the capacity of it is C. (1 & lt;= X, Y <= N, 1 <= C <=)
Outputfor each test cases, you should the output of the maximum flow from source 1 to sink N.
Sample Input
23 21 2 12 3 13 31 2 12 3 11 3 1

Sample Output
Case 1:1case 2:2

AuthorHyperhexagon
SourceHyperhexagon ' s Summer Gift (Original tasks)


Test instructions: n points, M bars have a forward edge, each edge has a capacity C; 1 to n is the maximum flow.

Analysis: This is a very basic, very classical maximum flow problem, the calculation is the PFS algorithm template problem.

Ps: Network Flow Learning

<span style= "FONT-SIZE:18PX;" > #include <iostream> #include <cstdio> #include <cstring> #include <stack> #include < queue> #include <map> #include <set> #include <vector> #include <cmath> #include < Algorithm>using namespace Std;const Double eps = 1e-6;const double pi = ACOs ( -1.0); const int INF = 1e9;const int MOD = 1e9+7; #define LL Long Long#define CL (A, B) memset (A,b,sizeof (a)) #define Lson (i<<1) #define Rson ((i<<1) |    Define N 1005int pre[n];///saves the precursor vertex int mat[n][n];///Residual network capacity of the points on the path through bool Vis[n];int s,t;int n,m;bool BFS () {int cur;    Queue<int> Q;    CL (Vis, 0);    CL (pre, 0);    Vis[s] = true;    Q.push (s); while (!        Q.empty ()) {cur = Q.front ();        Q.pop (); if (cur = = t) return true;///if it has reached T, indicating that a grace path has been found, return for (int i=1; i<=n; i++) {if (!vis[i] &                & Mat[cur][i])////Only the residual capacity is greater than 0 o'clock to exist side {Q.push (i);           Pre[i] = cur;     Vis[i] = true; }}} return false;}    int Max_flow () {int ans = 0;        while (1) {if (!bfs ()) return ans;///cannot find a grace path indicating that it is already the maximum stream, return int Min = INF;        for (int i=t; i!=s; i=pre[i])////through the pre[] array to find the edges on the path, the minimum value of the residual capacity min = min (min, mat[pre[i]][i]);            for (int i=t; i!=s; I=pre[i]) {mat[pre[i]][i] = Min;        Mat[i][pre[i]] + = Min;    } ans + = Min;    }}int Main () {int t,cas=1;    int u,v,c;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        s = 1;        t = N;        CL (Mat, 0);            while (m--) {scanf ("%d%d%d", &u,&v,&c);        MAT[U][V] + = C;    } printf ("Case%d:%d\n", Cas++,max_flow ()); } return 0;} </span>


HDU 3549 Flow problem (Max Stream)

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.