HDU 1532 Drainage Ditches
来源:互联网 发布:未网络上注册 有信号 编辑:程序博客网 时间:2024/06/11 11:05
题目地址:https://vjudge.net/problem/HDU-1532
思路:网络流模板题,学习了一下网络流的入门知识,让我感觉嗯……很神奇。本来想直接抄kuangbin模板上的dinic但是菜鸡抄了一遍发现还是没看懂…然后去网上找了份简单点的模板……争取这周把网络流的专题过掉吧
AC代码:
#include<bits/stdc++.h>using namespace std;const int MAXN = 2010;const int MAXM = 1200010;const int INF = 0x3f3f3f3f;struct Edge{ int u,v,next,cap,flow;}edge[MAXM];int tol;int head[MAXN];void init(){ tol = 0; memset(head,-1,sizeof(head));}void addedge(int u,int v,int w,int rw=0){ edge[tol].u=u;edge[tol].v=v;edge[tol].cap=w; edge[tol].next=head[u];head[u]=tol++; edge[tol].u=v;edge[tol].v=u;edge[tol].cap=rw; edge[tol].next=head[v];head[v]=tol++;}int dep[MAXN],cur[MAXN],sta[MAXN];bool bfs(int s,int t){//层次化建图 queue<int>q; memset(dep,-1,sizeof(dep)); dep[s]=0; q.push(s); while(!q.empty()){ int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v; if(edge[i].cap>0 && dep[v]==-1){ dep[v]=dep[u]+1; if(v==t) return true; q.push(v); } } } return false;}int dfs(int a,int b,int s,int t){ int r=0; if(a==t) return b; for(int i=head[a];i!=-1 && r<b;i=edge[i].next){ int u=edge[i].v; if(edge[i].cap>0 && dep[u]==dep[a]+1){ int x=min(edge[i].cap,b-r); x=dfs(u,x,s,t); r+=x; edge[i].cap-=x; edge[i^1].cap+=x; } } if(!r)dep[a]=-2; return r;}int dinic(int s,int t){ int total=0,temp; while(bfs(s,t)){ while(temp=dfs(s,INF,s,t)) total+=temp; } return total;}int main(){ int i,u,v,c,m,n,s,t; while(~scanf("%d%d",&m,&n)){ init(); s=1,t=n; for(int i=0;i<m;i++){ scanf("%d%d%d",&u,&v,&c); addedge(u,v,c); } printf("%d\n",dinic(s,t)); }}
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 1532Drainage 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
- HDU 1532 - Drainage Ditches
- HDU 1532Drainage Ditches
- HDU 1532Drainage Ditches
- HDU-1532-Drainage Ditches
- Redux系列01+核心概念 工作流程 安装 Action和Action创建函数 Reducer Store 数据流
- Java线程状态分析
- 10. Regular Expression Matching
- java学习第五天
- Java集合框架总结01之常用单列集合(ArrayList, HashSet)
- HDU 1532 Drainage Ditches
- Codeforces-792E Colored Balls(贪心/数学)
- 合并两个排序链表
- 数学-洛谷P1072 Hankson 的趣味题
- Python3.6下scrapy框架的安装
- Java技术文档珍藏
- Android实现计算器布局(四种布局方式)之LinearLayout线性布局
- java小问题
- VxWorks内核解读 -设备驱动