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> ...
随机推荐
- webform 验证控件
验证: 一.非空验证 RequiredFieldValidator ErrorMessage - 验证出错后的提示信息 ControlToValidate - 要验证的控件的ID Display - ...
- PHP 日期格式化 参数参考
a - "am" 或是 "pm" A - "AM" 或是 "PM" d - 几日,二位数字,若不足二位则前面补零; 如: ...
- 如何debug android cts
启动和关闭ADB服务(adb start-server和adbkill-server) 经作者测试,模拟器在运行一段时间后,adb服务有可能(在Windows进程中可以找到这个服务,该服务用来为模拟器 ...
- Android平台之不预览获取照相机预览数据帧及精确时间截
在android平台上要获取预览数据帧是一件极其容易的事儿,但要获取每帧数据对应的时间截并不那么容易,网络上关于这方面的资料也比较少.之所以要获取时间截,是因为某些情况下需要加入精确时间轴才能解决问题 ...
- tp28xx port pin (open-drain )and (push-pull) 和open collector)
具有开漏(OD)输出的器件是指内部输出和地之间有个N沟道的MOSFET(T1),这些器件可以用于电平转换的应用.输出电压由Vcc'决定.Vcc'可以大于输入高电平电压VCC(up-translate) ...
- Eclipse-cdt 配合 gdbserver 进行 arm 程序远程调试 下
上一篇中,介绍了如何编译.运行arm-linux-gdb 和 gdbserver,这一篇中介绍怎样结合Eclipse-cdt进行图形化编译调试 首先当然是使用CDT创建项目并添加源文件. 在左侧 Pr ...
- 【剑指offer】面试题31:连续子数组的最大和
题目: 在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2, ...
- Longest Consecutive Sequence 解答
Question Given an unsorted array of integers, find the length of the longest consecutive elements se ...
- Makefile中使用foreach生成一类规则
CSDN上,有朋友发帖问了这样一个问题(我按自己的理解翻译一下): 当前目录下有四个静态库文件: liba.a libb.a libc.a libd.a.现在想将它们做成一个动态库libp.so. ...
- Hive 11、Hive嵌入Python
Hive嵌入Python Python的输入输出都是\t为分隔符,否则会出错,python脚本输入print出规定格式的数据 用法为先add file,使用语法为TRANSFORM (name, it ...