网络流--最大流--EK模板
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
const int maxn=1005;
int g[maxn][maxn];//容量
int f[maxn][maxn];//流量
bool vis[maxn];
int pre[maxn];
bool bfs(int s,int t){
memset(pre,-1,sizeof pre);
memset(vis,false,sizeof vis);
queue<int> q;
vis[s]=true;
q.push(s);
while(!q.empty()){
int now=q.front();
q.pop();
for(int i=1;i<=n;i++){
if(!vis[i]&&g[now][i]){
vis[i]=true;
pre[i]=now;
if(i==t) return true; //找到汇点,即找到增广路。
q.push(i);
}
}
}
return false;
}
int EK(int s,int t){
int maxflow=0;
int u,v;
while(bfs(s,t)){
int d=INF;
v=t;
while(v!=s){
u=pre[v];
if(d>g[u][v])
d=g[u][v];
v=u;
}
maxflow+=d;
v=t;
while(v!=s){
u=pre[v];
g[u][v]-=d;
g[v][u]+=d;
if(f[v][u]>0)
f[v][u]-=d;
else f[u][v]+=d;
v=u;
}
}
return maxflow;
}
int main()
{
//int s,t;
int u,v,c;
while(~scanf("%d%d",&m,&n)){
memset(f,0,sizeof f);
memset(g,0,sizeof g);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&u,&v,&c);
g[u][v]+=c;
}
printf("%d\n",EK(1,n));
}
return 0;
}
网络流--最大流--EK模板的更多相关文章
- 网络流--最大流ek模板
标准大白书式模板,代码简单但由于效率并不高,所以并不常用,就是这样 #include<stdio.h> #include<string.h> #include<queue ...
- (通俗易懂小白入门)网络流最大流——EK算法
网络流 网络流是模仿水流解决生活中类似问题的一种方法策略,来看这么一个问题,有一个自来水厂S,它要向目标T提供水量,从S出发有不确定数量和方向的水管,它可能直接到达T或者经过更多的节点的中转,目前确定 ...
- 网络流--最大流dinic模板
标准的大白书式模板,除了变量名并不一样……在主函数中只需要用到 init 函数.add 函数以及 mf 函数 #include<stdio.h> //差不多要加这么些头文件 #includ ...
- 网络流 最大流SAPkuangbin模板
hdu 1532 求1~n的最大流 #include<stdio.h> #include<string.h> #include<algorithm> #includ ...
- 网络流-最大流 Dinic模板
#include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB push_back #defin ...
- 网络流--最大流--Dinic模板矩阵版(当前弧优化+非当前弧优化)
//非当前弧优化版 #include <iostream> #include <cstdio> #include <math.h> #include <cst ...
- HDU1532 网络流最大流【EK算法】(模板题)
<题目链接> 题目大意: 一个农夫他家的农田每次下雨都会被淹,所以这个农夫就修建了排水系统,还聪明的给每个排水管道设置了最大流量:首先输入两个数n,m ;n为排水管道的数量,m为节点的数量 ...
- HDU1532_Drainage Ditches(网络流/EK模板/Dinic模板(邻接矩阵/前向星))
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)
题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...
随机推荐
- String 对象-->概念和创建
1.String 对象 String 对象用于处理文本(字符串). String 对象创建方法: new String(). 语法: var txt = new String("string ...
- Windows Server 2016 Storage Replication
Storage Replication是Windows Server 2016中新增的一项功能,它是利用windows server自带的块存储复制技术 首先,我们简答粗暴的交代一下部署需求: 1.该 ...
- 深入理解new运算符
在 JavaScript 中,new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例.创建一个对象很简单,为什么我们还要多此一举使用 new 运算符呢?它到底有什么样的魔力? 认 ...
- shell 脚本常用调试方法
曾经我刚开始学习 shell 脚本时,除了知道用 echo 输出一些信息外,并不知道其他方法,仅仅依赖 echo 来查找错误,比较难调试且过程繁琐.效率低下.本文介绍下我常用的一些 shell 脚本调 ...
- 浅析CAS与AtomicInteger原子类
一:CAS简介 CAS:Compare And Swap(字面意思是比较与交换),JUC包中大量使用到了CAS,比如我们的atomic包下的原子类就是基于CAS来实现.区别于悲观锁synchroniz ...
- Linux学习笔记(七)关机、重启及常用的网络命令
关机.重启命令 sync shutdown reboot init sync 英文原意:flush file system buffers 功能:刷新文件系统缓冲区,将内存中的数据保存到硬盘中 语法: ...
- Soul Android app 悬浮view以及帖子中view的联动刷新逆向分析
Soul app是我司的竞品,对它的语音音乐播放同步联动的逻辑很感兴趣,于是就开启了一波逆向分析. 下面看代码,以及技术分析,直接步入正轨,哈哈. 我们根据https://github.com/xin ...
- lua实现游戏抽奖的几种方法
^_^内容原创,禁止转载 假设配置如下: local reward_pool = { {weight = , item = {, num = }}, {weight = , item = {, nu ...
- 基于centos7搭建kvm
其他的和安装一般的系统没有差别 安装完成后. 1]使用ping www.baidu.com 2]修改静态ip,也可以不修改 3]下载brctlyum -y install bridge-utils 4 ...
- Qt 的日期 时间
QDateTime 的构造函数,有参数是QDate的.这样就可以把日期转化成 QDateTime. QDateTime.toTime_t() 可以转化成 Unix 时间.