poj 1273 && hdu 1532 Drainage Ditches (网络最大流)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 53640 | Accepted: 20448 |
Description
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
Output
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50
Source
//312K 0MS C++ 1400B 2014-05-03 17:50:04
//最大流模板题
#include<iostream>
#include<queue>
#define INF 0x7ffffff
#define N 205
using namespace std;
int flow[N],vis[N];
int g[N][N],father[N];
int maxflow,n,m;
int Min(int a,int b)
{
return a<b?a:b;
}
void EK(int s,int e)
{
queue<int>Q;
while(){
memset(vis,,sizeof(vis));
memset(flow,,sizeof(flow));
Q.push(s);
flow[s]=INF;
while(!Q.empty()){
int u=Q.front();
Q.pop();
for(int v=;v<=n;v++){
if(!vis[v] && g[u][v]>){
vis[v]=;
father[v]=u;
Q.push(v);
flow[v]=Min(flow[u],g[u][v]);
}
}
if(flow[e]>){
while(!Q.empty()) Q.pop();
break;
}
}
if(flow[e]==) break;
for(int i=e;i!=s;i=father[i]){
g[father[i]][i]-=flow[e];
g[i][father[i]]+=flow[e];
}
maxflow+=flow[e];
}
}
int main(void)
{
int a,b,c;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(g,,sizeof(g));
maxflow=;
while(m--){
scanf("%d%d%d",&a,&b,&c);
g[a][b]+=c;
}
EK(,n);
printf("%d\n",maxflow);
}
return ;
}
dinic算法:
O(n*n*m)(如果用bfs实现增广路径的话则是O(n*m*m),好像是另一个算法了)
效率比EK高,因为多了优化。
算法思想:重复搜索增广路径的动作,而判定条件改为直到不能分层为止,能避免很多不必要的过程。(因为偷懒所以用了STL实现队列和邻接表)
//15MS 340K 1496 B G++
#include<iostream>
#include<queue>
#include<vector>
#define N 205
#define INF 0x7ffffff
using namespace std;
struct node{
int v,c;
node(int a,int b){
v=a;c=b;
}
};
int n,m;
vector<node>V[N];
int dis[N];
queue<int>Q;
bool bfs() //分层
{
memset(dis,-,sizeof(dis));
dis[]=;
int u=;
Q.push(u);
while(!Q.empty()){
u=Q.front();
Q.pop();
for(int i=;i<V[u].size();i++){
if(dis[V[u][i].v]==- && V[u][i].c>){
dis[V[u][i].v]=dis[u]+;
Q.push(V[u][i].v);
}
}
}
return dis[n]>=;
}
int dfs(int u,int minn) //增广
{
if(u==n) return minn;
for(int i=;i<V[u].size();i++){
if(dis[V[u][i].v]==dis[u]+ && V[u][i].c>){
int c=V[u][i].c;
minn=minn<c?minn:c;
int ans=dfs(V[u][i].v,minn);
if(ans>){
V[u][i].c-=ans;
return ans;
}
}
}
return ;
}
int dinic()
{
int ans=;
while(bfs()){
ans+=dfs(,INF);
//printf("*%d\n",ans);
}
return ans;
}
int main(void)
{
int u,v,c;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(int i=;i<=n;i++)
V[i].clear();
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&c);
V[u].push_back(node(v,c));
}
printf("%d\n",dinic());
}
return ;
}
ISAP:
O(n*n*m)
算法思想:这个算法看的时间比较长,因为比较难找到合理化的解释,第一次基本是套用别人的模板,现在写一下自己的理解。
算法复杂度上的分析我理解的不是很透彻,因此先借用别人的结果。而ISAP算法就分为几部分,开始先逆向BFS初始化距离标号,然后进入ISAP主函数,和标号法的思想差不多,(1)一直循环查找增广路径;(2)找到增广路径时则进行augment()处理,找不到时则回退retreat()处理;(3)当出现断层或则d[source]>=n时算法结束。
推荐一个网站:http://blog.csdn.net/yuhailin060/article/details/4897608
伪代码:
algorithm Improved-Shortest-Augmenting-Path
f <--
从终点 t 开始进行一遍反向 BFS 求得所有顶点的起始距离标号 d(i)
i <-- s
while d(s) < n do
if i = t then // 找到增广路
Augment
i <-- s // 从源点 s 开始下次寻找
if Gf 包含从 i 出发的一条允许弧 (i,j)
then Advance(i)
else Retreat(i) // 没有从 i 出发的允许弧则回退
return f procedure Advance(i)
设 (i,j) 为从 i 出发的一条允许弧
pi(j) <-- i // 保存一条反向路径,为回退时准备
i <-- j // 前进一步,使 j 成为当前结点 procedure Retreat(i)
d(i) <-- + min{d(j):(i,j)属于残量网络Gf} // 称为重标号的操作
if i != s
then i <-- pi(i) // 回退一步 procedure Augment
pi 中记录为当前找到的增广路 P
delta <-- min{rij:(i,j)属于P}
沿路径 P 增广 delta 的流量
更新残量网络 Gf
题目解决的实现:
邻接矩阵:
//15MS 532K 2612 B G++
#include<stdio.h>
#include<string.h>
#define N 202
#define inf 0x7fffffff
int n,m,source,sink;
int g[N][N],f[N][N];
int pi[N]; //增广路径
int curnode[N]; //当前节点,优化循环 int queue[N]; //队列 int d[N]; //距离标号
int num[N]; //num[i] 为 d[j]=i 的数量,记录是否断层,断层结束算法 int bfs() //逆向bfs初始化距离标号
{
int head=,tail=;
for(int i=;i<=n;i++)
num[d[i]=n]++; num[n]--;
d[sink]=;
num[]++; queue[++tail]=sink; while(head!=tail){
int u=queue[++head]; for(int i=;i<=n;i++){
if(d[i]<n || g[i][u]==) continue; queue[++tail]=i; num[n]--;
d[i]=d[u]+;
num[d[i]]++;
}
}
return ;
} int augment() //处理增广路径
{
int width=inf;
for(int i=sink,j=pi[i];i!=source;i=j,j=pi[j]){
if(g[j][i]<width) width=g[j][i];
} for(int i=sink,j=pi[i];i!=source;i=j,j=pi[j]){
g[j][i]-=width; f[j][i]+=width;
g[i][j]+=width; f[i][j]-=width;
} return width;
} int retreat(int &i) //处理回退,重标号
{
int temp;
int mind=n-;
for(int j=;j<=n;j++)
if(g[i][j]> && d[j]<mind)
mind=d[j]; temp=d[i]; num[d[i]]--;
d[i]=mind+;
num[d[i]]++; if(i!=source) i=pi[i]; return num[temp];
} int maxflow(int s,int e) //ISAP求最大流
{
int i,j,flow=; source=s;
sink=e;
bfs();
//for(int i=1;i<=n;i++) printf("*%d\n",d[i]);
for(i=;i<=n;i++) curnode[i]=; i=source; for( ; d[source]<n ; ){ //主循环
for(j=curnode[i];j<=n;j++) //查找允许弧
if(g[i][j]> && d[i]==d[j]+)
break;
if(j<=n){ //存在允许弧
curnode[i]=j;
pi[j]=i;
i=j; if(i==sink){ //找到增广路径
flow+=augment();
//printf("*%d %d\n",pi[i],flow);
i=source;
} }else{ //不存在允许弧
curnode[i]=;
if(retreat(i)==) break;
}
}
return flow;
} int main(void)
{
int a,b,c;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(g,,sizeof(g));
memset(f,,sizeof(f));
for(int i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
g[a][b]+=c;
}
printf("%d\n",maxflow(,n));
}
return ;
}
邻接表:
//15MS 320K 1811 B G++
#include<iostream>
#define N 1005
#define inf 0x7fffffff
using namespace std;
struct node{
int v;
int c;
int next;
}edge[N]; int head[N],eid;
int n,m; int h[N]; //距离标号
int gap[N]; //记录是否断层 void addedge(int u,int v,int c) //有向图
{
edge[eid].v=v;
edge[eid].c=c;
edge[eid].next=head[u];
head[u]=eid++;
edge[eid].v=u;
edge[eid].c=;
//edge[eid].c=c; //有向图
edge[eid].next=head[v];
head[v]=eid++;
}
int dfs(int u,int tc)
{
if(u==n) return tc; //找到增广路径
int c0;
int minh=n-;
int temp=tc;
for(int i=head[u];i!=-;i=edge[i].next){ //遍历与u有关的弧
int v=edge[i].v;
int c=edge[i].c;
if(c>){
if(h[v]+==h[u]){ //可行弧
c0=temp<c?temp:c;
c0=dfs(v,c0);
edge[i].c-=c0;
edge[i^].c+=c0;
temp-=c0;
if(h[]>=n) return tc-temp;
if(temp==) break;
}
if(h[v]<minh) minh=h[v]; //更新最短距离标号
}
}
if(temp==tc){ //不存在可行弧,回退操作
--gap[h[u]];
if(gap[h[u]]==) h[]=n; //出现断层
h[u]=minh+;
++gap[h[u]];
}
return tc-temp;
}
int ISAP() //isap算法
{
int ans=;
memset(gap,,sizeof(gap));
memset(h,,sizeof(h));
gap[]=n;
while(h[]<n){
ans+=dfs(,inf);
}
return ans;
}
int main(void)
{
int u,v,c;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(head,-,sizeof(head));
eid=;
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&c);
addedge(u,v,c);
}
printf("%d\n",ISAP());
}
return ;
}
poj 1273 && hdu 1532 Drainage Ditches (网络最大流)的更多相关文章
- POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)
Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...
- hdu 1532 Drainage Ditches(最大流模板题)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 【初识——最大流】 hdu 1532 Drainage Ditches(最大流) USACO 93
最大流首次体验感受—— 什么是最大流呢? 从一个出发点(源点),走到一个目标点(汇点),途中可以经过若干条路,每条路有一个权值,表示这条路可以通过的最大流量. 最大流就是从源点到汇点,可以通过的最大流 ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- hdu 1532 Drainage Ditches (最大流)
最大流的第一道题,刚开始学这玩意儿,感觉好难啊!哎····· 希望慢慢地能够理解一点吧! #include<stdio.h> #include<string.h> #inclu ...
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1532 Drainage Ditches (最大网络流)
Drainage Ditches Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) To ...
- HDU 1532 Drainage Ditches (网络流)
A - Drainage Ditches Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- hdu 1532 Drainage Ditches(最大流)
Drainage Dit ...
随机推荐
- DevExpress TreeList用法总结
http://blog.itpub.net/29251214/viewspace-774395/ http://blog.csdn.net/czp_huster/article/details/501 ...
- 北京Uber优步司机奖励政策(12月15日)
用户组:人民优步及电动车(适用于12月15日) 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:htt ...
- jquery实现倒计时功能
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SpringBoot学习:整合shiro(rememberMe记住我功能)
项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 首先在shiro配置类中注入rememberMe管理器 /** * cookie对象; ...
- iOS - Foundation相关
1.NSString A.创建的方式: stringWithFormat:格式化字符串 ,创建字符串对象在堆区域 @"jack& ...
- c++ reference can not be reassigned
#include <iostream> using namespace std; int main () { // declare simple variables int i; int ...
- 【廖雪峰老师python教程】——进程与线程
多进程 操作系统轮流让各个任务交替执行,任务1执行0.01秒,切换到任务2,任务2执行0.01秒,再切换到任务3,执行0.01秒……这样反复执行下去.表面上看,每个任务都是交替执行的,但是,由于CPU ...
- keepalived+nginx实现高可用+tomcat
1.keepalived的yum安装 安装依赖包[root@localhost ~]# yum install -y curl gcc openssl-devel libnl3-devel net-s ...
- 前端开发工程师 - 02.JavaScript程序设计 - 第2章.进阶篇
第2章--进阶篇 类型进阶 类型: Undefined Null Boolean String Number Object 原始类型(值类型):undefined, null, true, " ...
- Java开发工程师(Web方向) - 01.Java Web开发入门 - 第6章.蜂巢
第6章--蜂巢 蜂巢简介 网站开发完,就需要测试.部署.在服务器上运行. 网易蜂巢: 采用Docker容器化技术的云计算平台 https://c.163.com 容器管理:容器可被视作为云主机的服务器 ...