[BZOJ3396][Usaco2009 Jan]Total flow 水流(最大流)
2016-04-06 23:27
351 查看
题目描述
传送门题解
最大流裸题。代码
#include<iostream> #include<cstring> #include<cstdio> #include<queue> using namespace std; const int max_N=100; const int max_m=705; const int max_e=max_m*2; const int INF=1e9; char x,y; int n,N,u,t,cap,maxflow; int tot,point[max_N],next[max_e],v[max_e],remain[max_e]; int deep[max_N],cur[max_N],last[max_N],num[max_N]; queue <int> q; inline void addedge(int x,int y,int cap){ // cout<<x<<" "<<y<<" "<<cap<<endl; ++tot; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap; ++tot; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0; } inline void bfs(int t){ for (int i=1;i<=N;++i) deep[i]=N; deep[t]=0; for (int i=1;i<=N;++i) cur[i]=point[i]; while (!q.empty()) q.pop(); q.push(t); while (!q.empty()){ int now=q.front(); q.pop(); for (int i=point[now];i!=-1;i=next[i]) if (deep[v[i]]==N&&remain[i^1]){ deep[v[i]]=deep[now]+1; q.push(v[i]); } } } inline int addflow(int s,int t){ int now=t,ans=INF; while (now!=s){ ans=min(ans,remain[last[now]]); now=v[last[now]^1]; } now=t; while (now!=s){ remain[last[now]]-=ans; remain[last[now]^1]+=ans; now=v[last[now]^1]; } return ans; } inline void isap(int s,int t){ bfs(t); for (int i=1;i<=N;++i) ++num[deep[i]]; int now=s; while (deep[s]<N){ if (now==t){ maxflow+=addflow(s,t); now=s; } bool has_find=false; for (int i=cur[now];i!=-1;i=next[i]) if (deep[v[i]]+1==deep[now]&&remain[i]){ has_find=true; cur[now]=i; last[v[i]]=i; now=v[i]; break; } if (!has_find){ int minn=N-1; for (int i=point[now];i!=-1;i=next[i]) if (remain[i]) minn=min(minn,deep[v[i]]); if (!(--num[deep[now]])) break; num[deep[now]=minn+1]++; cur[now]=point[now]; if (now!=s) now=v[last[now]^1]; } } } int main(){ tot=-1; memset(point,-1,sizeof(point)); memset(next,-1,sizeof(next)); scanf("%d",&n); N=60; for (int i=1;i<=n;++i){ cin>>x>>y>>cap; u=x-'A'+2; t=y-'A'+2; addedge(u,t,cap); } addedge(1,2,INF); addedge(27,N,INF); isap(1,N); printf("%d\n",maxflow); }
相关文章推荐
- 忘记管理员密码的补救办法
- CCF 认证—消除类游戏(思维题目)
- 【LeetCode】50. Pow(x, n)解法及注释
- 《常见算法和数据结构》元素排序(4)——凸包问题
- ios设计模式之MVC体系
- mysql
- When creating mex in MATLAB: error C3861: 'fmax': identifier not found
- 《Algorithms算法》笔记:元素排序(4)——凸包问题
- 软件项目管理第五周作业
- 第六、七周总结
- 第六、七周总结
- FFMPEG解码流程
- windows server 2003 证书安装异常问题
- Android/Java面试题,边面试边成长,为了应试而应试
- 棋盘覆盖算法
- jQuery学习笔记 —— 1. 选择器
- UIPickerView的应用
- 为什么需要架构图,怎么画?
- solr学习之一 搜索基本知识
- SDAU 搜索专题 12 Rescue