图论 网络流 HDU 1532 最大流
2016-10-13 00:48
330 查看
题意:最大流,n边,m点,求1点到m点 最大流。
题解:最大流模板
题解:最大流模板
#include <iostream> #include <cstdio> #include <math.h> #include <cstring> #include <algorithm> #include <vector> #include <queue> using namespace std; const int MAXNODE = 105 * 2; const int MAXEDGE = 100005; typedef int Type; const Type INF = 0x3f3f3f3f; struct Edge { int u, v; Type cap, flow; Edge() {} Edge(int u, int v, Type cap, Type flow) { this->u = u; this->v = v; this->cap = cap; this->flow = flow; } }; struct Dinic { int n, m, s, t; Edge edges[MAXEDGE]; int first[MAXNODE]; int next[MAXEDGE]; bool vis[MAXNODE]; Type d[MAXNODE]; int cur[MAXNODE]; vector<int> cut; void init(int n) { this->n = n; memset(first, -1, sizeof(first)); m = 0; } void add_Edge(int u, int v, Type cap) { edges[m] = Edge(u, v, cap, 0); next[m] = first[u]; first[u] = m++; edges[m] = Edge(v, u, 0, 0); next[m] = first[v]; first[v] = m++; } bool bfs() { memset(vis, false, sizeof(vis)); queue<int> Q; Q.push(s); d[s] = 0; vis[s] = true; while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int i = first[u]; i != -1; i = next[i]) { Edge& e = edges[i]; if (!vis[e.v] && e.cap > e.flow) { vis[e.v] = true; d[e.v] = d[u] + 1; Q.push(e.v); } } } return vis[t]; } Type dfs(int u, Type a) { if (u == t || a == 0) return a; Type flow = 0, f; for (int &i = cur[u]; i != -1; i = next[i]) { Edge& e = edges[i]; if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) { e.flow += f; edges[i^1].flow -= f; flow += f; a -= f; if (a == 0) break; } } return flow; } Type Maxflow(int s, int t) { this->s = s; this->t = t; Type flow = 0; while (bfs()) { for (int i = 0; i < n; i++) cur[i] = first[i]; flow += dfs(s, INF); } return flow; } void MinCut() { cut.clear(); for (int i = 0; i < m; i += 2) { if (vis[edges[i].u] && !vis[edges[i].v]) cut.push_back(i); } } } gao; int main() { int n,m; while(~scanf("%d%d",&n,&m)){//n 边 m点 int a,b,c; gao.init(n); //边 for(int i=1;i<=n;i++){ scanf("%d%d%d",&a,&b,&c); gao.add_Edge(a,b,c); } printf("%d\n",gao.Maxflow(1,m)); } return 0; }
相关文章推荐
- hdu 1532 网络流-最大流 DINIC
- hdu 4292 Food 【图论-网络流-最大流-Dinic】
- poj 1273 hdu 1532 网络流最大流 Dinic算法
- HDU - 1532 Drainage Ditches解题报告(网络流 最大流魔板题)
- HDU-1532-网络流最大流
- hdu 3605 Escape【图论-网络流-最大流-状态压缩】
- hdu 3416 Marriage Match IV 【图论-网络流-最短路+最大流(spfa + Dinic)】
- hdu 1532(poj 1273) Drainage Ditches (网络流·最大流)
- 【网络流】HDU 1532 Drainage Ditches(最大流Ford-Fulkersson)+HDU 3549 Flow Problem(最大流Dinic算法)
- hdu 3081 Marriage Match II 【图论-网络流-最大流+并查集】
- HDU 1532 Drainage Ditches (最大网络流)
- hdu 1532 Drainage Ditches(最大网络流)
- hdu1532 Drainage Ditches网络流——最大流 EK算法模板
- hdu 2732 Leapin' Lizards 【图论-网络流-最大流】
- hdu 4289 Control 【图论-网络流-最大流-拆点】
- HDU 1532 Drainage Ditches (最大网络流)
- hdu 1532 Drainage Ditches(网络流之最大流EdmondsKarp)
- hdu 1532 Drainage Ditches(图论:最大流增广路)
- HDU 1532 Drainage Ditches (最大网络流)
- Hdu 1569 方格取数(2) (网络流最大点权独立集)