HDU-1532-Drainage Ditches
来源:互联网 发布:安装postgis linux篇 编辑:程序博客网 时间:2024/06/03 04:39
ACM模版
描述
题解
基础的最大流问题,模版题,我用了一个十分成熟的 Dinic 算法模版,用了邻接表优化,代码略微长,但是效率还是不错的。GG
代码
#include <iostream>#include <cstring>#include <cstdio>/* * Dinic 最大流 O(V^2 * E) * INIT: ne=2; head[]置为0; addedge()加入所有弧; * CALL: flow(n, s, t); */#define typec int // type of costusing namespace std;const typec inf = 0x3f3f3f3f; // max of costconst typec MAXE = 410;const typec MAXN = 210;struct edge{ int x, y, nxt; typec c;} bf[MAXE];int ne, head[MAXN], cur[MAXN], ps[MAXN], dep[MAXN];void addedge(int x, int y, typec c){ // add an arc(x->y, c); vertex:0~n-1; bf[ne].x = x; bf[ne].y = y; bf[ne].c = c; bf[ne].nxt = head[x]; head[x] = ne++; bf[ne].x = y; bf[ne].y = x; bf[ne].c = 0; bf[ne].nxt = head[y]; head[y] = ne++; return ;}typec flow(int n, int s, int t){ typec tr, res = 0; int i, j, k, f, r, top; while (1) { memset(dep, -1, n * sizeof(int)); for (f = dep[ps[0] = s] = 0, r = 1; f != r;) { for (i = ps[f++], j = head[i]; j; j = bf[j].nxt) { if (bf[j].c && -1 == dep[k = bf[j].y]) { dep[k] = dep[i] + 1; ps[r++] = k; if (k == t) { f = r; break; } } } } if (-1 == dep[t]) { break; } memcpy(cur, head, n * sizeof(int)); for (i = s, top = 0; ;) { if (i == t) { for (k = 0, tr = inf; k < top; ++k) { if (bf[ps[k]].c < tr) { tr = bf[ps[f = k]].c; } } for (k = 0; k < top; ++k) { bf[ps[k]].c -= tr, bf[ps[k]^1].c += tr; } res += tr; i = bf[ps[top = f]].x; } for (j = cur[i]; cur[i]; j = cur[i] = bf[cur[i]].nxt) { if (bf[j].c && dep[i] + 1 == dep[bf[j].y]) { break; } } if (cur[i]) { ps[top++] = cur[i]; i = bf[cur[i]].y; } else { if (0 == top) { break; } dep[i] = -1; i = bf[ps[--top]].x; } } } return res;}int E, N;int main(){ while (cin >> E >> N) { ne = 2; memset(head, 0, sizeof(head)); int s, t; typec w; while (E--) { scanf("%d%d%d", &s, &t, &w); addedge(s - 1, t - 1, w); } typec ans = flow(N, 0, N - 1); printf("%d\n", ans); } return 0;}
参考
《最大流》
0 0
- HDU 1532 Drainage Ditches
- hdu 1532 Drainage Ditches
- HDU 1532 Drainage Ditches
- hdu 1532 Drainage Ditches
- HDU 1532 Drainage Ditches
- hdu 1532 Drainage Ditches
- hdu 1532Drainage Ditches
- hdu 1532 Drainage Ditches
- hdu 1532 Drainage Ditches
- HDU 1532 Drainage Ditches
- HDU 1532 Drainage Ditches
- HDU 1532 Drainage Ditches
- hdu 1532 Drainage Ditches
- 【HDU】 1532 Drainage Ditches
- HDU 1532 - Drainage Ditches
- HDU 1532Drainage Ditches
- HDU 1532Drainage Ditches
- HDU-1532-Drainage Ditches
- Python DataFrame设置/更改列表字段/元素类型
- ffmpeg 的 tbr tbc 和 tbn的意义
- reactor, proactor
- 循环抓取图片
- jquery排序 sort()
- HDU-1532-Drainage Ditches
- git 相关记录
- poj-2236 Wireless Network (并查集)
- JVM内存模型与性能调优
- 动态编程(Dynamic Programming, DP)
- CentOS下LNMP环境搭建
- GPUImage头文件功能定义
- group by 比 distinct 快
- 你必须了解的RecyclerView的五大开源项目-解决上拉加载、下拉刷新和添加Header、Footer等问题