最大流加强 dinic+当前弧优化
qyy开始练习网络流啦 , 啊 ,蒟蒻只会套版 ,很裸的题 , 我连题都不想发了 ,可以参考我的代码(虽然我也是看的别人的
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <queue>
const int N = + , M = + ;
using namespace std ;
int n , m ;
int tot , head[N] ;
long long ans;
struct id
{
int to , nxt ;
long long val ;
} edge[M<<] ;
queue< int > Q;
int dis[N] ,cur[N]; inline void Init( )
{
freopen( "NSOOJ10477.in" , "r" , stdin ) ;
freopen( "NSOOJ10477.out" , "w" , stdout ) ;
} int read( )
{
char ch = getchar( ) ; int k = , ret = ;
while( ch > '' || ch < '' ) { if( ch == '-' ) k = - ; ch = getchar( ) ; }
while( ch <= '' && ch >= '' ) ret = ret * + ch - '' , ch = getchar( ) ;
return ret * k ;
} void add( int u , int v , int c )
{
edge[++tot].to = v , edge[tot].nxt = head[u] ;
edge[tot].val = c , head[u] = tot ;
} void input( )
{
n = read( ) , m = read( ) ;
memset( head , - , sizeof( head ) ) ;
tot = ;
for( int x = ; x <= m ; ++x )
{
int a, b , c ;
a = read( ) , b = read( ) , c = read( ) ;
add( a, b , c ) ;
add( b , a , ) ;
}
} bool bfs( )
{
memset( dis , - , sizeof(dis) ) ;
Q.push( ) ; dis[] = ;
while( !Q.empty( ) )
{
int u = Q.front( ) ; Q.pop( ) ;
for( int i = head[u] ; ~i ; i = edge[i].nxt )
{ int v = edge[i].to ;
if( dis[v] < && edge[i].val > )
{
dis[v] = dis[u] + ; Q.push( v ) ;
} }
}
// cout<<endl;
if( dis[n] > ) return true ;
return false ;
} long long int dfs( int u , int inf )
{
long long int ans = 0ll , cost = 0ll;
if( u == n ) return inf ;
for( int i = cur[u] ; ~i ; i = edge[i].nxt )
{
int v = edge[i].to ;
if( dis[v] != dis[u] + ) continue ;
ans = dfs( v , min( inf - cost , edge[i].val ) ) ;
edge[i].val -= ans ;
edge[i^].val += ans ;
if( edge[i].val ) cur[u] = i ;
cost += ans ;
if( cost == inf ) return inf ;
}
if( !cost ) dis[u] = - ;
return cost ;
} void sov( )
{
ans = ;
while( bfs( ) )
{
int now ;
for( int i = ; i <= n ; ++i ) cur[i] = head[i] ;
ans += dfs( , << ) ;
}
printf( "%lld" , ans ) ;
} int main( )
{
// Init( ) ;
input( ) ;
sov( ) ;
// fclose( stdin ) ;
// fclose( stdout ) ;
return ;
}
重新贴板,和上面的程序几乎没什么区别,码风固定了,值得开心。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
const int N = , M = 3e5 + , inf = << ;
using namespace std;
int n, m, head[N], cnt, dis[N],cur[N];
queue<int>Q;
struct edge
{
int nxt,to,vi;
} E[M<<]; void add(int u,int v,int vi)
{
E[++cnt] = (edge){head[u],v,vi}; head[u] = cnt;
E[++cnt] = (edge){head[v],u,}; head[v] = cnt;
} void Init()
{
scanf("%d%d",&n,&m); cnt = -;
int u,v,vi;
memset(head,-,sizeof(head));
for(int i = ; i <= m; ++i)
{
scanf("%d%d%d",&u,&v,&vi);
add(u,v,vi);
}
} bool bfs()
{
memset(dis,-,sizeof(dis));
Q.push(); dis[] = ;
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(dis[v] == - && E[i].vi > ) dis[v] = dis[u] + , Q.push(v);
}
}
return (dis[n] > );
} int dfs(int u,int t,int ff)
{
int ret = , cost = ;
if(u == t) return ff;
for(int i = cur[u] ; ~i; i = E[i].nxt)
{
int v = E[i].to;
if(dis[v] != dis[u] + ) continue;
cost = dfs(v,t,min(ff-ret,E[i].vi));
E[i].vi -= cost, E[i^].vi += cost; ret += cost;
if(E[i].vi) cur[u] = i;
if(ret == ff) return ff;
}
if(!ret) dis[u] = -;
return ret;
} int max_flow(int s,int t)
{
int ret = ;
while(bfs())
{
for(int i = ; i <= n; ++i) cur[i] = head[i];
ret += dfs(s,t,inf);
}
return ret;
} void Solve()
{
int ans = max_flow(,n);
printf("%d\n",ans);
} int main()
{
Init();
Solve(); return ;
}
最大流加强 dinic+当前弧优化的更多相关文章
- ARC085E(最小割规划【最大流】,Dinic当前弧优化)
#include<bits/stdc++.h>using namespace std;typedef long long ll;const ll inf=0x3f3f3f3f;int cn ...
- P3376 网络流-最大流模板题(Dinic+当前弧优化)
(点击此处查看原题) Dinic算法 Dinic算法相对于EK算法,主要区别在于Dinic算法对图实现了分层,使得我们可以用一次bfs,一次dfs使得多条增广路得到增广 普通的Dinic算法已经可以处 ...
- [Poj2112][USACO2003 US OPEN] Optimal Milking [网络流,最大流][Dinic+当前弧优化]
题意:有K个挤奶机编号1~K,有C只奶牛编号(K+1)~(C+K),每个挤奶机之多能挤M头牛,现在让奶牛走到挤奶机处,求奶牛所走的最长的一条边至少是多少. 题解:从起点向挤奶机连边,容量为M,从挤奶机 ...
- Dinic当前弧优化 模板及教程
在阅读本文前,建议先自学最大流的Ek算法. 引入 Ek的核心是执行bfs,一旦找到增广路就停下来进行增广.换言之,执行一遍BFS执行一遍DFS,这使得效率大大降低.于是我们可以考虑优化. 核心思路 在 ...
- 网络最大流算法—Dinic算法及优化
前置知识 网络最大流入门 前言 Dinic在信息学奥赛中是一种最常用的求网络最大流的算法. 它凭借着思路直观,代码难度小,性能优越等优势,深受广大oier青睐 思想 $Dinic$算法属于增广路算法. ...
- HDU 4280 Island Transport(dinic+当前弧优化)
Island Transport Description In the vast waters far far away, there are many islands. People are liv ...
- 【Luogu】P1231教辅的组成(拆点+Dinic+当前弧优化)
题目链接 妈耶 我的图建反了两次 准确的说是有两个地方建反了,然后反上加反改了一个小时…… 知道为什么要拆点吗? 假设这是你的图 左边到右边依次是超级源点 练习册 书 答案 ...
- 网络流小记(EK&dinic&当前弧优化&费用流)
欢 迎 来 到 网 络 瘤 的 世 界 什么是网络流? 现在我们有一座水库,周围有n个村庄,每个村庄都需要水,所以会修水管(每个水管都有一定的容量,流过的水量不能超过容量).最终水一定会流向唯一一个废 ...
- 网络流 - dinic + 当前弧优化【代码】
这是初学网络流的时候从<算法竞赛进阶指南>抄下来的一份代码,自己理解的也不是很透彻. 注意,边要从 \(1\) 开始计,不然直接 \(xor\) 运算的话取反边会直接炸掉. #includ ...
随机推荐
- 24种设计模式--适配器模式【Adapter Pattern】
今天讲适配器模式,这个模式也很简单,你笔记本上的那个拖在外面的黑盒子就是个适配器,一般你在中国能用,在日本也能用,虽然两个国家的的电源电压不同,中国是 220V,日本是 110V,但是这个适配器能够把 ...
- 取消IE“已限制此网页运行可以访问计算机的脚本,转自“园封记忆”
为了有利于保护安全性,IE已限制此网页运行可以访问计算机的脚本或 ActiveX 控件.请单击这里获取选项... 方法一: 在本地调试html页,如果其中包含js或flash,IE经常会提示“IE已限 ...
- div section article aside的理解
div 是一个大的容器 内部可以包含header main nav aside footer等标签 没有语义,多用于为脚本添加样式 section的语义比div语义强些,用于主题性比较强的内容,比如一 ...
- centos 7.0防火墙导致vagrant端口映射失败
在vagrant上部署了centos7.0后,Vagrantfile端口转发设置后,宿主机访问客户机站点还是无法访问,问题出在:centos7.0以上版本默认会安装firewalld防火墙, fire ...
- 一些static_cast const_cast
static_cast:干杂活的,那三个都有各自的专有用途,那三个不做的都由这个转型符来做,只要它能做的,用C语法的强制类型转换运算符也一定能够完成:但话又说回来了,C强制类型转换能做的,它可不一定都 ...
- 【python之旅】python的面向对象
一.面向过程 VS 面向对象 1.编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程,一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,实现一个任务的 ...
- python中os模块的常用接口和异常中Exception的运用
1.os.path.join(arg1, arg2) 将arg1和arg2对应的字符串连接起来并返回连接后的字符串,如果arg1.arg2为变量,就先将arg1.arg2转换为字符串后再进行连接. 2 ...
- Yarn应用程序编程实例
Yarn自带的Application示例程序:DistributedShell 和 UnManaged AM1 DistributedShell ,故名思意,是一个分布式运行shell命令的应用程序, ...
- The Perfect Stall
poj1274:http://poj.org/problem?id=1274 题意:有n个奶牛和m个谷仓,现在每个奶牛有自己喜欢去的谷仓,并且它们只会去自己喜欢的谷仓吃东西,问最多有多少奶牛能够吃到东 ...
- QImage与QPixmap加载图片效果(QImage不能拉伸图片,QPixmap默认拉伸图片)
QImage与QPixmap加载图片 效果 . 分类: QT开发 qtQtQT PixmapTest::PixmapTest(QWidget *parent) : QDialog(parent) {t ...