标签:算法
Description
Input
Output
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #include <queue> using namespace std; #define inf 9999999999 int flow[210][210]; int maxflow[210],father[210],vis[210]; int max_flow; int m,i; void EK(int s,int e) { queue<int >q; int u,v; while(1) { memset(maxflow,0,sizeof(maxflow));//每次寻找增广路径都将每个点的流入容量置为0; memset(vis,0,sizeof(vis)); maxflow[s]=inf;//源点的流入量置为正无穷; q.push(s);//将源点压入队列; while(!q.empty())//当队列不为空 { u=q.front(); q.pop(); for(v=s;v<=e;v++) { if(!vis[v]&&flow[u][v]>0) { vis[v]=1; father[v]=u;//记录下他的父亲方便反向更新 q.push(v); maxflow[v]=min(maxflow[u],flow[u][v]);//当前点的容量为父亲点容量与边流量的较小者 } } if(maxflow[e]>0)//如果找到了汇点并且汇点容量不为0则清空队列 { while(!q.empty()) q.pop(); break; } } if(maxflow[e]==0)//已经找不到到汇点的增光路经了,就退出整个循环 break; for(i=e;i!=s;i=father[i]) { flow[father[i]][i]-=maxflow[e];//正向更新 flow[i][father[i]]+=maxflow[e];//反向更新 } max_flow+=maxflow[e];//更新最大流 } } int main() { int n; int si,ei,ci; while(~scanf("%d %d",&n,&m)) { max_flow=0;//最大流初始化; memset(flow,0,sizeof(flow)); for(i=0;i<n;i++) { scanf("%d %d %d",&si,&ei,&ci); flow[si][ei]+=ci; } EK(1,m); printf("%d\n",max_flow); } }
Drainage Ditches(最大流基础_增广路算法),布布扣,bubuko.com
标签:算法
原文地址:http://blog.csdn.net/u013486414/article/details/38659271