博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1273 网络流入门题 dinic算法解决,可作模板使用
阅读量:5129 次
发布时间:2019-06-13

本文共 3119 字,大约阅读时间需要 10 分钟。

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 62078   Accepted: 23845

Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 41 2 401 4 202 4 202 3 303 4 10

Sample Output

50

Source

 
 
 
#include
#include
#include
#include
#include
using namespace std;int edge[300][300];//邻接矩阵int dis[300];//距源点距离,分层图int start,end;int m,n;//N:点数;M,边数int bfs(){ memset(dis,-1,sizeof(dis));//以-1填充 dis[1]=0; queue
q; q.push(start); while(!q.empty()){ int u=q.front(); q.pop(); for(int i=1;i<=n;i++){ if(dis[i]<0&&edge[u][i]){ dis[i]=dis[u]+1; q.push(i); } } } if(dis[n]>0) return 1; else return 0;//汇点的DIS小于零,表明BFS不到汇点}//Find代表一次增广,函数返回本次增广的流量,返回0表示无法增广int find(int x,int low){ //Low是源点到现在最窄的(剩余流量最小)的边的剩余流量 int a=0; if(x==n) return low;//是汇点 for(int i=1;i<=n;i++){ if(edge[x][i]>0&&dis[i]==dis[x]+1&&//联通,,是分层图的下一层 (a=find(i,min(low,edge[x][i])))){ //能到汇点(a <> 0) edge[x][i]-=a; edge[i][x]+=a; return a; } } return 0;}int main(){ while(scanf("%d%d",&m,&n)!=EOF){ memset(edge,0,sizeof(edge)); for(int i=1;i<=m;i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); edge[u][v]+=w; } start=1; end=n; int ans=0; while(bfs()){ //要不停地建立分层图,如果BFS不到汇点才结束 ans+=find(1,0x7fffffff);//一次BFS要不停地找增广路,直到找不到为止 } printf("%d\n",ans); } return 0;}

 

 
 

转载于:https://www.cnblogs.com/13224ACMer/p/4695423.html

你可能感兴趣的文章
最大权闭合子图
查看>>
oracle 创建暂时表
查看>>
201421410014蒋佳奇
查看>>
导入导出数据库和导入导出数据库表
查看>>
linux下操作mysql
查看>>
【03月04日】A股滚动市盈率PE历史新低排名
查看>>
总结:Bias(偏差),Error(误差),Variance(方差)及CV(交叉验证)
查看>>
iOS7 界面适配-NavigationBar StateBar
查看>>
用canvas上传图片
查看>>
五子棋-开发环境搭建过程
查看>>
Java数据结构与算法解析(三)——队列与背包
查看>>
Xcode5和ObjC新特性
查看>>
.Net Discovery 系列之二--string从入门到精通(下)
查看>>
Loadrunner:录制APP脚本
查看>>
jvm slot复用
查看>>
高并发系统数据库设计
查看>>
js 点击获取验证码后的倒数60s
查看>>
杭电ACM-1.2.3 QuickSum
查看>>
基于mini2440的boa服务器移植
查看>>
我写的第4个程序(日志最近行读取函数)
查看>>