POJ1273 USACO 4.2.1 Drainage Ditches CodeVS1993草地排水 网络流 最大流 SAP
欢迎访问~原文出处——博客园-zhouzhendong
去博客园看该题解
传送门 - POJ
传送门 - CodeVS
题意概括
给出一个图,告诉你边和容量,起点是1,汇点是n,让你求最大流。
题解
最大流模板题。
SAP跑一发!
学习网络流
代码
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
const int N=+,M=N*;
int n,m;
struct edge{
int x,y,cap,flow,nxt;
// x,y 边的两个节点, cap 容量, flow 流量, nxt 指向 x 的下一条边
};
struct gragh{
int cnt,fst[N],dist[N],s,t,num[N],cur[N],p[N];
int q[N],head,tail;
edge e[M];
void set(int S,int T){//初始化
s=S,t=T,cnt=;
memset(fst,,sizeof fst);
memset(e,,sizeof e);
}
void add(int a,int b,int c){
// 建立正向边,容量为c
cnt++;
e[cnt].x=a,e[cnt].y=b,e[cnt].cap=c,e[cnt].flow=;
e[cnt].nxt=fst[a],fst[a]=cnt;
// 建立反向边,容量为0
cnt++;
e[cnt].x=b,e[cnt].y=a,e[cnt].cap=,e[cnt].flow=;
e[cnt].nxt=fst[b],fst[b]=cnt;
}
void re_bfs(){
memset(dist,-,sizeof dist);
memset(q,,sizeof q);
head=tail=dist[t]=;
q[++tail]=t;
while (head<tail)
for (int x=q[++head],i=fst[x];i;i=e[i].nxt){
int y=e[i].y;
if (e[i].cap==&&dist[y]==-)
q[++tail]=y,dist[y]=dist[x]+;
}
//bfs给所有的节点分层,得到每个点到达汇点的一种路径的边数
//据说可以达到常数级优化的效果
for (int i=;i<=n;i++)
if (dist[i]==-)
dist[i]=n;
}
int Augment(int &point){
int ex_Flow=<<;
for (int i=t;i!=s;i=e[p[i]].x)
if (e[p[i]].cap-e[p[i]].flow<ex_Flow)
ex_Flow=e[p[i]].cap-e[p[i]].flow,point=e[p[i]].x;
for (int i=t;i!=s;i=e[p[i]].x){
e[p[i]].flow+=ex_Flow;
e[p[i]^].flow-=ex_Flow;
}
return ex_Flow;
}
int ISAP(){
int x,y,MaxFlow=;
memset(num,,sizeof num);
for (int i=;i<=n;i++)
cur[i]=fst[i];//保存每个节点增广到的弧,
//作为当前弧优化的重要部分
for (int i=;i<=n;i++)
num[dist[i]]++;//算出每一个层次当前的节点数量
x=s;
while (dist[s]<n){
if (x==t){
MaxFlow+=Augment(x);
continue;
}
bool found=;
for (int i=cur[x];i;i=e[i].nxt){
y=e[i].y;
if (dist[y]+==dist[x]&&e[i].cap>e[i].flow){
p[y]=cur[x]=i,x=y,found=;
break;
}//找到一条可能为增广路的路径
}
if (!found){
int d=n+;
for (int i=fst[x];i;i=e[i].nxt)
if (e[i].cap>e[i].flow)
y=e[i].y,d=min(d,dist[y]+);
if (!(--num[dist[x]]))
return MaxFlow;//传说中的GAP优化
num[dist[x]=d]++;
cur[x]=fst[x];
if (x!=s)
x=e[p[x]].x;
}
}
return MaxFlow;
}
}g;
int main(){
while (~scanf("%d%d",&m,&n)){
g.set(,n);
for (int i=,a,b,c;i<=m;i++)
scanf("%d%d%d",&a,&b,&c),g.add(a,b,c);
g.re_bfs();
printf("%d\n",g.ISAP());
}
return ;
}
POJ1273 USACO 4.2.1 Drainage Ditches CodeVS1993草地排水 网络流 最大流 SAP的更多相关文章
- codevs1993 草地排水(最大流)
1993 草地排水 USACO 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在农夫约翰的农场上,每逢下雨,Bes ...
- codevs1993草地排水(最大流)
最近学了最大流,于是去codevs找了几道最大流裸题(这是我第一次写网络流). 题目大意:求一个图的最大流(就是这样的裸题) 第一次A网络流的题,发个博客纪念一下. var n,m,i,j,k,h,t ...
- USACO Section 4.2 Drainage Ditches(最大流)
最大流问题.ISAP算法.注意可能会有重边,不过我用的数据结构支持重边.距离d我直接初始化为0,也可以用BFS逆向找一次. -------------------------------------- ...
- USACO Section 4.2: Drainage Ditches
最大流的模板题 /* ID: yingzho1 LANG: C++ TASK: ditch */ #include <iostream> #include <fstream> ...
- Drainage Ditches - poj 1273(网络流模板)
题意:1是源点,m是汇点,求出来最大流量,没什么好说的就是练习最大流的模板题 ************************************************************* ...
- POJ 1273 Drainage Ditches【图论,网络流】
就是普通的网络流问题,想试试新学的dinic算法,这个算法暑假就开始看国家集训队论文了,之前一直都只用没效率的EK算法,真正学会这个算法还是开学后白书上的描述:dinic算法就是不断用BFS构建层次图 ...
- 【codevs1993】草地排水(最大流)
最近学了最大流,于是去codevs找了几道最大流裸题(这是我第一次写网络流). 题目大意:求一个图的最大流(就是这样的裸题) 第一次A网络流的题,发个博客纪念一下. var n,m,i,j,k,h,t ...
- 【Codevs1993】草地排水(最大流,Dinic)
题意:在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水 ...
- poj1273 Drainage Ditches Dinic最大流
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 76000 Accepted: 2953 ...
随机推荐
- LA 3263 (欧拉定理)
欧拉定理题意: 给你N 个点,按顺序一笔画完连成一个多边形 求这个平面被分为多少个区间 欧拉定理 : 平面上边为 n ,点为 c 则 区间为 n + 2 - c: 思路: 先扫,两两线段的交点,存下来 ...
- 前端 -----jQuery的选择器
02-jQuery的选择器 我们以前在CSS中学习的选择器有: 今天来学习一下jQuery 选择器. jQuery选择器是jQuery强大的体现,它提供了一组方法,让我们更加方便的获取到页面中的元 ...
- Python-Pool类
目录: multiprocessing模块 Pool类 apply apply_async map close terminate join 进程实例 multiprocessing模块 如果你打算编 ...
- WebSocket异步通讯,实时返回数据相关问题论坛
https://stackoverflow.com/questions/23773407/a-websockets-receiveasync-method-does-not-await-the-ent ...
- Confluence 6 访问日志脚本
日志访问脚本在连接:https://confluence.atlassian.com/download/attachments/133267635/Atlassian-accessLogScripts ...
- Confluence 6 关于 Decorators
Confluence 是使用开源的 SiteMesh 库构建的.一个 Web 页面的布局系统,这个布局系统能够在全站点中提供完整统一的界面和外观.SiteMesh 是通过 "decorato ...
- nodejs之crypto加密算法
示例 const crypto = require('crypto'); const hash = crypto.createHash('sha256'); hash.update('some dat ...
- 图书管理系统(无中间件,用装饰器的)-----未基于FORM组件
目的:实现图书的增删改查 models.py from django.db import models # Create your models here. class Book(models.Mod ...
- 操作dom获取datatable中的某一行的某一列的数据
需求描述:编辑的时候,点击的那一行,进入后台的验证方法,验证通过后,再进入编辑页面,进入的时候需要把本行<tr>数据中的某一列<td>的值传递过去 思路表述:之前我想的是,给列 ...
- 【kafka】生产者速度测试
非常有用的参考博客:http://blog.csdn.net/qq_33160722/article/details/52903380 pykafka文档:http://pykafka.readthe ...