HDOJ3549-Flow Problem(最大流)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDOJ3549-Flow Problem(最大流)相关的知识,希望对你有一定的参考价值。

Problem 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: 1
Case 2: 2

 技术分享

Code:

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define CLR(x , v)      memset(x , v , sizeof(x))
 5 
 6 static const int OO = 0x3fffffff;
 7 
 8 static const int MAXN = 1e3 + 10;
 9 
10 int n , m;
11 int cap[MAXN][MAXN];
12 int flow[MAXN][MAXN];
13 int add[MAXN];
14 queue<int> q;
15 int pre[MAXN];
16 int main()
17 {
18     int t;
19     scanf("%d" , &t);
20     for(int c = 1 ; c <= t ; ++c)
21     {
22         int ans = 0;
23         CLR(cap , 0);
24         CLR(flow , 0);
25         CLR(pre , 0);
26         while(!q.empty())
27             q.pop();
28         scanf("%d%d" , &n , &m);
29         for(int i = 1 ; i <= m ; ++i)
30         {
31             int a , b , c;
32             scanf("%d%d%d" , &a , &b , &c);
33             cap[a][b] += c;
34         }
35 
36         for(;;)
37         {
38             CLR(add , 0);
39             add[1] = OO;
40             q.push(1);
41             while(!q.empty())
42             {
43                 int u = q.front();
44                 q.pop();
45                 for(int i = 1 ; i <= n ; ++i)
46                 {
47                     if(!add[i] && cap[u][i] > flow[u][i])
48                     {
49                         pre[i] = u;
50                         q.push(i);
51                         add[i] = min(add[u] , cap[u][i] - flow[u][i]);
52                     }
53                 }
54             }
55 
56             if(!add[n])
57                 break;
58 
59             for(int i = n ; i != 1 ; i = pre[i])
60             {
61                 flow[pre[i]][i] += add[n];
62                 flow[i][pre[i]] -= add[n];//反向弧
63             }
64 
65             ans += add[n];
66         }
67 
68         printf("Case %d: %d\\n" , c , ans);
69     }
70 }
View Code

 

以上是关于HDOJ3549-Flow Problem(最大流)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 3549 Flow Problem(最大流模板)

HDU-3549-Flow Problem

hdu 3549 Flow Problem (最大流入门题)

题解报告:hdu 3549 Flow Problem(最大流入门)

hdu 3549 Flow Problem

hdu 3549 Flow Problem (Dinic)