hdu 1532Drainage Ditches
来源:互联网 发布:java基础入门 编辑:程序博客网 时间:2024/05/20 10:15
网络流!
1. E_K()
#include <iostream>#include<queue>#include<cstring>#include<cstdlib>using namespace std;int g[205][205];int n,m;queue<int> q;int parent[205];int vis[205];int flow[205];int min(int a,int b){ return a>b?b:a;}int bfs(){ while(!q.empty()) q.pop(); for(int i=0;i<=m;i++) flow[i]=vis[i]=0; flow[1]=100000000; q.push(1); vis[1]=1; while(!q.empty()) { int cur=q.front(); q.pop(); if(cur==m) return flow[cur]; for(int i=2;i<=m;i++) if(!vis[i]&&g[cur][i]) { vis[i]=1; q.push(i); parent[i]=cur; flow[i]=min(flow[cur],g[cur][i]); } } return 0;}int E_K(){ int all=0,add=0; parent[1]=0; while((add=bfs())) { all+=add; int now=m,pre; while((pre=parent[now])) { g[pre][now]-=add; g[now][pre]+=add; now=pre; } } return all;}int main(){// freopen("in.txt","r",stdin); while(scanf("%d %d",&n,&m)==2) { for(int i=0;i<=m;i++) for(int j=0;j<=m;j++) g[i][j]=0; for(int i=0;i<n;i++) { int s,e,c; scanf("%d %d %d",&s,&e,&c); g[s][e]+=c; } int t=0; printf("%d\n",E_K()); } return 0;}
2. Dinic()
#include <iostream>#include<queue>#include<cstring>#include<cstdlib>using namespace std;int g[205][205];int n,m;int layer[205];queue<int> q;int min(int a,int b){ return a>b?b:a;}int bfs(){ for(int i=0;i<=m;i++) layer[i]=-1; while(!q.empty()) q.pop(); q.push(1); layer[1]=0; while(!q.empty()) { int cur=q.front(); q.pop(); if(cur==m) return 1; for(int i=2;i<=m;i++) if(layer[i]==-1&&g[cur][i]) { layer[i]=layer[cur]+1; q.push(i); } } return 0;}int dfs(int cur,int flow){ if(cur==m) return flow; for(int i=2;i<=m;i++) if((layer[i]==layer[cur]+1)&&g[cur][i]) { int t=dfs(i,min(flow,g[cur][i])); if(t) { g[cur][i]-=t; g[i][cur]+=t; return t; } } return 0;}int dinic(){ int all=0,t; while(bfs()) while(t=dfs(1,100000000)) all+=t; return all;}int main(){// freopen("in.txt","r",stdin); while(scanf("%d %d",&n,&m)==2) { for(int i=0;i<=m;i++) for(int j=0;j<=m;j++) g[i][j]=0; for(int i=0;i<n;i++) { int s,e,c; scanf("%d %d %d",&s,&e,&c); g[s][e]+=c; } int t=dinic(); printf("%d\n",t); } return 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
- #include <filename.h> 和 #include “filename.h” 有什么区别?
- C语言深度解剖读书笔记之——C语言基础测试题
- 设定xfce工具栏图标背景的几种方法
- C#编写的发送手机短信的类库 && C#开发短信的方法和简介 &&短信编程实例
- html中制表符(TAB)的转义字符
- hdu 1532Drainage Ditches
- 基于Socket的UDP和TCP编程介绍
- JAVA在类继承中类成员变量“重写”的问题
- BASH命令和SHELL脚本总结(17)在BASH中计算两天的间隔天数
- 技术出身的项目经理容易犯的一个错误
- 关于火狐firework屏蔽非80端口含有AJAX页面运行的解决之道
- Python学习笔记【2】
- buffers/cache
- rsync 的核心算法