网络流-最大流 Dinic模板
2018-04-10 22:08
477 查看
#include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB push_back #define ls first #define rs second typedef long long LL; typedef pair<int,int> PII; const double eps=1e-8; const double pi=acos(-1.0); const int K=1e5+7; const int mod=1e9+7; vector<pair<int,int>>mp[K]; int n,m,cnt,flow[K*2],deep[K],cur[K]; int bfs(int s,int t) { queue<int>q; memset(deep,0,sizeof deep); q.push(s),deep[s]=1; while(!q.empty()) { int u=q.front();q.pop(); for(auto &it:mp[u]) if(!deep[it.ls]&&flow[it.rs]) { deep[it.ls]=deep[u]+1; q.push(it.ls); if(it.ls==t) return 1; } } return 0; } int dfs(int x,int d,int t) { if(x==t) return d; for(int i=cur[x];i<mp[x].size();cur[x]=++i) { int u=mp[x][i].ls,v=mp[x][i].rs; if(deep[u]==deep[x]+1&&flow[v]) { int td=min(d,dfs(u,min(d,flow[v]),t)); if(!td) continue; flow[v]-=td; flow[v^1]+=td; return td; } } return 0; } int dinic(int s,int t) { int ret=0,d; while(bfs(s,t)) { memset(cur,0,sizeof cur); while(d=dfs(s,mod,t)) ret+=d; } return ret; } int main(void) { while(~scanf("%d%d",&m,&n)) { cnt=0; memset(mp,0,sizeof mp); for(int i=0,u,v,w;i<m;i++) { scanf("%d%d%d",&u,&v,&w); flow[cnt]=w,flow[cnt+1]=0; mp[u].PB(MP(v,cnt++)); mp[v].PB(MP(u,cnt++)); } printf("%d\n",dinic(1,n)); } return 0; }
相关文章推荐
- poj 1459 Power Network(网络流 最大流 dinic模板题)
- Dining POJ - 3281 EK网络流模板/最大流/拆点
- POJ1273-Drainage Ditches-网络流-最大流(模板题)
- 【模板】【网络流】Dinic
- bzoj1001: [BeiJing2006]狼抓兔子(网络流模板题:最大流)
- 网络流最大流sap算法模板
- Dinic 二分图最大匹配最大流解法(来自lixiyi学姐的模板
- 【DayDayUp】【算法_图_网络流_之二_Dinic层次图与最小费最大流的概述】(待续)
- 网络最大流(dinic)模板
- 网络流--最大流dinic模板
- 模板---图论:网络流增广路算法 Dinic
- hdu4888 最大流,dinic模板
- hdu 4289 Control(网络流 最大流+拆点)(模板)
- 【最大流 模板 Dinic】POJ 1459 Power Network
- UESTC 1143 传输数据 网络流 最大流 Dinic
- 网络流模板:最大流ISAP算法和Dinic算法
- (dinic增广网络流模板)(sizeof用法)poj-3469-Dual Core CPU
- HDU 1532-Drainage Ditches(网络最大流,EK,Ford-Fulkerson,dinic3种算法套模板)
- 最大流的理解以及dinic模板 poj1273
- 网络流dinic模板