Flow ProblemTime limit:5000 MsMemory limit:32768kb64bit Io format:% I64d & % i64u submit status
Description
Network 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.
Input
The first line of input contains an integer T, denoting the number of test cases.
For each test case, the first line contains two integers n and M, denoting the number of vertexes and edges in the graph. (2 <= n <= 15, 0 <= m <= 1000)
Next m lines, each line contains three integers x, y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
Output
For each test cases, you should output the maximum flow from source 1 to sink n.
Sample Input
2 3 2 1 2 1 2 3 1 3 3 1 2 1 2 3 1 1 3 1
Sample output
Case 1: 1Case 2: 2
The question is relatively simple, and the entry level is the biggest stream problem.
# Include <cstdio>
# Include <algorithm>
# Include <queue>
# Include <string. h>
# Define maxn 21
# Define INF 1 <28
Using namespace STD;
Int CAP [maxn] [maxn], pre [maxn], vis [maxn], maxflow [maxn];
Int maxflow;
Int n, m, I, J;
Void Init ()
{
Int x, y, z;
Memset (Cap, 0, sizeof (CAP ));
Memset (PRE, 0, sizeof (pre ));
Scanf ("% d", & N, & M );
For (I = 1; I <= m; I ++)
{
Scanf ("% d", & X, & Y, & Z );
Cap [x] [Y] + = z; // duplicate edge
}
}
Int min (int A, int B)
{
Return A> B? B:;
}
Int main ()
{
Int T, U, k = 1;
Scanf ("% d", & T );
While (t --)
{
Init ();
Queue <int> q;
Maxflow = 0;
While (1)
{
Memset (maxflow, 0, sizeof (maxflow ));
Memset (VIS, 0, sizeof (VIS ));
Maxflow [1] = inf;
Q. Push (1 );
While (! Q. Empty ())
{
U = Q. Front ();
Q. Pop ();
For (INT v = 1; v <= N; V ++)
{
If (! Vis [v] & Cap [u] [v]> 0)
{
Vis [v] = 1;
Pre [v] = u;
Q. Push (v );
Maxflow [v] = maxflow [u] <CAP [u] [v]? Maxflow [u]: CAP [u] [v];
}
}
}
If (maxflow [N] = 0) break;
For (INT I = N; I! = 1; I = pre [I])
{
Cap [pre [I] [I]-= maxflow [N];
Cap [I] [pre [I] + = maxflow [N];
}
Maxflow + = maxflow [N];
}
Printf ("case % d: % d \ n", K ++, maxflow );
}
Return 0;
} View code