Dinic
2016-02-03 12:50
302 查看
多路增广+当前弧优化
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn = 210 << 1; const int INF = 2000000010; int n,m,tot(0),to[maxn],nxt[maxn],fir[maxn],rest[maxn],mark[maxn]; void add(int u, int v, int w){ to[++tot] = v; nxt[tot] = fir[u]; fir[u] = tot; rest[tot] = w; to[tot^1] = u; nxt[tot^1] = fir[v]; fir[v] = tot^1; rest[tot^1] = 0; tot++; } int dfs(int x,int limit){ if (x == n) return limit; int used = 0,flow,i; for (i = fir[x];i && used<limit;i = nxt[i]) if (mark[to[i]]==mark[x]+1 && rest[i]){ flow = dfs(to[i],min(rest[i],limit-used)); rest[i] -= flow; rest[i^1] += flow; used += flow; } if (!used) mark[x]=-1; return used; } bool bfs(){ memset(mark,-1,sizeof(mark)); queue <int> q; int x; mark[1] = 1; q.push(1); while (!q.empty()){ x = q.front(); q.pop(); for (int i = fir[x];i;i = nxt[i]) if (mark[to[i]] == -1 && rest[i]) { q.push(to[i]); mark[to[i]] = mark[x]+1; } } return mark !=-1; } int main(){ int u,v,w; scanf("%d%d",&m,&n); for (int i=0;i<m;i++){ scanf("%d%d%d",&u,&v,&w); if (u != v) add(u,v,w); } int ans(0); while (bfs()) ans += dfs(1,INF); printf("%d\n",ans); return 0; }
相关文章推荐
- 父亲王佩禄的文化遗产
- 安卓代码实现截屏
- CSS的三种样式:内联式,嵌入式,外部式以及他们的优先级
- Xcode重构功能怎么用我全告诉你
- Java异常处理
- linux驱动心得
- android studio编程时引入外部字体样式
- Java api 入门教程 之 JAVA的SYSTEM类
- IOS8以上版本,使用UIAlertController代替 UIActionSheet和UIAlertView
- RSA 加密算法(.NET)
- 使用Excel PowerQuery和PowerPivot分析Dynamics CRM数据
- blend关于透明度的弊端和其alphatest的取舍
- 黄聪:使用七牛上传视频调用视频播放器的推荐方案
- struts2自己定义类型转换器
- bzoj1588[HNOI2002]营业额统计
- 方差,偏差的区别
- 【HDOJ】2459 Maximum repetition substring
- 图片压缩指定大小
- Soket编程学习
- Thking in Java---从哲学家就餐问题看死锁现象