[loj#101] 最大流 网络流模板
#101. 最大流
题目描述
这是一道模板题。
给定 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≤106,1≤m≤4×106,0≤c≤231−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] 最大流 网络流模板的更多相关文章
- LOJ 101 最大流(ISAP 模板)
开long long的最大流 #include<bits/stdc++.h> using namespace std; ;//点数的最大值 ;//边数的最大值 ; struct Edge ...
- loj#101. 最大流 dinic+当前弧
板子题 当前弧优化版本 目前效率最高 //#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize(&q ...
- loj 101 最大流
冬令营送到我脸上的20分都没拿全 心态爆炸 冬令营前一天学的dinic 后一天才发出来 #include<iostream> #include<cstdio> #include ...
- 2018.08.20 loj#115. 无源汇有上下界可行流(模板)
传送门 又get到一个新技能,好兴奋的说啊. 一道无源汇有上下界可行流的模板题. 其实这东西也不难,就是将下界变形而已. 准确来说,就是对于每个点,我们算出会从它那里强制流入与流出的流量,然后与超级源 ...
- Drainage Ditches - poj 1273(网络流模板)
题意:1是源点,m是汇点,求出来最大流量,没什么好说的就是练习最大流的模板题 ************************************************************* ...
- Power Network POJ - 1459 [网络流模板]
http://poj.org/problem?id=1459 嗯,网络流模板...多源点多汇点的图,超级汇点连发电厂,用户连接超级汇点 Status Accepted Time 391ms Memor ...
- Drainage Ditches--hdu1532(网络流 模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/Other ...
- 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 ...
- POJ 1273:Drainage Ditches 网络流模板题
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63339 Accepted: 2443 ...
随机推荐
- Java-数据结构之二叉树练习
本来这个随笔应该在4月17号就应该发出来的.不巧的是,那天晚上收到了offer,然后接下去两天为入职到处跑,20号入职后一直忙,直到最近几天才有时间看看书.然而20多天前就看完的了二叉树,然后17号那 ...
- Eclipse 创建 Java 类---Eclipse教程第10课
打开新建 Java 类向导 你可以使用新建 Java 类向导来创建 Java 类,可以通过以下途径打开 Java 类向导: 点击 "File" 菜单并选择 New > Cla ...
- css一些事儿
1. margin和padding 如果边界画一条线,则margin的属于边界外,padding属于边界内 当我们给元素背景色时,margin区域不会被着色,而padding区域会被着色. 当上下两个 ...
- Java架构师必会的技能
Java架构师必会的技能 我把它分为了五大专题 工程化专题 工程化专题 git git安装使用 git日常使用:fetch/pull/push/revert/rebase git分支管理git flo ...
- USACO Section1.3 Wormholes 解题报告
wormhole解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------- ...
- android的5个进程等级
一.Foreground Process(前台进程,优先级别最高) 1.正处于 Activity Resume() 状态 2.正处于与 bound 服务交互的状态 3.正处于服务在前台运行的状态(st ...
- Windows Phone 8.1 学习之路
前几天看一哥们写的“Android学习之路”一文很不错,遂也写一篇Windows Phone的学习之路. 开发环境 台式机 不管是台式机还是笔记本,建议配置在I5+8G以上,I3+4G的话就别考虑用模 ...
- NOIP 2018 总结
NOIP 2018 总结 提高组: 应得分 \(100 + 100 + 40 + 100 + 50 + 44 = 434\). 考后期望得分 \(100 + 100 + 20 + 100 + 50 + ...
- pytest 运行指定用例
pytest运行指定用例 随着软件功能的增加,模块越来越多,也意味用例越来越多,为了节约执行时间,快速得到测试报告与结果,在工作中可以通过运行指定用例,达到快速执行用例 例子目录 spec_sub1_ ...
- EasyUi DataGrid 请求Url两次问题
easyui datagrid 1.4 当total为0时,请求两次url问题 框架问题:需要在easyui文件后加修补补丁 /** * The Patch for jQuery EasyUI 1.4 ...