poj 1273 Drainage Ditches
2015-08-31 14:43
447 查看
原题链接:http://poj.org/problem?id=1273
最大流模板题。。
最大流模板题。。
[code]#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<vector> #include<map> using std::min; using std::find; using std::sort; using std::pair; using std::vector; using std::multimap; #define pb(e) push_back(e) #define sz(c) (int)(c).size() #define mp(a, b) make_pair(a, b) #define all(c) (c).begin(), (c).end() #define iter(c) __typeof((c).begin()) #define cls(arr, val) memset(arr, val, sizeof(arr)) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define rep(i, n) for(int i = 0; i < (int)n; i++) #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i) const int N = 210; const int INF = 0x3f3f3f3f; struct Ford_Flukerson { struct edge { int to, cap, next, rev; }G[N << 1]; bool vis ; int tot, head ; inline void init() { tot = 0, cls(head, -1); } inline void add_edge(int u, int v, int cap) { G[tot] = (edge){ v, cap, head[u], tot + 1 }; head[u] = tot++; G[tot] = (edge){ u, 0, head[v], tot - 1 }; head[v] = tot++; } inline void built(int m) { int u, v, f; while(m--) { scanf("%d %d %d", &u, &v, &f); add_edge(u, v, f); } } inline int dfs(int u, int t, int f) { if(u == t) return f; vis[u] = true; for(int i = head[u]; ~i; i = G[i].next) { edge &e = G[i]; if(!vis[e.to] && e.cap > 0) { int d = dfs(e.to, t, min(e.cap, f)); if(d > 0 ) { e.cap -= d; G[e.rev].cap += d; return d; } } } return 0; } inline void max_flow(int n) { int flow = 0; while(true) { cls(vis, false); int f = dfs(1, n, INF); if(!f) break; flow += f; } printf("%d\n", flow); } }go; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w+", stdout); #endif int n, m; while(~scanf("%d %d", &m, &n)) { go.init(); go.built(m); go.max_flow(n); } return 0; }
相关文章推荐
- leecode 每日解题思路 127-Factorial Trailing Zeroes
- (null): Linker command failed with exit code 1 (use -v to see invocation)
- 为什么要内存对齐 Data alignment: Straighten up and fly right
- INSTALL_FAILED_CONFLICTING_PROVIDER错误解决方法
- 将 paired count 和unpaired count 相加
- rust用struct保存特定trait的引用
- 不同RAID模式的优缺点
- win7旗舰版卸载“获取Windows10”升级助手
- leetcode 172 Factorial Trailing Zeroes(难易度:Easy)
- 详解C语言中的wait()函数和waitpid()函数
- 【LeetCode】70. Climbing Stairs
- [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)错误原因
- Linux线程-互斥锁pthread_mutex_t http://blog.csdn.net/zmxiangde_88/article/details/7998458
- 减少页面回流与重绘(Reflow & Repaint)
- foxmail邮箱 邮件地址信息 以及邮件内容信息的导入导出
- XML 元素 http://blog.csdn.net/chaiyu2002/article/details/5967186
- xml节点和元素的关系 http://blog.csdn.net/chaiyu2002/article/details/5967186
- foxmail邮件太多,需要经常手工清理邮件怎么办
- UVALive - 2197 Paint the Roads(费用流)
- epoll详细工作原理 http://blog.csdn.net/hdutigerkin/article/details/7517390