SPOJ 4110 Fast Maximum Flow (最大流模板)
题目大意:
无向图,求最大流。
算法讨论:
Dinic可过。终于我的常数还是太大。以后要注意下了。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll; struct MF{
static const int N = + ;
static const int M = + ;
static const ll oo = 10000000000000LL; int n, m, s, t, tot, tim;
int first[N], next[M];
int u[M], v[M], cur[N], vi[N];
ll cap[M], flow[M], dis[N];
int que[N + N]; void Clear(){
tot = ;tim = ;
for(int i = ; i <= n; ++ i) first[i] = -;
}
void Add(int from, int to, ll cp, ll flw){
u[tot] = from; v[tot] = to; cap[tot] = cp; flow[tot] = flw;
next[tot] = first[u[tot]];
first[u[tot]] = tot;
++ tot;
}
bool bfs(){
++ tim;
dis[s] = ;vi[s] = tim; int head, tail;
head = tail = ;
que[head] = s;
while(head <= tail){
for(int i = first[que[head]]; i != -; i = next[i]){
if(vi[v[i]] != tim && cap[i] > flow[i]){
vi[v[i]] = tim;
dis[v[i]] = dis[que[head]] + ;
que[++ tail] = v[i];
}
}
++ head;
}
return vi[t] == tim;
}
ll dfs(int x, ll a){
if(x == t || a == ) return a;
ll flw = , f;
int &i = cur[x];
for(i = first[x]; i != -; i = next[i]){
if(dis[x] + == dis[v[i]] && (f = dfs(v[i], min(a, cap[i]-flow[i]))) > ){
flow[i] += f; flow[i^] -= f;
a -= f; flw += f;
if(a == ) break;
}
}
return flw;
}
ll MaxFlow(int s, int t){
this->s = s;this->t = t;
ll flw = ;
while(bfs()){
for(int i = ; i <= n; ++ i) cur[i] = ;
flw += dfs(s, oo);
}
return flw;
}
}Net;
int n, m; int main(){
int x, y;
ll z;
scanf("%d%d", &n, &m);
Net.n = n;
Net.Clear();
for(int i = ; i <= m; ++ i){
scanf("%d%d%lld", &x, &y, &z);
Net.Add(x, y, z, );
Net.Add(y, x, z, );
}
printf("%lld\n", Net.MaxFlow(,Net.n));
return ;
}
SPOJ 4110
SPOJ 4110 Fast Maximum Flow (最大流模板)的更多相关文章
- SPOJ 4206 Fast Maximum Matching (二分图最大匹配 Hopcroft-Carp 算法 模板)
题目大意: 有n1头公牛和n2头母牛,给出公母之间的m对配对关系,求最大匹配数.数据范围: 1 <= n1, n2 <= 50000, m <= 150000 算法讨论: 第一反应 ...
- Flow Problem(最大流模板)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- hdu 3549 Flow Problem(最大流模板题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem Description Network flow is a well-known ...
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...
- 图论算法-最小费用最大流模板【EK;Dinic】
图论算法-最小费用最大流模板[EK;Dinic] EK模板 const int inf=1000000000; int n,m,s,t; struct node{int v,w,c;}; vector ...
- ZOJ_2314_Reactor Cooling_有上下界可行流模板
ZOJ_2314_Reactor Cooling_有上下界可行流模板 The terrorist group leaded by a well known international terroris ...
- [转载]Maximum Flow: Augmenting Path Algorithms Comparison
https://www.topcoder.com/community/data-science/data-science-tutorials/maximum-flow-augmenting-path- ...
- [Algorithm] Maximum Flow
Ref MIT: lecture-13-incremental-improvement-max-flow-min-cut/ Ford Fulkerson algorithm for finding m ...
- Drainage Ditches---hdu1532(最大流, 模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 最大流模板题: EK:(复杂度为n*m*m); #include<stdio.h> ...
随机推荐
- Jquery对选取到的元素显示指定的长度,对于的字符串用“...”显示
$(function() { $(".video_name").each(function() { var s = $(this).text(); $()); }); }); fu ...
- JSP EL
一.JSP EL语言定义 E L(Expression Language) 目的:为了使JSP写起来更加简单. 表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 ...
- Unity3D TouchScript 插件教程一
只是个人学习小记,谈不上教程,但是为了命中搜索引擎关键词,只好装逼了:),可能对于大家来说太简单了吧,网上中文教程没搜到 ,只好自己摸索了. 插件资源下载地址:https://www.assetsto ...
- 关于Android界面编程与视图(View)组件
UI组件--------------->android.widget.* View组件------------->android.view.* 视图(View)组件 所有UI组件都是建立在 ...
- PHP之CI框架架设错误--Only variable references should be returned by reference
解决参考 http://www.rafalinux.com/ La búsqueda fue bastante infructuosa, y hasta hace un par de días, na ...
- Windows 8.1 Update1 6610 32位/64位下载、安装和新增功能简评
今天,微软已经确认完成Windows 8.1 2014 Update RTM正式版的开发工作,累计修复99%的已知bug.随后,微软会将Win8.1首个春季更新正式版,即Win8.1 2014 Upd ...
- 《Programming WPF》翻译 第5章 4.元素类型样式
原文:<Programming WPF>翻译 第5章 4.元素类型样式 命名样式非常有用,当你得到一组属性并应用到特点的元素上.然而,如果你想要应用一个统一的样式到所有确定元素类型的实例, ...
- Linux 使用yum工具
Red Hat 发行版安装后无法使用yum,须注册方可使用,可以通过以下方式处理:cd /etc/yum.repos.d/备份目录下已经存在的repo文件,然后新建文件local.repo,具体脚本如 ...
- devStack for Openstack dev Env
devstack是一套用来给开发人员快速部署Openstack开发环境的脚本,其实对于整个安装过程没有什么好说的,因为脚本写的很完善,全程无脑式安装也没什么大问题,但是因为公司里的网络环境不给力,我的 ...
- 主题简介 ASP .NET
由控件的外观.样式组成的集合,由一个文件组构成,存放在App_Themes文件夹下. 主题包括:皮肤文件(.Skin).CSS文件(.CSS).图片.其它资源等. 主题的作用:统一设置Web页面的外观 ...