HDU 1532 Drainage Ditches 网络流
2012-11-24 17:38
309 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1532
题意:
从1开始到终点n,最多能通过多少的流量,就比如说1到2的流量是20,2到3的流量是10,那么到达终点3的流量为10,而
1到2还能经过10的流量,2到3不能再通过。
坑爹:
每次找到一条增广轨的时候要补一个反向的边,不然你走这一条路就认为你一定走了,因为有可能这条路不是最佳的路径。
解法:
利用BFS搜索点,找到终点就返回1,没有则返回0,再设置一个maxflow函数,只要BFS的返回值为1,就将这条增广轨上的
最大可以通过的流量(min)算出来(利用change_map函数),然后 max_flow += min。
View Code
题意:
从1开始到终点n,最多能通过多少的流量,就比如说1到2的流量是20,2到3的流量是10,那么到达终点3的流量为10,而
1到2还能经过10的流量,2到3不能再通过。
坑爹:
每次找到一条增广轨的时候要补一个反向的边,不然你走这一条路就认为你一定走了,因为有可能这条路不是最佳的路径。
解法:
利用BFS搜索点,找到终点就返回1,没有则返回0,再设置一个maxflow函数,只要BFS的返回值为1,就将这条增广轨上的
最大可以通过的流量(min)算出来(利用change_map函数),然后 max_flow += min。
#include<iostream> #include<queue> using namespace std; const int maxn = 200 + 10; const int INF = 0x3fffffff; int map[maxn][maxn]; int front[maxn]; int used[maxn]; int max_flow; int m; int n; void init() { max_flow = 0; memset(map,0,sizeof(map)); } int BFS() { memset(used,0,sizeof(used)); queue<int> Q; while(!Q.empty()) { Q.pop(); } Q.push(1); front[1] = 1; used[1] = 1; while(!Q.empty()) { int i; int temp = Q.front(); Q.pop(); for(i=2; i<=n; i++) { if(map[temp][i] != 0 && used[i] == 0) { Q.push(i); used[i] = 1; front[i] = temp; if(i == n) { return 1; } } } } return 0; } int change_map() { int min = INF; int temp = n; while(front[temp] != temp) { if(min > map[front[temp]][temp]) { min = map[front[temp]][temp]; } temp = front[temp]; } temp = n; while(front[temp] != temp) { map[front[temp]][temp] -= min; map[temp][front[temp]] += min;//必须补反边 temp = front[temp]; } return min; } void maxflow() { while(BFS()) { max_flow += change_map(); } } int main() { while(cin>>m>>n) { init(); int i; int a; int b; int c; for(i=0; i<m; i++) { cin>>a>>b>>c; map[a][b] += c; } maxflow(); cout<<max_flow<<endl; } return 0; }
View Code
相关文章推荐
- hdu 1532 网络流
- HDU 1532 Drainage Ditches 网络流模板题(Dinic)
- hdu 1532 网络流-最大流 DINIC
- HDU 1532 Drainage Ditches 网络流模板
- HDU-1532-网络流最大流
- HDU 1532 Drainage Ditches (最大网络流)
- Drainage Ditches (poj 1273 && hdu 1532 网络流之Ford-Fulkerson)
- HDU1532 Drainage Ditches(网络流EdmondsKarp)
- NYOJ 323 && HDU 1532 && POJ 1273 Drainage Ditches (网络流之最大流入门)
- HDU 1532 && POJ 1273 Drainage Ditches (网络流 EK算法)
- 图论 网络流 HDU 1532 最大流
- HDU1532 Drainage Ditches 网络流--dinic算法 SAP算法
- hdu1532网络流
- Hdu 1532 网络流模版
- 网络流改进SAP算法模版、HDU 1532 Drainage Ditches(解题报告)
- Drainage Ditches - HDU 1532 - 网络流
- hdu 1532 Drainage Ditches(最大网络流)
- HDU-1532 Drainage Ditches,人生第一道网络流!
- 网络流改进SAP算法模版、HDU 1532 Drainage Ditches(解题报告)
- Hdu 1532 Drainage Ditches【网络流】