网络流-最大流 Dinic模板
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
#define ls first
#define rs second
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e5+;
const int mod=1e9+; vector<pair<int,int>>mp[K];
int n,m,cnt,flow[K*],deep[K],cur[K]; int bfs(int s,int t)
{
queue<int>q;
memset(deep,,sizeof deep);
q.push(s),deep[s]=;
while(!q.empty())
{
int u=q.front();q.pop();
for(auto &it:mp[u])
if(!deep[it.ls]&&flow[it.rs])
{
deep[it.ls]=deep[u]+;
q.push(it.ls);
if(it.ls==t)
return ;
}
}
return ;
}
int dfs(int x,int d,int t)
{
if(x==t) return d;
for(int i=cur[x];i<mp[x].size();cur[x]=++i)
{
int u=mp[x][i].ls,v=mp[x][i].rs;
if(deep[u]==deep[x]+&&flow[v])
{
int td=min(d,dfs(u,min(d,flow[v]),t));
if(!td) continue;
flow[v]-=td;
flow[v^]+=td;
return td;
}
}
return ;
}
int dinic(int s,int t)
{
int ret=,d;
while(bfs(s,t))
{
memset(cur,,sizeof cur);
while(d=dfs(s,mod,t)) ret+=d;
}
return ret;
}
int main(void)
{
while(~scanf("%d%d",&m,&n))
{
cnt=;
memset(mp,,sizeof mp);
for(int i=,u,v,w;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
flow[cnt]=w,flow[cnt+]=;
mp[u].PB(MP(v,cnt++));
mp[v].PB(MP(u,cnt++));
}
printf("%d\n",dinic(,n));
}
return ;
}
网络流-最大流 Dinic模板的更多相关文章
- 网络流--最大流dinic模板
标准的大白书式模板,除了变量名并不一样……在主函数中只需要用到 init 函数.add 函数以及 mf 函数 #include<stdio.h> //差不多要加这么些头文件 #includ ...
- 网络流--最大流--Dinic模板矩阵版(当前弧优化+非当前弧优化)
//非当前弧优化版 #include <iostream> #include <cstdio> #include <math.h> #include <cst ...
- [讲解]网络流最大流dinic算法
网络流最大流算法dinic ps:本文章不适合萌新,我写这个主要是为了复习一些细节,概念介绍比较模糊,建议多刷题去理解 例题:codevs草地排水,方格取数 [抒情一下] 虽然老师说这个多半不考,但是 ...
- 【模板】网络流-最大流 Dinic
洛谷 3376 #include<cstdio> #include<algorithm> #include<cstring> #define N 10010 #de ...
- 网络流最大流——dinic算法
前言 网络流问题是一个很深奥的问题,对应也有许多很优秀的算法.但是本文只会讲述dinic算法 最近写了好多网络流的题目,想想看还是写一篇来总结一下网络流和dinic算法以免以后自己忘了... 网络流问 ...
- Power Network(网络流最大流 & dinic算法 + 优化)
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24019 Accepted: 12540 D ...
- POJ训练计划1459_Power Network(网络流最大流/Dinic)
解题报告 这题建模实在是好建.,,好贱.., 给前向星给跪了,纯dinic的前向星居然TLE,sad.,,回头看看优化,.. 矩阵跑过了.2A,sad,,, /******************** ...
- 最大流dinic模板
循环版,点的编号从0开始: ; ; const int INF = 0x3f3f3f3f; struct Edge { int to, next, cap, flow; }edge[MAXM]; in ...
- (网络流 最大流 Dinic || SAP)Control -- hdu --4289
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4289 http://acm.hust.edu.cn/vjudge/contest/view.action ...
随机推荐
- 本地项目通过github客户端上传到github网站上
一.github客户端上传步骤 一.github客户端上传步骤1.百度搜索关键字: GitHub 离线安装包 ==> Github_3.3.4.0版本链接:https://pan.baidu.c ...
- Mysql语句优化
总结总结自己犯过的错,网上说的与自己的Mysql语句优化的想法. 1.查询数据库的语句的字段,尽量做到用多少写多少. 2.建索引,确保查询速度. 3.orm框架自带的方法会损耗一部分性能,这个性能应该 ...
- 使用sql语句删除数据库中重复的数据
在数据库中有一批数据,但是由于数据入库之前某些不当的操作引入了一些重复的数据,需要将这些重复的数据删除.数据的结构大概是下面的样子 CREATE TABLE [dbo].[aaa]( [id] [in ...
- android 设置系统屏幕亮度
/** * 获得当前屏幕亮度的模式 * SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度 * SCREEN_BRIGHTNESS_MODE_MANUAL=0 为 ...
- Zookeeper可以干什么
在Zookeeper的官网上有这么一句话:ZooKeeper is a centralized service for maintaining configuration information, n ...
- poj3261 Milk Patterns【后缀数组】【二分】
Farmer John has noticed that the quality of milk given by his cows varies from day to day. On furthe ...
- 插入排序之python
插入排序( Insert sort) 通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入: 由于不需要全部都比较完,所以排序速度优于冒泡和选择排序. #插入排序就像是斗地 ...
- df and du
1.若有进程在占用某个文件,而其他进程把这文件删掉,只会删除其在磁盘中的标记,而不会释放其占用的磁盘空间:直到所有访问该文件的进程退出为止: 2.df 是从内核中获取磁盘占用情况数据的,而du是统计当 ...
- nginx之配置proxy_set_header
win10客户端请求web服务,win10的ip:192.168.223.1 nginx作为反向代理服务器:192.168.223.136 nginx作为后端web服务器:192.168.223.13 ...
- HIVE表保存的路径
HIVE表保存的默认路径在${HIVE_HOME}/conf/hive-site.xml配置文件的hive.metastore.warehouse.dir属性指定