设为首页 加入收藏

TOP

HDU 3549 Flow Problem 网络最大流问题 Edmonds_Karp算法
2015-07-20 17:37:52 来源: 作者: 【 】 浏览:2
Tags:HDU 3549 Flow Problem 网络 最大 问题 Edmonds_Karp 算法

?

?

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 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

Author HyperHexagon
Source HyperHexagon's Summer Gift (Original tasks)
题意:

?

纯求网络最大流,用EK算法就可以实现。

?

代码:

?

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       using namespace std; #define maxn 20 #define INF 0x3f3f3f3f int ans, s, t, n; int a[maxn], pre[maxn]; int flow[maxn][maxn]; int cap[maxn][maxn]; void Edmonds_Karp() { queue
      
        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找增光路径 { 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]) //改进网络流 { 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%d, &n, &m); memset(cap, 0, sizeof(cap)); while(m--) { scanf(%d%d%d, &u, &v, &c); cap[u][v] += c; } s = 1, t = n; Edmonds_Karp(); printf(Case %d: %d , cas, ans); } return 0; } 
      
     
    
   
  


?

?

?

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 2045 不容易系列之(3)―― LE.. 下一篇HDU 1532 Drainage Ditches 最大..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)