最大流模版 dinic
朴素dinic+多路增广
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXM=100005,MAXN=10005;
int init(){
int rv=0,fh=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') fh=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
rv=(rv<<1)+(rv<<3)+c-'0';
c=getchar();
}
return rv*fh;
}
queue <int> q;
int n,nume,m,s,t,head[MAXN],dep[MAXN];
struct edge{
int to,nxt,cap,flow;
}e[MAXM<<1];
void adde(int from,int to,int cap){
e[++nume].to=to;
e[nume].nxt=head[from];
head[from]=nume;
e[nume].cap=cap;
}
bool bfs(){
while(!q.empty()) q.pop();
memset(dep,0,sizeof(dep));
dep[s]=1;q.push(s);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if(!dep[v]&&e[i].flow<e[i].cap){
dep[v]=dep[u]+1;
q.push(v);
}
}
}
if(dep[t]) return 1;
else return 0;
}
int dfs(int u,int flow){
if(u==t) return flow;
int tot=0;
for(int i=head[u];i&&tot<flow;i=e[i].nxt){
int v=e[i].to;
if((dep[v]==dep[u]+1)&&(e[i].flow<e[i].cap)){
if(int t=dfs(v,min(flow-tot,e[i].cap-e[i].flow))){
e[i].flow+=t;
e[((i-1)^1)+1].flow-=t;
tot+=t; //不return ,多路增广
}
}
}
return tot;
}
int main(){
n=init();m=init();s=init();t=init();
for(int i=1;i<=m;i++){
int u=init(),v=init(),cap=init();
adde(u,v,cap);
adde(v,u,0);
}
int ans=0;
while(bfs()){
ans+=dfs(s,0x3f3f3f3f);
}
cout<<ans<<endl;
}
当前弧优化+多路增广
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
const int MAXN = 10005;
int init() {
int rv = 0, fh = 1;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') fh = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
rv = (rv<<1) + (rv<<3) + c - '0';
c = getchar();
}
return fh * rv;
}
int head[MAXN], n, m, ss, tt, dep[MAXN], cur[MAXN], maxflow, nume;
struct edge{
int to, nxt, cap, flow;
}e[MAXN * 20];
void adde(int from, int to, int cap) {
e[++nume].to = to;
e[nume].cap = cap;
e[nume].nxt = head[from];
head[from] = nume;
}
queue<int> q;
bool bfs() {
memset(dep, 0, sizeof(dep));
q.push(ss); dep[ss] = 1;
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if(!dep[v] && e[i].flow < e[i].cap) {
dep[v] = dep[u] + 1;
q.push(v);
}
}
}
if(dep[tt]) return 1;
else return 0;
}
int dfs(int u, int flow) {
if(u == tt) return flow;
int tot = 0;
for(int &i = cur[u]; i && tot < flow; i = e[i].nxt) {
int v = e[i].to;
if((dep[v] == dep[u] + 1) && (e[i].flow < e[i].cap)) {
if(int t = dfs(v, min(flow - tot, e[i].cap - e[i].flow))) {
e[i].flow += t;
e[((i - 1) ^ 1) + 1].flow -= t;
tot += t;
}
}
}
return tot;
}
void dinic() {
while(bfs()) {
for(int i = 1; i <= n; i++) cur[i] = head[i];
maxflow += dfs(ss, inf);
}
}
int main() {
n = init(); m = init(); ss = init(); tt = init();
for(int i = 1; i <= m; i++) {
int u = init(), v = init(), cap = init();
adde(u, v, cap); adde(v, u, 0);
}
dinic();
cout << maxflow << endl;
return 0;
}
最大流模版 dinic的更多相关文章
- 最大流模版 pascal
//最大流模版 ; maxm=; ..maxn] of integer; end; var n,m,max:longint; r:..maxn,..maxn] of longint; g:..maxn ...
- 【Codevs1993】草地排水(最大流,Dinic)
题意:在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水 ...
- POJ 1459 Power Network(网络最大流,dinic算法模板题)
题意:给出n,np,nc,m,n为节点数,np为发电站数,nc为用电厂数,m为边的个数. 接下来给出m个数据(u,v)z,表示w(u,v)允许传输的最大电力为z:np个数据(u)z,表示发电 ...
- poj1459 Power Network --- 最大流 EK/dinic
求从电站->调度站->消费者的最大流,给出一些边上的容量.和电站和消费者能够输入和输出的最大量. 加入一个超级源点和汇点,建边跑模板就能够了. 两个模板逗能够. #include < ...
- 网络流入门—用于最大流的Dinic算法
"网络流博大精深"-sideman语 一个基本的网络流问题 最早知道网络流的内容便是最大流问题,最大流问题很好理解: 解释一定要通俗! 如右图所示,有一个管道系统,节点{1,2,3 ...
- 浅谈最大流的Dinic算法
PART 1 什么是网络流 网络流(network-flows)是一种类比水流的解决问题方法,与线性规划密切相关.网络流的理论和应用在不断发展,出现了具有增益的流.多终端流.多商品流以及网络流的分解与 ...
- 【最大流之Dinic算法】POJ1273 【 & 当前弧优化 & 】
总评一句:Dinic算法的基本思想比较好理解,就是它的当前弧优化的思想,网上的资料也不多,所以对于当前弧的优化,我还是费了很大的功夫的,现在也一知半解,索性就写一篇博客,来发现自己哪里的算法思想还没理 ...
- HDU1532最大流 Edmonds-Karp,Dinic算法 模板
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 网络最大流算法—Dinic算法及优化
前置知识 网络最大流入门 前言 Dinic在信息学奥赛中是一种最常用的求网络最大流的算法. 它凭借着思路直观,代码难度小,性能优越等优势,深受广大oier青睐 思想 $Dinic$算法属于增广路算法. ...
随机推荐
- 爬 NationalData ,虽然可以直接下,但还是爬一下吧
爬取的是分省月度数据,2017年的,包括:居民消费价格指数,食品烟酒类居民消费价格指数,衣着类居民消费价格指数,居住类居民消费价格指数,生活用品及服务类居民消费价格指数,交通和通信类居民消费价格指数, ...
- hadoop2.6.0集群搭建
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- 将项目(代码)从GitHub上克隆(下载)到本地仓库
要将项目从GitHub上克隆到本地,首先你得下载并安装好git for window. 下载地址:http://www.xp510.com/xiazai/Application/other/30988 ...
- Lucene学习笔记2-Lucene的CRUD(V7.1)
在进行CRUD的时候请注意IndexWriterConfig的设置. public class IndexCRUD { "}; private String citys[]={"j ...
- 访问远程MySQL数据库的方法
请问各位部署LAMP的时候MySQL是独立出来的服务器,在apache上编译安装php的时候有个--with-mysql后面应该是带mysql路径的,可我应该怎样把这个连接到mysql服务器,因为不是 ...
- 遇到安装app不识别的情况
一般->blokfile->证书设定设置
- JAVA 一句话技巧
1.拆分字符串 遇到特殊字符,比如:对‘$’符号,就应该使用‘\\$’,后总结可以加个方括号如 "[.]".2.遍历HASHMAP Iterator itr = map.keySe ...
- MYsql优化where子句
该部分讨论where子句的优化,不仅select之中,相同的优化同样试用与delete 和update语句中的where子句: 1: 移去不必要的括号: ((a AND b) AND c OR ((( ...
- ASP.NET MVC5 中百度ueditor富文本编辑器的使用
随着网站信息发布内容越来越多,越来越重视美观,富文本编辑就是不可缺少的了,众多编辑器比较后我选了百度的ueditor富文本编辑器. 百度ueditor富文本编辑器分为两种一种是完全版的ueditor, ...
- MySQL查看和修改表的存储引擎
1 查看系统支持的存储引擎 show engines; 2 查看表使用的存储引擎 两种方法: a.show table status from db_name where name='table_na ...