#101. 最大流

内存限制:512 MiB时间限制:5000 ms标准输入输出
题目类型:传统评测方式:文本比较
上传者: 匿名

题目描述

这是一道模板题。

给定 n nn 个点,m mm 条边,给定每条边的容量,求从点 s ss 到点 t tt 的最大流。

输入格式

第一行四个整数 n nn、m mm、s ss、t tt。
接下来的 m mm 行,每行三个整数 u uu、v vv、c cc,表示 u uu 到 v vv,流量为 c cc 的一条边。

输出格式

输出点 s ss 到点 t tt 的最大流。

样例

样例输入

7 14 1 7
1 2 5
1 3 6
1 4 5
2 3 2
2 5 3
3 2 2
3 4 3
3 5 3
3 6 7
4 6 5
5 6 1
6 5 1
5 7 8
6 7 7

样例输出

14

数据范围与提示

1≤n≤106,1≤m≤4×106,0≤c≤231−1 1 \leq n \leq 10 ^ 6, 1 \leq m \leq 4 \times 10 ^ 6, 0 \leq c \leq 2 ^ {31} - 11≤n≤10​6​​,1≤m≤4×10​6​​,0≤c≤2​31​​−1

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define maxm 4000006
#define maxn 1000006
using namespace std;
int read() {
int x=,f=;char ch=getchar();
while(!isdigit(ch)){ch=getchar();}
while(isdigit(ch)){x=x*+ch-'';ch=getchar();}
return x;
}
struct data {
int from,to,next,w;
}e[maxm*];
int head[maxn],cnt;
int cur[maxn];
void add(int u,int v,int w){e[cnt].from=u;e[cnt].next=head[u];e[cnt].to=v;e[cnt].w=w;head[u]=cnt++;}
int n,m,s,t;
int q[maxn];
bool vis[maxn];
int dis[maxn];
bool bfs() {
memset(dis,-,sizeof(dis));
int h=,tt=;
q[h]=t;
vis[t]=;
dis[t]=;
while(h!=tt) {
int now=q[h];h++;vis[now]=;if(h==maxn) h=;
for(int i=head[now];i>=;i=e[i].next) {
int to=e[i].to;
if(e[i^].w&&dis[to]<-) {
dis[to]=dis[now]-;
if(!vis[to]){
vis[to]=;
q[tt++]=to;if(tt==maxn) tt=;
}
}
}
}
return dis[s]>=-;
}
int dfs(int now,int a) {
if(now==t||a==) return a;
int flow=,f;
for(int i=cur[now];i>=;i=e[i].next) {
int to=e[i].to;
if(dis[to]==dis[now]+&&e[i].w>&&(f=dfs(to,min(a,e[i].w)))) {
e[i].w-=f;
e[i^].w+=f;
flow+=f;
a-=f;
if(a==) return flow;
}
cur[now]=i;
}
if(!flow) dis[now]=-;
return flow;
}
int main() {
memset(head,-,sizeof(head));
n=read(),m=read(),s=read(),t=read();
for(int i=;i<=m;i++) {
int u=read(),v=read(),w=read();
add(u,v,w);add(v,u,);
}
int ans=;
while(bfs()){
for(int i=;i<=n;i++) cur[i]=head[i];
ans+=dfs(s,);
}
printf("%d",ans);
}

[loj#101] 最大流 网络流模板的更多相关文章

  1. LOJ 101 最大流(ISAP 模板)

    开long long的最大流 #include<bits/stdc++.h> using namespace std; ;//点数的最大值 ;//边数的最大值 ; struct Edge ...

  2. loj#101. 最大流 dinic+当前弧

    板子题 当前弧优化版本 目前效率最高 //#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize(&q ...

  3. loj 101 最大流

    冬令营送到我脸上的20分都没拿全 心态爆炸 冬令营前一天学的dinic 后一天才发出来 #include<iostream> #include<cstdio> #include ...

  4. 2018.08.20 loj#115. 无源汇有上下界可行流(模板)

    传送门 又get到一个新技能,好兴奋的说啊. 一道无源汇有上下界可行流的模板题. 其实这东西也不难,就是将下界变形而已. 准确来说,就是对于每个点,我们算出会从它那里强制流入与流出的流量,然后与超级源 ...

  5. Drainage Ditches - poj 1273(网络流模板)

    题意:1是源点,m是汇点,求出来最大流量,没什么好说的就是练习最大流的模板题 ************************************************************* ...

  6. Power Network POJ - 1459 [网络流模板]

    http://poj.org/problem?id=1459 嗯,网络流模板...多源点多汇点的图,超级汇点连发电厂,用户连接超级汇点 Status Accepted Time 391ms Memor ...

  7. Drainage Ditches--hdu1532(网络流 模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/Other ...

  8. POJ-2135-Farm Tour(最大费用最小流)模板

    Farm Tour POJ - 2135 When FJ's friends visit him on the farm, he likes to show them around. His farm ...

  9. POJ 1273:Drainage Ditches 网络流模板题

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63339   Accepted: 2443 ...

随机推荐

  1. Retrofit get post query filed FiledMap

    直接请求型 1.如果是直接请求某一地址,写法如下: @GET("/record") Call getResult(); 2.如果是组合后直接请求,如/result/{id}写法如下 ...

  2. Linux下单机安装部署kafka及代码实现

    技术交流群:233513714 这几天研究了kafka的安装及使用,在网上找了很多教程但是均以失败告终,直到最后想起网络方面的问题最终才安装部署成功,下面就介绍一下kafka的安装部署及代码实现 一. ...

  3. svn服务器的配置和使用

    安装好了svn服务器,打开 VisualSVN Server Manager 先新建用户和组,在代码库创建的时候可以指定哪些用户或者组有读写权限,也可以创建好后指定 创建用户 输入用户名和密码创建用户 ...

  4. laravel5.5路由使用name的好处

    使用name的好处 辅助函数 route 可以用于为指定路由生成 URL.命名路由生成的 URL 不与路由上定义的 URL 相耦合.因此,就算路由的 URL 有任何更改,都不需要对 route 函数调 ...

  5. R语言中的机器学习包

    R语言中的机器学习包   Machine Learning & Statistical Learning (机器学习 & 统计学习)  网址:http://cran.r-project ...

  6. DOS程序员手册(十五)

    837页 writeln('TRACING Current Buffer==='); holdup; bcbtrc(cvtbase^.curbfr); writeln; holdup ; writel ...

  7. Visual Studio 提示某个dll文件(已在Microsoft Visual Studio 外对该文件进行了修改,是否重新加载它)

    如题: Visual Studio 提示某个dll文件(已在Microsoft Visual Studio 外对该文件进行了修改,是否重新加载它) 如果选择“是”,那恭喜你,第二次生成的时候,引用这个 ...

  8. 了解JavaScript核心精髓(三)

    1.js判断对象是否存在属性. hasOwnProperty(‘property’)  判断原型属性是否存在. "property" in o;  判断原型属性和原型链属性是否存在 ...

  9. 【转载】全面解析Unity3D自动生成的脚本工程文件

    我们在Unity3D开发的时候,经常会看到它会产生不少固定命名工程文件,诸如:  Assembly-CSharp-vs.csproj  Assembly-CSharp-firstpass-vs.csp ...

  10. ImportError: libcudart.so.8.0: cannot open shared object file: No such file or directory

    这是在python中使用caffe产生的错误. 程序很普通: #-*-coding=utf-8-*- import numpy as npimport matplotlib.pyplot as plt ...