LibreOJ #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 <cstring>
#include <vector>
#include <cstdio>
#include <queue>
#define N 4000005
using namespace std;
struct node
{
int to,next,dis;
}edge[N*];
int tot=,Answer,dis[],head[],n,m,s,t,i,j;
void add(int from,int to,int w)
{
tot++;
edge[tot].next=head[from];
edge[tot].to=to;
edge[tot].dis=w;
head[from]=tot;
}
bool bfs()
{
queue<int>q;
memset(dis,-,sizeof(dis));
dis[s]=;
q.push(s);
while(!q.empty() )
{
int Top=q.front() ;
q.pop() ;
for(i=head[Top];i;i=edge[i].next)
{
if(dis[edge[i].to]==-&&edge[i].dis>)
{
dis[edge[i].to]=dis[Top]+;
if(edge[i].to==t) return ;
else q.push(edge[i].to);
}
}
}
return ;
}
int work(int now,int f)
{
if(now==t||f==) return f;
int rest=;
for(int i=head[now];i;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].dis>&&dis[v]==dis[now]+)
{
int t=work(v,min(f,edge[i].dis));
rest+=t;
f-=t;
edge[i].dis-=t;
edge[i^].dis+=t;
if(f==) return rest;
}
}
if(rest!=f) dis[now]=-;
return rest;
}
int main()
{
scanf("%d%d%d%d",&n,&m,&s,&t);
int u,v,l;
for(i=;i<m;++i)
{
scanf("%d%d%d",&u,&v,&l);
add(u,v,l);
add(v,u,);
}
while(bfs()) Answer+=work(s,1e8);
printf("%d",Answer);
return ;
}
LibreOJ #101. 最大流的更多相关文章
- [loj#101] 最大流 网络流模板
#101. 最大流 内存限制:512 MiB时间限制:5000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 这是一道模板题. 给定 ...
- 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 ...
- LibreOJ 题解汇总
目录 #1. A + B Problem #2. Hello, World! #3. Copycat #4. Quine #7. Input Test #100. 矩阵乘法 #101. 最大流 #10 ...
- CTF流量分析题大全(掘安攻防平台)
突然想做一下流量分析题,记得掘安攻防实验室上面有很多的流量分析题目,故做之 流量分析题一般使用的都是wireshark,(流量分析工具中的王牌 夺取阿富汗 说了分析http头,所以直接过滤http协议 ...
- LibreOJ #116. 有源汇有上下界最大流
二次联通门 : LibreOJ #116. 有源汇有上下界最大流 /* LibreOJ #116. 有源汇有上下界最大流 板子题 我也就会写写板子题了.. 写个板子第一个点还死活过不去... 只能打个 ...
- LibreOJ #115. 无源汇有上下界可行流
二次联通门 : LibreOJ #115. 无源汇有上下界可行流 /* LibreOJ #115. 无源汇有上下界可行流 板子题 我也就会写写板子题了.. */ #include <cstdio ...
- LibreOJ #6013. 「网络流 24 题」负载平衡 最小费用最大流 供应平衡问题
#6013. 「网络流 24 题」负载平衡 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
随机推荐
- 理解 Android MVP 开发模式
/***************************************************************************************** * 理解 Andr ...
- 「LuoguT36048」 Storm in Lover
Description 平成二十四年(2012年),5月11日,东京,某弓道场. "呐,呐,海未酱,你听说了吗?几天后的那场弓道大会?啊-!"橙发少女兴奋地拿着一张传单一样的纸跑向 ...
- JAVA 布局控制
在Java里该方法是安一个组件到一个窗体中去,它不同我们使用过的其它GUI系统.首先,它是全代码的:没有控制安放组件的“资源”.其次,该方法的组件被安放到一个被“布局管理器”控制的窗体中,由“布局管理 ...
- 安装PostgreSQL数据库(Linux篇)
0.编译环境 Linux: CentOS 5.5 gcc: 4.1.2 1. 安装PostgreSQL 1) 解压postgresql-9.1.7.tar.bz2 #tar jxvf postgres ...
- bzoj4547
矩阵乘法 看成了合并果子... 就是斐波那契数列,只是有负数的时候,先把负数变成正的,然后矩乘 矩乘还是用单位举矩阵记录快速幂的矩阵比较保险 #include<cstdio> #inclu ...
- Codeforces - 1114B - Yet Another Array Partitioning Task - 构造 - 排序
https://codeforces.com/contest/1114/problem/B 一开始叫我做,我是不会做的,我没发现这个性质. 其实应该很好想才对,至少要选m个元素,其中m个作为最大值,从 ...
- hdoj3711【水】
题意: 给你两个集合,对于每个B集合的元素,从A集合找一个数使得a^b的二进制的1个数最少. 思路: 直接搞= = #include <bits/stdc++.h> using names ...
- hdoj1698【线段树Lazy操作】
区间更新lazy操作一发. #include<cstdio> #include<iostream> #include<string.h> #include<a ...
- lightoj1231【背包】
题意: 有n种货币,已知每种货币的价值和数量. 给出一个k,问最多能组成k的方法.这个数量很大,需要mod 1e8+7. 思路: 额....这一题和上一篇的差不多,直接传送门: (此题很棒) 这里的话 ...
- Linux下备份MySQL数据库的Shell脚本
数据库每天都想备份,手动备份太麻烦而又容易忘记,所以写了一个自动备份MySQL数据库的脚本,加入定时计划中,每天自运运行. 创建Shell脚本代码如下,命名为mysql_dump.sh #!/bin/ ...