【模板】【网络流】Dinic
2015-03-18 20:01
302 查看
/* 唐代杜荀鹤 《小松》 自小刺头深草里,而今渐觉出蓬蒿。 时人不识凌云木,直待凌云始道高。 */ #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <utility> #include <iomanip> #include <string> #include <cmath> #include <queue> #include <assert.h> #include <map> #include <ctime> #include <cstdlib> #include <stack> #include <set> #define LOCAL const int INF = 0x7fffffff; const int MAXN = 100000 + 10; const int maxnode = 20000 * 2 + 200000 * 20; const int MAXM = 50000 + 10; const int MAX = 100; using namespace std; struct Edge{ int u, v, c, f; void init(int a, int b, int d, int e){ u = a;v = b; c = d;f = e; } }edges[MAXM]; int next[MAXN], M, cur[MAXN], vis[MAXN]; int dist[MAXN], head[MAXN], n, m, sta, end; void addEdge(int u, int v, int c){ edges[M].init(u, v, c, 0);M++; edges[M].init(v, u, 0, 0);//反向边 next[M - 1] = head[u]; head[u] = M - 1; next[M] = head[v]; head[v] = M++; return; } void init(){ M = 0;//总边数 memset(head, -1, sizeof(head)); scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++){ int u, v, c; scanf("%d%d%d", &u, &v, &c); addEdge(u, v, c); } sta = 1; end = n; } bool bfs(){ memset(vis, 0, sizeof(vis)); queue<int>Q; dist[sta] = 0; vis[sta] = 1; Q.push(sta); while (!Q.empty()){ int u = Q.front(); Q.pop(); for (int i = head[u]; i != -1; i = next[i]){ int e = i, v = edges[e].v; if (vis[v]) continue; if (edges[e].c > edges[e].f){ vis[v] = 1; dist[v] = dist[u] + 1; Q.push(v); } } } return vis[end]; } int dfs(int x, int a){ if (x == end || a == 0) return a; int flow = 0, f; if (cur[x] == -1) cur[x] = head[x]; for (int &i = cur[x]; i != -1; i = next[i]){ int e = i, v = edges[i].v; if (dist[v] == dist[x] + 1 && (f = dfs(v, min(edges[e].c - edges[e].f, a))) > 0){ flow += f; a -= f; edges[e].f += f; edges[e ^ 1].f -= f; if (a == 0) break; } } return flow; } int Dinic(){ int flow = 0; while (bfs()){ memset(cur, -1, sizeof(cur)); flow += dfs(sta, INF); } return flow; } int main(){ #ifdef LOCAL freopen("data.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif init(); printf("%d", Dinic()); //printf("%d", (a == c)); return 0; }
View Code
相关文章推荐
- usaco Drainage Ditches(网络流dinic模板)
- 网络流Dinic模板
- 模板 网络流 dinic
- 网络流——增广路算法(dinic)模板 [BeiJing2006]狼抓兔子
- 网络流dinic模板 hdu3549
- HDU 1532 Drainage Ditches 网络流模板题(Dinic)
- dinic 网络流模板
- HDU 3549 适合网络流入门(内含sap模板和Dinic模板)Flow Problem
- (dinic增广网络流模板)(sizeof用法)poj-3469-Dual Core CPU
- poj 3308 Paratroopers(网络流 最小割 dinic模板)
- 网络流dinic模板
- 网络流dinic模板
- {模板}网络流Dinic
- 模板---图论:网络流增广路算法 Dinic
- 网络流dinic模板
- 初识网络流(EK and Dinic 模板)
- 【模板】网络流-最大流模板(Dinic)
- 网络流模板--dinic
- hdu 1532 Drainage Ditches(网络流dinic模板)
- poj 1459 Power Network(网络流 最大流 dinic模板题)