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> ...
随机推荐
- PDO LIMIT bug
PDO存在一个LIMIT BUG(mysql) 需要指定数据类型,而且limit后面跟的2个参数必须是数值类型,不然的话获取不到数据 例1: $dsn = "mysql:host=127.0 ...
- C++的学习记录 - 0
最近玩Arduino发现,在编写函数库的时候要用到C++.正好手头有一本教材,于是时隔2年,开始重学. 又看到重载.构造.拷贝这些词竟然还有些小兴奋. 开个系列日志记录一下学习过程中的问题和体会. 看 ...
- android布局常用属性记录
android布局常用属性记录 http://blog.csdn.net/xn4545945/article/details/7717086这里有一部分别人总结的其余的: align:对齐 par ...
- 使用apache的ab对页面进行压力测试
ab是apache自带的一个很好用的压力测试工具,当安装完apache的时候,就可以在bin下面找到ab 我们可以模拟100个并发用户,对一个页面发送1000个请求 ./ab -n1000 -c100 ...
- Android Support Library package简介
转自http://my.oschina.net/chengliqun/blog/148451 N久未做android了,以前做的时候,2.2才刚出来,现在android都更新到了4.3了,而从前一段时 ...
- Java Web开发及应用软件方向的学习计划
从接触计算机以来,一直抱有很浓厚的兴趣.我并不擅长与人交际,与机器对话可能更有性格方面的优势.虽然我很想做出一些改变,但总得需要时间和历练,暂时也只能这样了~ 一直很向往代码的神秘,在梦之站待过两年时 ...
- PowerShell_零基础自学课程_8_高级主题:WMI对象和COM组件
本系列文章从最初的初识开始,基本上可以完成一些简单的系统管理了,为了更方便的管理系统,同时为了更好的发掘系统的性能,就需要用到系统提供 的一些高级特性,在Windows Server系列的OS中,如果 ...
- tyvj1297 小气的小B
描述 其实你们都不知道,小B是很小气的.一天小B带着他的弟弟小B'一起去摘果子,走着走着,他们忽然发现了一颗长满了果子的树.由于弟弟长得太矮了,弟弟只有让哥哥小B帮他摘一些果子下来.哥哥小B说:&qu ...
- python高级编程之(类级):子类内建类型
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #类级 #在2.2中,提出了类型(type0与类(class)统一( ...
- 简易的C/S系统(实现两个数的和)
//Client:#include <string.h> #include <sys/socket.h> #include <stdio.h> #include & ...