首页 > 代码库 > HDU 3549 Flow problem
HDU 3549 Flow problem
Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 8144 Accepted Submission(s): 3790
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)
Recommend
zhengfeng | We have carefully selected several similar problems for you: 1532 3572 3416 3081 3491
第一次做网络流的题目,求一个最大流
用 Dinic 算法 ,有向图 , 反向弧的容量设为 0 。
每次用 bfs 构建层次图, 若果 vis[ t ] == 1 的话证明图上还是有增广路 。
然后用 递归dfs 寻找增广路 。
dfs 过程除了当前结点 x 以外 , 还需要传入 一个表示“ 目前为止所有弧的最小残量 ” 的 a ,
当 x 为汇点 或者 a = 0的时候终止dfs 过程 ,否则多路增广 。
有一个重要的优化 就是保存每一个结点 x 正在考虑的 弧 cur[x] , 以避免重复计算 。( dfs 更新增广路之前把eh 复制给 cur 就ok 了)
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int INF = 1e9 ;const int N = 20;const int M = 2010;bool vis[N];int eh[N],et[M],nxt[M],ef[M],ec[M],etot;int s , t , n , m;int d[N],cur[N];void init(){ memset( eh, -1 , sizeof eh) ; etot =0 ;}void addedge( int u , int v , int c ,int f){ et[etot] = v ; nxt[etot] = eh[u]; ef[etot] = f; ec[etot] = c ; eh[u] = etot++; et[etot] = u ; nxt[etot] = eh[v]; ef[etot] = f; ec[etot] = 0 ; eh[v] = etot++;}bool bfs (){ memset( vis , 0 , sizeof vis); queue< int >que; que.push(s) ; d[s] = 0; vis[s] =1 ; while( !que.empty() ) { int u = que.front(); que.pop(); for( int i = eh[ u ] ; ~i ; i = nxt[i] ){ int v = et[i] , c = ec[i] , f = ef[i]; if( !vis[v] && c > f){ vis[v] = 1 ; d[v] = d[u] + 1; que.push(v); } } } return vis[t];}int dfs (int x ,int a){ if ( x == t || a == 0 ){ return a ; } int flow = 0 , F; for( int &i = cur[x] ; ~i ; i = nxt[i] ){ int v = et[i] , c = ec[i] , &f = ef[i]; if( d[x] + 1 == d[v] && ( F = dfs (v, min( a, c - f))) > 0) { f += F; ef[ i ^ 1 ] -= F; flow += F; a -= F; if( a == 0 )break; } } return flow;}int MF(){ int flow = 0 ; while( bfs() ){ memcpy( cur , eh , sizeof eh ); flow += dfs(s , INF); } return flow;}void run(){ int u,v,c; scanf("%d%d",&n,&m); init(); while(m--){ scanf("%d%d%d",&u, &v, &c); addedge( u ,v , c , 0 ); } s = 1 , t = n; printf("%d\n",MF());}int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL int _ , cas = 1 ; scanf("%d",&_); while(_--){ printf("Case %d: ",cas++); run(); } return 0;}
HDU 3549 Flow problem
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。