HDU 3549 flow problem maximum network flow problem edmonds_karp Algorithm

Source: Internet
Author: User

Link: HDU 3549 Flow Problem

Flow Problem Time Limit: 5000/5000 MS (Java/others) memory limit: 65535/32768 K (Java/Others)
Total submission (s): 8218 accepted submission (s): 3824


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 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)
Outputfor each test cases, you should output 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)
Question:

You can use the ek algorithm to find the maximum network stream.


Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <queue> using namespace STD; # define maxn 20 # define INF 0x3f3f3fint ans, S, T, N; int A [maxn], pre [maxn]; int flow [maxn] [maxn]; int CAP [maxn] [maxn]; void edmonds_karp () {queue <int> q; memset (flow, 0, sizeof (flow); ans = 0; while (1) {memset (A, 0, sizeof (a); A [s] = inf; q. push (s); While (! Q. empty () // BFS find the increment path {int u = Q. front (); q. pop (); For (INT v = 1; v <= N; V ++) if (! A [v] & Cap [u] [v]> flow [u] [v]) {pre [v] = u; q. push (V); A [v] = min (A [u], Cap [u] [v]-flow [u] [v]);} if (A [T] = 0) break; For (INT u = T; u! = S; u = pre [u]) // improved network stream {flow [pre [u] [u] + = A [T]; flow [u] [pre [u]-= A [T];} ans + = A [T];} int main () {// freopen ("hdu_3549.txt", "r", stdin); int T, M, U, V, C; scanf ("% d", & T ); for (int cas = 1; CAS <= T; CAS ++) {scanf ("% d", & N, & M); memset (Cap, 0, sizeof (CAP); While (M --) {scanf ("% d", & U, & V, & C ); cap [u] [v] + = C;} s = 1, t = n; edmonds_karp (); printf ("case % d: % d \ n", Cas, ans);} return 0 ;}





HDU 3549 flow problem maximum network flow problem edmonds_karp Algorithm

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.