hdu 1532 Drainage Ditches 最大流 水题
来源:互联网 发布:php class实例化 编辑:程序博客网 时间:2024/05/20 18:53
//0MS268K#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <vector>#include <algorithm>using namespace std;struct edge{ int to, cap, rev; edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}};const int inf = 0x7fffffff;const int maxv = 200;const int maxe = 200;vector<edge> G[maxv+5];int level[maxv+5];int iter[maxv+5];inline void AddEdge(int from, int to, int cap){ G[from].push_back(edge(to, cap, G[to].size())); G[to].push_back(edge(from, 0, G[from].size() - 1));}void bfs(int s){ memset(level, -1, sizeof(level)); queue<int> que; level[s] = 0; que.push(s); while(!que.empty()){ int v = que.front(); que.pop(); for(int i=0; i<G[v].size(); i++){ edge& e = G[v][i]; if(e.cap > 0 && level[e.to] < 0){ level[e.to] = level[v] + 1; que.push(e.to); } } }}int dfs(int v, int t, int f){ if(v == t) return f; for(int& i=iter[v]; i<G[v].size(); i++){ edge& e = G[v][i]; if(e.cap > 0 && level[v] < level[e.to]){ int d = dfs(e.to, t, min(f, e.cap)); if(d > 0){ e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0;}int MaxFlow(int s, int t){ int flow = 0; while(1){ bfs(s); if(level[t] < 0) return flow; memset(iter, 0, sizeof(iter)); int f; while((f = dfs(s, t, inf)) > 0){ flow += f; } }}int main(){ int N, M; while(~scanf("%d%d", &N, &M)){ //N是边数,M是顶点数 for(int i=1; i<=N; i++) G[i].clear(); int X, Y, C; for(int i=1; i<=N; i++){ scanf("%d%d%d", &X, &Y, &C); AddEdge(X, Y, C); } printf("%d\n", MaxFlow(1, M)); } return 0;}另一种数据结构
//15MS280K#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <vector>#include <algorithm>using namespace std;struct edge{ int to, cap, flow; edge(int to, int cap, int flow) : to(to), cap(cap), flow(flow) {}};const int inf = 0x7fffffff;const int maxv = 200;const int maxe = 200;vector<int> G[maxv+5];vector<edge> es;int level[maxv+5];int iter[maxv+5];inline void AddEdge(int from, int to, int cap){ es.push_back(edge(to, cap, 0)); G[from].push_back(es.size() - 1); es.push_back(edge(from, 0, 0)); G[to].push_back(es.size() - 1);}void bfs(int s){ memset(level, -1, sizeof(level)); queue<int> que; level[s] = 0; que.push(s); while(!que.empty()){ int v = que.front(); que.pop(); for(int i=0; i<G[v].size(); i++){ edge& e = es[G[v][i]]; if(e.flow < e.cap && level[e.to] < 0){ level[e.to] = level[v] + 1; que.push(e.to); } } }}int dfs(int v, int t, int rem){ if(v == t) return rem; for(int& i=iter[v]; i<G[v].size(); i++){ edge& e = es[G[v][i]]; if(e.flow < e.cap && level[v] < level[e.to]){ int d = dfs(e.to, t, min(rem, e.cap - e.flow)); if(d > 0){ e.flow += d; es[G[v][i]^1].flow -= d; return d; } } } return 0;}int MaxFlow(int s, int t){ int flow = 0; while(true){ bfs(s); if(level[t] < 0) return flow; memset(iter, 0, sizeof(iter)); int f; while((f = dfs(s, t, inf)) > 0){ flow += f; } }}int main(){ int N, M; while(~scanf("%d%d", &N, &M)){ //N是边数,M是顶点数 for(int i=1; i<=N; i++) G[i].clear(); es.clear(); int X, Y, C; for(int i=1; i<=N; i++){ scanf("%d%d%d", &X, &Y, &C); AddEdge(X, Y, C); } printf("%d\n", MaxFlow(1, M)); } 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 1532 Drainage Ditches (最大流)
- [hdu 1532] Drainage Ditches(最大流dinic)
- hdu 1532 Drainage Ditches(最大流)
- hdu 1532 最大网络流 Drainage Ditches
- HDU 1532 Drainage Ditches 最大流
- HDU 1532 Drainage Ditches(最大流)
- Drainage Ditches - HDU 1532 最大流
- HDU 1532--Drainage Ditches【最大流】
- hdu 1532 Drainage Ditches(最大流)
- HDU 1532 Drainage Ditches(最大流)
- hdu 1532 Drainage Ditches【最大流】
- HDU 1532 Drainage Ditches (最大流模板)
- PDF中的内嵌字体问题
- Fill the Square
- Ajax之旅(二)--XMLHttpRequest
- poj 1094 Sorting It All Out
- 【HDU】4869 Turn the pokers 组合数
- hdu 1532 Drainage Ditches 最大流 水题
- 转载:跟我一起写Makefile
- 上传应用时查看是否网络有问题
- TOJ English Training of ALPCs 错排列
- 在项目中导入import javax.servlet 出错解决办法
- Windows cmd 命令
- 使用 Python 安装 xlutils 出错
- 第1章 程序设计入门 习题
- 使用fastjson获取用户的IP所在地