网络流--最大流--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 ...
随机推荐
- 【第二章】黎姿的python学习笔记
- Python安装MySQL数据库模块
背景 折腾: [记录]使用Python操作MySQL数据库 的过程中,需要去安装MySQLdb. 下载MySQLdb 去官网: http://pypi.python.org/pypi/MySQL-py ...
- MyBatis-Plus使用小结
官网: https://mybatis.plus/ https://gitee.com/baomidou/mybatis-plus https://github.com/baomidou/mybati ...
- Win8.1/Win10在某些程序输入中文变成问号的解决方法
之前我是使用Win8.1,在某些软件上输入中文,却显示问号,换输入法也没用,当时也没用太在意,后来升级到Win10还是一样.同样的软件在其它Win8.1/Win10电脑却可以正常显示中文. 解决方法如 ...
- AJ学IOS(15)UI之曾经大热的打砖块小游戏
AJ分享,必须精品 先看效果图 代码 // ViewController.m // 打砖块 // // Created by liufan on 13-8-17. // Copyright (c) 2 ...
- AJ学IOS(07)UI之UITextField代理事件_类似QQ登陆窗口的简单实现
AJ分享,必须精品 先看效果图: 学习代码 // // NYViewController.m // 05-UITextField事件_UIKit复习 // // Created by apple on ...
- java nio消息半包、粘包解决方案
问题背景 NIO是面向缓冲区进行通信的,不是面向流的.我们都知道,既然是缓冲区,那它一定存在一个固定大小.这样一来通常会遇到两个问题: 消息粘包:当缓冲区足够大,由于网络不稳定种种原因,可能会有多条消 ...
- codeforces 122C perfect team
You may have already known that a standard ICPC team consists of exactly three members. The perfect ...
- MySQL的单表查询
单表查询 单表查询语法: select distinct 字段1,字段2... from 表名 where 条件 group by field having筛选 order by 关键字执行的优先级: ...
- [De1CTF 2019]SSRF Me
原帖地址 : https://xz.aliyun.com/t/5927 De1CTF 2019 的一个题目总结 题目描述 直接查看页面源代码可以看到正确格式的代码 #! /usr/bin/env py ...