Dinic算法——重述
赛前赛后算是第三次接触Dinic算法了,每一次接触都能有种很好的感觉,直男的我没法描述~~
已经比较懂得DInic的基本算法思想了
首先是bfs进行进行分层处理,然后dfs寻找分层后的最大流,在这其中做好正向边流量和反向边流量的优化处理
bfs依旧是比较的简单,维护flor的数组
dfs依旧是Dinic的精华
int dfs(int s,int t,int value)
{
int ret = value;
if(s == t || value == )return value; int a; for(int &i = cur[s];~i;i = e[i].pre)
{
int to = e[i].to;
int cost = e[i].cost;
if(flor[to] == flor[s] + && (a = dfs(to,t,min(ret,e[i].cost))))
{
e[i].cost -= a;
e[i ^ ].cost += a;
ret -= a;
if(ret == )break;
}
} if(ret == value)flor[s] = ;
return value - ret;
}
从s到t,value为其路径上的最大流量值
然后访问分层后的边(仅仅访问一次)往下继续留,递归回来的时候进行正向边和反向边的流量优化
最最后面删点,删去此次分层后被截流的点 然后根据优化后的网络流继续分层,直到分层结束。、、
/*
Dinic算法求取最小费用最大流问题
必要的概念:
前向弧:方向与链正方向一致的弧——p+
后向弧:方向与链反方向一致的弧——p-
增广路:也称为可改进路径,对于一条可行流,存在p
满足p的所有前向弧都是非饱和的,
p的所有后向弧都是非零流弧
割:去除摸一个点后使基图不在连通,
也就是将原来的两个点集划分为两个子集S和T
s-t割:分割后的两个子集,远点s属于S,汇点t属于T
s-t割后的弧u-v,u属于sv属于t的为前向弧,反之为后向弧
割的容量:前向弧的容量和
最小割:使容量最小的割
*/
/*
最大流
也就是寻找增广路
1.现在是零流
2.(当前情况下)寻找一条路,从s到t,并且每一段的流量都小于容量(并不是小于等于!!流量代表的是当前的流量,如果想等就不再可行)
3.那么找到这条路上的min(容量 - 流量),然后每一段的流量都加上delta(这就叫增广路)
4.重复步骤二,知道找不到增广路
以上为基础算法思想
*/
/*
逐步的优化
反向边:为了求出最大流
反向边的出现给了流浪一个反向的几回,虽然有重复的部分但是能确保反向增广后,可以得到两条独一无二的链!
在说的明白一点反向的可行性是建立在可以退流的基础上,像二分图匹配似的
*/
/*
Dinic算法
1》初始化容量网络和网络流
2》构造残留网络和层次网络,若汇点不在层次网络之中结束
3》对层次网络进行DFS,进行增广
4》重复步骤二
*/
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#define inf 0xffffff
using namespace std;
const int maxn = 220;
const int maxm = 4e4 + 4e2;
int n,m;
struct node{
int pre;
int to,cost;
}e[maxm];
int id[maxn],cnt;
int flor[maxn];
void init()
{
memset(id,-1,sizeof(id));
cnt = 0;
}
int cur[maxn];//DFS的优化遍历
void add(int from,int to,int cost)
{
e[cnt].to = to;
e[cnt].cost = cost;
e[cnt].pre = id[from];
id[from] = cnt++;
swap(from,to);
e[cnt].to = to;
e[cnt].cost = 0;
e[cnt].pre = id[from];
id[from] = cnt++;
//可以用异或去取他的反向边
//偶数为正向,奇数为反向
}
int bfs(int s,int t)
{
memset(flor,0,sizeof(flor));
queue<int>q;
while(q.size())q.pop();
flor[s] = 1;//flor还充当vis数组,所以从1层开始分层
q.push(s);
while(q.size())
{
int now = q.front();q.pop();
for(int i = id[now];~i;i = e[i].pre)
{
int to = e[i].to;
int cost = e[i].cost;
if(flor[to] == 0 && cost > 0)
{
flor[to] = flor[now] + 1;
q.push(to);
if(to == t)return 1;
}
}
}
return 0;
}
int dfs(int s,int t,int value)
{
int ret = value;
if(s == t || value == 0)return value;
int a;
for(int &i = cur[s];~i;i = e[i].pre)
{
int to = e[i].to;
int cost = e[i].cost;
if(flor[to] == flor[s] + 1 && (a = dfs(to,t,min(ret,e[i].cost))))
{
e[i].cost -= a;
e[i ^ 1].cost += a;
ret -= a;
if(ret == 0)break;
}
}
if(ret == value)flor[s] = 0;
return value - ret;
}
int Dinic(int s,int t)
{
int ret = 0;
while(bfs(s,t))
{
memcpy(cur,id,sizeof(id));
// for(int i = 0;i < n;i++)
// {
// cur[i] = id[i];
// }
ret += dfs(s,t,inf);
//cout<<"cs"<<endl;
}
return ret;
}
int main()
{
/*
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
*/
while(~scanf("%d%d",&m,&n))
{
init();
int a,b,x;
for(int i = 1;i <= m;i++)
{
scanf("%d%d%d",&a,&b,&x);
add(a,b,x);
}
int ret = Dinic(1,n);
printf("%d\n",ret);
}
return 0;
}
/*不知道什么时候的事了,博客园的代码插入位置咋不能在光标之后了,一开始就是因为CSDN的不好编辑才用的博客园哎~~*/
Dinic算法——重述的更多相关文章
- ACM/ICPC 之 Dinic算法(POJ2112)
Optimal Milking //二分枚举最大距离的最小值+Floyd找到最短路+Dinic算法 //参考图论算法书,并对BFS构建层次网络算法进行改进 //Time:157Ms Memory:65 ...
- ISAP算法对 Dinic算法的改进
ISAP算法对 Dinic算法的改进: 在刘汝佳图论的开头引言里面,就指出了,算法的本身细节优化,是比较复杂的,这些高质量的图论算法是无数优秀算法设计师的智慧结晶. 如果一时半会理解不清楚,也是正常的 ...
- [知识点]网络流之Dinic算法
// 此博文为迁移而来,写于2015年2月6日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vrg4.html ...
- 学习笔记 --- 最大流Dinic算法
为与机房各位神犇同步,学习下网络流,百度一下发现竟然那么多做法,最后在两种算法中抉择,分别是Dinic和ISAP算法,问过 CA爷后得知其实效率上无异,所以决定跟随Charge的步伐学习Dinic,所 ...
- Power Network(网络流最大流 & dinic算法 + 优化)
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24019 Accepted: 12540 D ...
- HDU 1532 (Dinic算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意: 就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络 ...
- poj 1459 Power Network : 最大网络流 dinic算法实现
点击打开链接 Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 20903 Accepted: ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- 【BZOJ】1001: [BeiJing2006]狼抓兔子 Dinic算法求解平面图对偶图-最小割
1001: [BeiJing2006]狼抓兔子 Description 左上角点为(1,1),右下角点为(N,M)(上图中N=4,M=5).有以下 三种类型的道路 1:(x,y)<==>( ...
随机推荐
- 839A Arya and Bran
A. Arya and Bran time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- node.js 在使用child_process 模块时候,调试端口占用的问题解决方案(EADDRINUSE)
在fork的时候,带参数{ execArgv: ['--debug=' + (process.debugPort + 1)] }
- Luogu 3424 [POI2005]SUM-Fibonacci Sums
Solution 没有任何算法, 只要会$for$ 就能AC... 我们观察到, 如果有一个位置 的$F_i$ 的系数$b_i$ 为2, 那么只需要把 $b_{i-2}+1,b_{i+1}+1$即可. ...
- 需求文件requirements.txt的创建及使用
pip freeze >requirements.txt pip install -r requirements.txt
- Servlet会话管理一(URL重写和表单隐藏域)
会话可以简单的理解为客户端用户打开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器的整个过程称为一个会话.即一个客户端用户和服务器端进行通讯的过程,也是客户端和服务器端之间的数据传 ...
- Subarray Sum Equals K LT560
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- linux 查看信息-服务器相关
查看系统内核 查看磁盘信息 查看CPU的信息 查看内存相关信息
- IOS初级:UIwindow
AppDelegate.h @property (strong, nonatomic) UIWindow *window; AppDelegate.m - (BOOL)application:(UIA ...
- .net写本地文件的一个方法
整理代码,.net在本地写html文件的一个方法,代码如下 public static void WriteFile(string FilePath, string FileInfo, string ...
- centos配置备忘(apache\php\mysql)
1. 安装apache\php\mysql=======================================yum -y install httpd php mysql mysql-ser ...