博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU-3549 Flow Problem 简单最大流
阅读量:6040 次
发布时间:2019-06-20

本文共 9848 字,大约阅读时间需要 32 分钟。

Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 1252    Accepted Submission(s): 606

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
 
  首先给定有多少组测试数据,然后给定一个顶点数,边数,然后给定一个单向的流量,求从第一个点到最后一个点的最大流量。注意该流网络是有向图,所以对于流量的控制是给定x 到 y 的流量是 z , 那么只需要给cap[x][y]+= z 即可。求解网络流就是在图中寻找增广路径,一条增广路径是指能够从起点到终点寻找到一条大于零的流量路径,并试图将其填充满,注意抑制一次寻找过程中回流现象,这是无意义的,因为在每一次队列的计算中,并没有及时的更新管道的容量,所以回流的形成会无限制的循环,肯定会超出内存,实质上也是一个死循环。
  代码如下:
#include 
#include
#include
#include
#include
#define INF 0x7f7f7f7f usingnamespace std; int flow[205][205], cap[205][205], p[205], c[205]; // f用来记录单前节点流,p用来记录父路径 void BFS( int&maxflow, int N ) {
queue
q; maxflow=0; bool finish=false; memset( flow, 0, sizeof( flow ) ); while( !finish ) { memset( p, 0, sizeof( p ) ); memset( c, 0, sizeof( c ) ); c[1]= INF, p[1]=-1; q.push( 1 ); while( !q.empty() ) { int pos= q.front(); q.pop(); for( int i=1; i<= N; ++i ) { if( !c[i]&& flow[pos][i]< cap[pos][i] ) // 如果该条边没有达到饱和或者形成回流 { c[i]= min( c[pos], cap[pos][i]- flow[pos][i] ); // 选取当前管道残留容量和截止到上一次的路径残留容量之间的较小者,可以看作是一种更新 // 即该增广路径的残留容量是有路径中残留容量最小的管道决定的 p[i]= pos; // 记录父亲路径 q.push( i ); } } } if( c[N]==0 ) { finish=true; } maxflow+= c[N]; int pos= N; while( !finish&& pos!=1 ) { flow[ p[pos] ][ pos ]+= c[N]; flow[ pos ][ p[pos] ]-= c[N]; pos= p[ pos ]; } } } int main() { int N, M, maxflow, ca=0, T; scanf( "%d", &T ); while( T-- ) { scanf( "%d %d", &M, &N ); memset( cap, 0, sizeof( cap ) ); for( int i=1; i<= N; ++i ) { int x, y, z; scanf( "%d %d %d", &x, &y, &z ); cap[x][y]+= z; } BFS( maxflow, M ); printf( "Case %d: %d\n", ++ca, maxflow ); } }

Dinic版 Dinic算法相比于上面的KM算法,采用了分层思想,以最短路寻找增广路,时间复杂度O(V2E)。

#include 
#include
#include
#include
#define INF 0x3fffffff#define RE(x) (x)^1#define S 1#define MAXN 20using namespace std;int N, M, head[MAXN], lvl[MAXN], idx;struct Edge{ int No, cap, next;}e[1005];void init(){ idx = -1; memset(head, 0xff, sizeof (head));}void insert(int x, int y, int z){ ++idx; e[idx].No = y; e[idx].cap = z; e[idx].next = head[x]; head[x] = idx; ++idx; e[idx].No = x; e[idx].cap = 0; e[idx].next = head[y]; head[y] = idx;}bool bfs(){ int pos; queue
q; memset(lvl, 0xff, sizeof (lvl)); lvl[S] = 0; q.push(S); while (!q.empty()) { pos = q.front(); q.pop(); for (int i = head[pos]; i!=-1; i = e[i].next) { if (e[i].cap > 0 && lvl[e[i].No]==-1) { lvl[e[i].No] = lvl[pos]+1; q.push(e[i].No); // 注意不要直接把“i”push进去了 } } } return lvl[N] != -1;}int dfs(int u, int flow){ if (u == N) { return flow; // 结束条件,把流推到了T节点(汇点) } int tf = 0, f; for (int i = head[u]; i != -1; i = e[i].next) { if (lvl[u]+1 == lvl[e[i].No] && e[i].cap > 0 && (f = dfs(e[i].No, min(e[i].cap, flow - tf)))) { e[i].cap -= f; e[RE(i)].cap += f; tf += f; } } if (tf == 0) { lvl[u] = -1; // tf等于零表示该点没有进行增广的能力,应及时将其高度赋值为-1,防止第二次被搜索到 } return tf;}int main(){ int T, ans, ca = 0, a, b, c; scanf("%d", &T); while (T--) { init(); ans = 0; scanf("%d %d", &N, &M); for (int i = 0; i < M; ++i) { scanf("%d %d %d", &a, &b, &c); insert(a, b, c); } while (bfs()) { ans += dfs(S, INF); } printf("Case %d: %d\n", ++ca, ans); } return 0; }

 寻找增广路径的sap算法,时间复杂度为O(V2E):

#include 
#include
#include
#include
#include
#define INF 0x3fffffff#define RE(x) (x)^1#define MAXN 20using namespace std;int lvl[MAXN], gap[MAXN], idx, head[MAXN], sink, source, N, M;// lvl用来记录每个节点离汇点的高度,gap数组用来记录高度值为i的个数gap[i]struct Edge{ int v, cap, next;}e[2010];void init(){ idx = -1; memset(head, 0xff, sizeof (head)); memset(lvl, 0, sizeof (lvl)); memset(gap, 0, sizeof (gap)); // 0xff == -1 (对于一个字节来说)}void insert(int a, int b, int c){ ++idx; e[idx].v = b, e[idx].next = head[a]; e[idx].cap = c, head[a] = idx;}int dfs(int u, int flow){ if (u == sink) { return flow; } int tf = 0, sf, mlvl = N-1; for (int i = head[u]; i != -1; i = e[i].next) { if (e[i].cap > 0) { // 只要还有边还有容量的话 if (lvl[u] == lvl[e[i].v]+1) { // 与dinic不同这个层数是反的 sf = dfs(e[i].v, min(flow-tf, e[i].cap)); e[i].cap -= sf; e[RE(i)].cap += sf; tf += sf; if (lvl[source] >= N) { // 可能在子递归中出现了断层或者不能再流的现象,这个时候要及时的退出 return tf; } if (tf == flow) { break; } } mlvl = min(mlvl, lvl[e[i].v]); } } if (tf == 0) { --gap[lvl[u]]; if (!gap[lvl[u]]) { lvl[source] = N; } else { lvl[u] = mlvl+1; ++gap[lvl[u]]; } } return tf;}int sap(){ int ans = 0; gap[0] = N; while (lvl[source] < N) { ans += dfs(source, INF); } return ans;}int main(){ int T, a, b, c, ca = 0, ans; scanf("%d", &T); while (T--) { init(); ans = 0; scanf("%d %d", &N, &M); for (int i = 0; i < M; ++i) { scanf("%d %d %d", &a, &b, &c); insert(a, b, c); insert(b, a, 0); } sink = N, source = 1; printf("Case %d: %d\n", ++ca, sap()); } return 0;}

 

网络流 sap非递归模板:

// HDU-4280 #include 
#include
#include
#define VM 100010#define EM 400010const int inf = 0x3f3f3f3f;struct E{ int to, frm, nxt, cap;}edge[EM];int head[VM],e,n,m,src,des;int dep[VM], gap[VM];void addedge(int cu, int cv, int cw){ edge[e].frm = cu; edge[e].to = cv; edge[e].cap = cw; edge[e].nxt = head[cu]; head[cu] = e++; edge[e].frm = cv; edge[e].to = cu; edge[e].cap = 0; edge[e].nxt = head[cv]; head[cv] = e++;}int que[VM];void BFS(){ memset(dep, -1, sizeof(dep)); memset(gap, 0, sizeof(gap)); gap[0] = 1; int front = 0, rear = 0; dep[des] = 0; que[rear++] = des; int u, v; while (front != rear) { u = que[front++]; front = front%VM; for (int i=head[u]; i!=-1; i=edge[i].nxt) { v = edge[i].to; if (edge[i].cap != 0 || dep[v] != -1) continue; que[rear++] = v; rear = rear % VM; ++gap[dep[v] = dep[u] + 1]; } }}int cur[VM],stack[VM];int Sap() //sap模板{ int res = 0; BFS(); int top = 0; memcpy(cur, head, sizeof(head)); int u = src, i; while (dep[src] < n) { if (u == des) { int temp = inf, inser = n; for (i=0; i!=top; ++i) if (temp > edge[stack[i]].cap) { temp = edge[stack[i]].cap; inser = i; } for (i=0; i!=top; ++i) { edge[stack[i]].cap -= temp; edge[stack[i]^1].cap += temp; } res += temp; top = inser; u = edge[stack[top]].frm; } if (u != des && gap[dep[u] -1] == 0) break; for (i = cur[u]; i != -1; i = edge[i].nxt) if (edge[i].cap != 0 && dep[u] == dep[edge[i].to] + 1) break; if (i != -1) { cur[u] = i; stack[top++] = i; u = edge[i].to; } else { int min = n; for (i = head[u]; i != -1; i = edge[i].nxt) { if (edge[i].cap == 0) continue; if (min > dep[edge[i].to]) { min = dep[edge[i].to]; cur[u] = i; } } --gap[dep[u]]; ++gap[dep[u] = min + 1]; if (u != src) u = edge[stack[--top]].frm; } } return res;}int main(){ int T, i; scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); int x, y; int Min = inf, Max = -inf; for (i=1; i<=n; ++i) //找出起点src 终点des { scanf("%d%d", &x, &y); if (x <= Min) { src = i; Min = x; } if (x >= Max) { des = i; Max = x; } } e = 0; memset(head, -1, sizeof(head)); int u, v, c; for (i=0; i!=m; ++i) { scanf("%d%d%d", &u, &v, &c); addedge(u,v,c); addedge(v,u,c); } int ans = Sap(); printf("%d\n", ans); } return 0;}

 

 

转载于:https://www.cnblogs.com/Lyush/archive/2011/08/08/2130660.html

你可能感兴趣的文章
HashMap的工作原理及HashMap和Hashtable的区别
查看>>
GregorianCalendar日历程序
查看>>
Sublime 中运行 Shell 、Python、Lua、Groovy...等各种脚本
查看>>
【Java集合源码剖析】ArrayList源码剖析
查看>>
linux的目录结构
查看>>
这次逻辑通了,
查看>>
HTMLHelper
查看>>
快速构建Windows 8风格应用29-捕获图片与视频
查看>>
OC语言Block和协议
查看>>
使用xpath时出现noDefClass的错误(找不到某个类)
查看>>
.Net规则引擎介绍 - REngine
查看>>
CSS3 transforms 3D翻开
查看>>
利用传入的Type类型来调用范型方法的解决方案
查看>>
Top命令内存占用剖析
查看>>
转 网络IO模型:同步IO和异步IO,阻塞IO和非阻塞IO
查看>>
求带分数(蓝桥杯)
查看>>
Retrofit 入门学习
查看>>
Spring Boot学习笔记
查看>>
laravel 集合接口
查看>>
java.exe进程来源排查录
查看>>