POJ 1273 Drainage Ditches(网络流,最大流)
Description
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
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
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50
Source
<pre name="code" class="cpp">#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int INF=0x7ffffff;
const int maxn=220;
int N,M;
int r[maxn][maxn];
int pre[maxn];
bool visit[maxn];
bool bfs(int s,int t)
{
queue<int>q;
memset(pre,-1,sizeof(pre));
memset(visit,false,sizeof(visit)); pre[s]=s;
visit[s]=true;
q.push(s); int p;
while(!q.empty())
{
p=q.front();
q.pop();
for(int i=1;i<=M;i++)
{
if(r[p][i]>0&&!visit[i])
{
pre[i]=p;
visit[i]=true;
if(i==t)
return true;
q.push(i);
}
}
}
return false;
}
int solve(int s,int t)
{
int d,maxflow=0;
while(bfs(s,t))
{
d=INF;
for(int i=t;i!=s;i=pre[i])
d=min(d,r[pre[i]][i]);
for(int i=t;i!=s;i=pre[i])
{
r[pre[i]][i]-=d;
r[i][pre[i]]+=d;
}
maxflow+=d;
}
return maxflow;
}
int main()
{
while(cin>>N>>M)
{
memset(r,0,sizeof(r));
int s,e,c;
for(int i=0;i<N;i++)
{
cin>>s>>e>>c;
r[s][e]+=c;
}
cout<<solve(1,M)<<endl;
}
return 0;
}
Dinic算法:依据残留网络计算层次图,在层次图中进行DFS增广。
详见:Comzyh的博客(凝视具体,解说易懂)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
int mp[250][250];
int dis[250];
int q[2000],h,r;
int n,m,ans;
int bfs()
{
int i,j;
memset(dis,-1,sizeof(dis));
dis[1]=0;
h=0;r=1;
q[1]=1;
while(h<r)
{
j=q[++h];
for(i=1;i<=n;i++)
{
if(dis[i]<0&&mp[j][i]>0)
{
dis[i]=dis[j]+1;
q[++r]=i;
}
}
}
if(dis[n]>0) return 1;
else return 0;
}
int find(int x,int low)
{
int a;
if(x==n) return low;
for(int i=1;i<=n;i++)
{
if(mp[x][i]>0&&dis[i]==dis[x]+1&&(a=find(i,min(low,mp[x][i]))))
{
mp[x][i]-=a;
mp[i][x]+=a;
return a;
}
}
return 0;
}
int main()
{
int flow,tans;
int s,t;
while(~scanf("%d%d",&m,&n))
{
memset(mp,0,sizeof(mp));
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&s,&t,&flow);
mp[s][t]+=flow;
}
ans=0;
while(bfs())
{
if(tans=find(1,0x7ffffff))
ans+=tans;
}
printf("%d\n",ans);
}
return 0;
}
POJ 1273 Drainage Ditches(网络流,最大流)的更多相关文章
- poj 1273 Drainage Ditches 网络流最大流基础
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59176 Accepted: 2272 ...
- poj 1273 Drainage Ditches (网络流 最大流)
网络流模板题. ============================================================================================ ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- poj 1273 Drainage Ditches【最大流入门】
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63924 Accepted: 2467 ...
- poj 1273 Drainage Ditches(最大流,E-K算法)
一.Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clove ...
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- POJ 1273 Drainage Ditches 网络流 FF
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 74480 Accepted: 2895 ...
- POJ 1273 Drainage Ditches【最大流】
题意:给出起点是一个池塘,M条沟渠,给出这M条沟渠的最大流量,再给出终点是一条河流,问从起点通过沟渠最多能够排多少水到河流里面去 看的紫书的最大流,还不是很理解,照着敲了一遍 #include< ...
- POJ 1273 Drainage Ditches【最大流模版】
题意:现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条有向水渠,给出这n条水渠所连接的点和所能流过的最大流量,求从源点到汇点能流过的最大流量 Dinic #include<iost ...
随机推荐
- Krita 3.0 发布,KOffice 的图像处理器(刺激一下自己的神经)
Krita 3.0 发布了,经历了一年多的开发,动画功能被集成到Krita核心,改善了绘画功能,可及时预览绘画结果,该版本也是最新移植到QT的版本. 查看完整发布说明,可以点击这里. 下载地址: Wi ...
- windows服务怎么向应用程序发消息(部署在同一台机,非SCOKET)
命名管道:NamedPipeClientStream & NamedPipeClientStream 参考实例:http://msdn.microsoft.com/zh-cn/library/ ...
- Qt Chart 5.7.0 傻瓜安装教程
前提 qtchart 里的README文件(注意红色标记处)(本人翻译不行.多多见谅,也可以在评论里纠正( ⊙ o ⊙ )) --------------- Qt Charts 5.7.0 ----- ...
- 【JSP】JSTL核心标签库的使用方法和示例
JSTL 核心标签库 JSTL 核心标签库标签共有13个,功能上分为4类: 1. 表达式控制标签:out.set.remove.catch 2. 流程控制标签:if.choose.when.other ...
- 解决打包时IsCmdBld.exe出错的问题
1.查看环境变量是否配置了 2.查看是否是使用administrator登陆的,要求使用administrator登陆否则可能会出现权限不足的现象
- JQuery EasyUI框架学习
前言 新项目的开发前端技术打算採用EasyUI框架(基于EasyUI较为丰富的UI组件库),项目组长将前端EasyUI这块的任务分配给了我.在进行开发之前,须要我这菜鸟对EasyUI框架进行一些基础的 ...
- Linux安装完Tomcat后无法登陆管理界面
今天在Linux中安装完Tomcat后无法登陆Tomcat的管理界面,也就无法利用Tomcat管理界面来部署项目. 在Windows中一般配置完Tomcat后,只要在[conf]目录下的“tomcat ...
- bootstrap datatable项目封装支持单选多选
自己在开发项目是根据自己的项目后台框架封装的jquery datatable插件基本上能集成到任何项目中使用,当然封装的还不够完美,给大家学习 调侃 使用介绍:query_dataTable({tab ...
- 使用Eclipse EE开发web项目
最近使用EclipseEE开发web项目,遇到了以下几个问题: 1. 通过tomcat启动web应用的时候,总是提示找不到包或者class. 经过排查,发现所有的jar包并没有放到WEB-INF/li ...
- POJ 2286 The Rotation Game 迭代搜索深度 + A* == IDA*
感觉这样的算法还是比較局限的吧,反复搜索是一个不好的地方,并且须要高效的估值函数来进行强剪枝,这点比較困难. 迭代搜索深度是一个比較炫酷的搜索方式,只是有点拿时间换空间的感觉. 首先迭代深度比較搓的写 ...