【codevs1993】草地排水(isap)
来源:互联网 发布:js 日期转换时间戳 编辑:程序博客网 时间:2024/04/26 13:42
题目描述
传送门
题解
网络流最大流模板题
代码
#include<iostream>#include<cstring>#include<cstdio>#include<queue>using namespace std;const int max_n=2005;const int max_m=2005;const int max_e=max_m*2;const int inf=1e9;int point[max_n],next[max_e],v[max_e],remain[max_e],tot;int cur[max_n],deep[max_n],last[max_n],num[max_n];int n,m,x,y,cap,maxflow;queue <int> q;inline void add(int x,int y,int cap){ ++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; 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 ans=inf,now=t; 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){ int now=s; bfs(t); for (int i=1;i<=n;++i) ++num[deep[i]]; for (int i=1;i<=n;++i) cur[i]=point[i]; 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]){ int u=v[i]; if (deep[u]+1==deep[now]&&remain[i]){ has_find=true; cur[now]=i; last[u]=i; now=u; 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%d",&m,&n); for (int i=1;i<=m;++i){ scanf("%d%d%d",&x,&y,&cap); add(x,y,cap); } isap(1,n); printf("%d\n",maxflow);}
0 0
- 【codevs1993】草地排水(isap)
- [codevs1993]草地排水(最大流裸题)
- codevs1993 草地排水
- Codevs1993草地排水
- 【CODEVS1993】【网络流】草地排水
- 【codevs1993】草地排水,网络流入门(dinic+ispa)
- codevs1993草地排水——第一次写网络流
- 网络流(草地排水)
- 草地排水
- 【codevs 1993】草地排水(最大流)
- #bzoj1084#草地排水(网络流)
- 网络流最大流ISAP算法[草地排水][运输问题1]
- 【Drainage Ditches 草地排水】
- 【wikioi1993】草地排水
- 【wikioi1993】 草地排水
- USACO草地排水
- code vs 草地排水
- [codevs 1993]草地排水
- Docker安装
- Integrity Measurement Architecture (IMA)
- Boost 编译
- Spring MVC下 Excel 导入导出(poi)
- 启动spark-shell提示scala版本不一致的解决
- 【codevs1993】草地排水(isap)
- lua中的require机制
- java第一篇
- 软键盘挡着edittext的解决办法
- OC中的关键字,weak,assign,copy,strong,nonatomic,retain
- 工具的使用——windows操作系统的使用(创建宽带连接)
- discuz!x2目录结构及文件用途说明
- 查看签名文件的有效期限等详细信息
- Android 滚动字幕实现