网络流Dinic--模板
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
#include <cassert>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
//******************
int abss(int a);
int lowbit(int n);
int Del_bit_1(int n);
int maxx(int a,int b);
int minn(int a,int b);
double fabss(double a);
void swapp(int &a,int &b);
clock_t __STRAT,__END;
double __TOTALTIME;
void _MS(){__STRAT=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef vector<int> VI;
typedef long long ll;
const double E=2.718281828;
const double PI=acos(-1.0);
//const ll INF=(1LL<<60);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)2e5+; class DINIC
{
public:
// const int MAXN=10004,MAXWAY=100005;
int n,way,max_flow,deep[N];
int tot,head[N],cur[N];
struct EDGE{
int to,next;
int dis;
}edge[N];
void Init(int n)
{
tot=-;//因为加反向边要^1,所以要从0开始;
for(int i=;i<=n;++i)
head[i]=-;
}
void add(int from,int to,int cost,bool flag)
{
++tot;
edge[tot].to=to;
edge[tot].dis=;
if(flag)edge[tot].dis=cost;
edge[tot].next=head[from];
head[from]=tot;
}
queue<int>q;
bool bfs(int s,int t)
{
for(int i=;i<=n;++i)
deep[i]=inf;
while(!q.empty())q.pop();
for(int i=;i<=n;++i)cur[i]=head[i];
deep[s]=;
q.push(s); while(!q.empty())
{
int now=q.front();q.pop();
for(int i=head[now];i!=-;i=edge[i].next)
{
if(deep[edge[i].to]==inf&&edge[i].dis)
{
deep[edge[i].to]=deep[now]+;
q.push(edge[i].to);
}
}
}
return deep[t]<inf;
}
int dfs(int now,int t,int limit)
{
if(!limit||now==t)return limit;
int flow=,f;
for(int i=cur[now];i!=-;i=edge[i].next)
{
cur[now]=i;
if(deep[edge[i].to]==deep[now]+&&(f=dfs(edge[i].to,t,min(limit,edge[i].dis))))
{
flow+=f;
limit-=f;
edge[i].dis-=f;
edge[i^].dis+=f;
if(!limit)break;
}
}
return flow;
}
void Dinic(int s,int t)
{
while(bfs(s,t))
max_flow+=dfs(s,t,inf);
}
}G; int main()
{
int S,T;
sc("%d%d%d%d",&G.n,&G.way,&S,&T);
G.Init(G.n);
for(int i=;i<=G.way;++i)
{
int u,v,cost;
sc("%d%d%d",&u,&v,&cost);
G.add(u,v,cost,);
G.add(v,u,cost,);
}
G.Dinic(S,T);
pr("%d\n",G.max_flow);
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}
网络流Dinic--模板的更多相关文章
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- Power Network POJ - 1459 网络流 DInic 模板
#include<cstring> #include<cstdio> #define FOR(i,f_start,f_end) for(int i=f_startl;i< ...
- 网络流Dinic模板
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #d ...
- 网络流dinic模板,邻接矩阵+链式前向星
//这个是邻接矩阵的#include<iostream> #include<queue> #include<string.h> #include<stdio. ...
- POJ 2987 Firing 最大流 网络流 dinic 模板
https://www.cnblogs.com/137shoebills/p/9100790.html http://poj.org/problem?id=2987 之前写过这道题,码一个dinic的 ...
- hdu 1532 Dinic模板(小白书)
hdu1532 输入n,m. n条边,m个点,之后给出a到b的容量,求1到m的最大流. 注意:Dinic只能调用一次,因为原理是改变cap的值,如果调用多次一样的,那么第一次会对,其余的都会是0,因为 ...
- 最大流算法 ISAP 模板 和 Dinic模板
ISAP // UVa11248 Frequency Hopping:使用ISAP算法,加优化 // Rujia Liu struct Edge { int from, to, cap, flow; ...
- 网络流——SAP模板
//网络流SAP模板,复杂度O(N^2*M) //使用前调用init(源点,汇点,图中点的个数),然后调用add_edge()加边 //调用getflow得出最大流 #define N 55 #def ...
- 洛谷P3376【模板】网络最大流 Dinic模板
之前的Dinic模板照着刘汝佳写的vector然后十分鬼畜跑得奇慢无比,虽然别人这样写也没慢多少但是自己的就是令人捉急. 改成邻接表之后快了三倍,虽然还是比较慢但是自己比较满意了.虽然一开始ecnt从 ...
- ACM/ICPC 之 有流量上下界的网络流-Dinic(可做模板)(POJ2396)
//有流量上下界的网络流 //Time:47Ms Memory:1788K #include<iostream> #include<cstring> #include<c ...
随机推荐
- scrapy框架之spider
爬取流程 Spider类定义如何爬取指定的一个或多个网站,包括是否要跟进网页里的链接和如何提取网页内容中的数据. 爬取的过程是类似以下步骤的循环: 1.通过指定的初始URL初始化Request,并指定 ...
- electron之环境安装、启动程序
1.安装node.js 2.安装淘宝镜像 npm install -g cnpm --registry=https://registry.npm.taobao.org 3.安装全局electron n ...
- ArcGIS超级工具SPTOOLS-数据处理篇
1. 数据处理 1.1 两个图层按重叠度赋属性 两个面层按重合度赋属性,下图把依据赋数据属性图层,按重合度,赋值给目标.,重合度设置为负值,取面积最大的. 1.2 分区域消除 按区域字段值相同的, ...
- Flutter移动电商实战 --(30)列表页_商品列表UI界面布局
小程序里面的布局方式 小程序的图片上这里使用的是warp布局,因为首页里面火爆专区,已经用过了warp来布局了. 所以这里我们没有必要再讲一遍,这里我们使用ListView,我们把它布局成下图这种形式 ...
- 怎么理解linux的平均负载及平均负载高后的排查工具
什么是平均负载 平均负载可以对于我们来说及熟悉又陌生,但我们问平均负载是什么,但大部分人都回答说平均负载不就是单位时间内CPU使用率吗?其实并不是这样的,如果可以的话,可以 man uptime 来了 ...
- BSD process name correspondlng to current thread: knernel_task Mac OS version Not yet set
网上查了一大堆,没有一个靠谱的, 百度,以说黑苹果装系统最容易出现这个,这个让我开始怀疑公司给我们的所谓外观的iMac是黑苹果了,因为一直很卡,比上家公司的真黑苹果还卡. 谷歌,有说重置BIOS电池的 ...
- Linux下如何启用MySQL数据库远程访问
远程连接MySQL出于安全考虑,一般都关闭了远程访问,但有时候需要提供远程访问数据库的服务,下面我们快速学习下: 第一步:修改my.cnf文件使用文本编辑器去编辑MySQL服务器的配置文件my.cnf ...
- 【leetcode】506. Relative Ranks
problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...
- 中国行政区划表,包括34个省、直辖市的所有数据 mysql数据
中国行政区划表,包括34个省.直辖市的所有数据 sql文件地址 https://gitee.com/zwh_9527/ChinaProvince
- 【ABAP系列】SAP ABAP 字符编码与解码、Unicode
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 字符编码与解码 ...