网络流

【Luogu P3376】网络最大流

▼魔方 西西 提交于 2019-12-06 04:27:14
Luogu P3376 最大流是网络流模型的一个基础问题。 网络流模型就是一种特殊的有向图。 概念: 源点:提供流的节点(入度为0),类比成为一个无限放水的水厂 汇点:接受流的节点(出度为0),类比成为一个无限收水的小区 弧:类比为水管 弧的容量:类比为水管的容量;用函数 \(c(x,y)\) 表示弧 \((x,y)\) 的容量 弧的流量:类比为当前在水管中水的量;用函数 \(f(x,y)\) 表示弧 \((x,y)\) 的流量 弧的残量:即容量-流量 容量网络:对于一个网络流模型,每一条弧都给出了容量,则构成一个容量网络。 流量网络:对于一个网络流模型,每一条弧都给出了流量,则构成一个流量网络。 残量网络:对于一个网络流模型,每一条弧都给出了残量,则构成一个残量网络。最初的残量网络就是容量网络。 对于网络流模型 \(G=(V,E)\) ( \(V\) 为点集, \(E\) 为边集)有如下性质: 流量守恒:除了源点与汇点之外,流入任何节点的流一定等于流出该节点的流 容量限制: \(\forall (x,y) \in E,有0<=f(x,y)<=c(x,y)\) 斜对称性: \(\forall (x,y) \in E,有f(x,y)=-f(y,x).\) 类似于函数奇偶性中的奇函数,或者是矢量的方向。 最大流问题,用通俗的方式解释就是从源点S到汇点T输送流量

网络流初步(1)

爷,独闯天下 提交于 2019-12-06 04:09:36
总算A串。来屯思路的。 蜥蜴 没有比这个更板子的了。对于每个石柱拆点成两个,连边限制流量。 1 #include<bits/stdc++.h> 2 using namespace std; 3 int cnt=2,in[22][22],out[22][22],n,m,d,tms[22][22],ecnt=1,dep[1005]; 4 int fir[1005],l[50005],to[50005],v[50005],x,maxflow,q[1005],t,cntt; 5 int fab(int p){return p*p;} 6 void connect(int a,int b,int vv){ 7 l[++ecnt]=fir[a];fir[a]=ecnt;to[ecnt]=b;v[ecnt]=vv; 8 l[++ecnt]=fir[b];fir[b]=ecnt;to[ecnt]=a; 9 } 10 int read1(){ 11 register int ch=getchar(); 12 while(ch<'0'||ch>'3')ch=getchar(); 13 return ch-48; 14 } 15 int read2(){ 16 register int ch=getchar(); 17 while(ch!='L'&&ch!='.')ch=getchar(); 18

网络流扩展知识

时间秒杀一切 提交于 2019-12-06 03:34:58
网络流扩展知识 最小费用最大流 luogu P3381 【模板】最小费用最大流 解析: 先用spfa求出最短路径(单位流量费用最少) 多路增广流掉这些流量(spfa没有记录dep信息,但凭借dis[]信息的关系不能保证不会访问之前访问过的节点,所以需要vis[]标记) code #include<bits/stdc++.h> #include<iostream> using namespace std; #define CL(a,b) memset(a,b,sizeof(a)) #define db(x) cout<<"["<<#x<<"]="<<x<<endl #define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) const int inf = 0x3f3f3f3f; const int maxn = 5e3+100; const int maxm = 5e4+100; struct edge{ int u,v,cap,cost,nxt; }es[maxm*100]; int cnt, head[maxn],dis[maxn],vis[maxn]; void addEdge(int u,int v,int cap,int cost){ es[cnt].u = u, es[cnt].v = v,es

网络流学习笔记

落花浮王杯 提交于 2019-12-06 01:55:10
目录 网络流学习笔记 网络流资料 最大流dinic 费用流dinic 无源汇有上下界可行流 有源汇有上下界可行流 (最大流/最小流) 最大流 最小流 最大权闭合子图 证明(proof) 例题 最大密度子图 二分图的最小点权覆盖集和最大点权独立集 网络流24题中的思想与解题方案 网络流学习笔记 网络流资料 最大流dinic #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int INF = 1e9; const int N = 10005; const int M = 200005; int to[M], ne[M]; int w[M], h[N], tot = -1; inline void add(int x,int y,int z) { ne[++tot] = h[x], h[x] = tot; to[tot] = y, w[tot] = z; } template <typename T> void read(T &x) { x = 0; int f = 0; char c = getchar(); for (;!isdigit(c);c=getchar()) if (c=='-') f=1; for (;isdigit(c);c

BZOJ 1001 狼抓兔子

淺唱寂寞╮ 提交于 2019-12-06 01:14:44
题面 题解   平面图上网络流转最短路。   题意可转化为求左上角到右下角的最大流。根据最大流最小割定理,网络流中最大流的值等于最小割的容量。由于本题中给出的网络流是平面图,可以用最短路在几何意义上算出最小割:   对每一个封闭区域 \(u\) 建点 \(u'\) ,对每两个相接的封闭区域 \(u,v\) 的公共边 \(e\) 建边 \(e'\) 连接 \(u',v'\) , \(e'\) 的权值等于 \(e\) 的流量,如果把选取 \(e'\) 看作割断 \(e\) ,那么求网络流的最小割就可以等效为选取权值和尽可能小的 \(e'\) ,使得图的左下方和右上方联通,而这个问题可以用最短路来解决。复杂度 \(O(nm\log(nm))\) 。 代码 来源: https://www.cnblogs.com/Kilo-5723/p/11955410.html

网络流:最大流之SAP算法

a 夏天 提交于 2019-12-05 09:32:27
网络流主要解决三种问题:最大流、最小流和费用流。 最大流算法主要有三种:EK算法、Dinic算法、SAP算法。 本篇博客是关于SAP算法的。最坏的情况下,SAP算法将达到复杂度 O ( V E 2 )。 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <queue> 5 #include <cstring> 6 7 using namespace std; 8 const int INF = 0x3f3f3f3f; 9 const int maxn = 200 + 10; 10 const int maxm = 200 + 10; 11 12 int n,m; 13 int head[maxn];//链式前向星 14 int tot = 0; 15 16 struct edge 17 { 18 int to; 19 int c; 20 int next; 21 edge(int x = 0, int y = 0, int z = 0) : to(x), c(y), next(z) {} 22 }es[maxm*2];//记录边 注意是2倍 23 24 void add_edge(int u, int v, int c) 25 { 26 es[tot] = edge(v,c

POJ1273【网络流】

人走茶凉 提交于 2019-12-05 09:24:43
                                                      Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 91824 Accepted: 35588 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning

网络流:最大流之Dinic算法

≯℡__Kan透↙ 提交于 2019-12-05 05:23:05
网络流主要解决三种问题:最大流、最小流和费用流。 最大流算法主要有三种:EK算法 、Dinic算法、S AP算法 本篇博客是关于Dinic算法的。最坏的情况下,Dinic算法将达到复杂度 O (V 2 E )。 1 #include<string.h> 2 #include<cstdio> 3 #include<algorithm> 4 #include<limits.h> 5 #include<queue> 6 using namespace std; 7 const int M=420; 8 const int inf=0x3f3f3f3f; 9 struct aa 10 { 11 int u,v,r,next; 12 } edge[888888]; 13 int cnt,dis[M],s,f,n,m,cost[M],pre[M],head[M]; 14 void add(int u,int v,int r) 15 { 16 edge[cnt].v=v; 17 edge[cnt].u=u; 18 edge[cnt].r=r; 19 edge[cnt].next=pre[u]; 20 pre[u]=cnt++; 21 edge[cnt].v=u; 22 edge[cnt].u=v; 23 edge[cnt].r=0; 24 edge[cnt].next=pre[v]; 25

网络流

瘦欲@ 提交于 2019-12-05 02:58:32
网络流未处理文件的代码: /*656.网络流-矩阵计算 (10分) C时间限制:3000?毫秒?|? C内存限制:3000?Kb 题目内容: 有一个n行m列的整数矩阵A, 知道每行的和以及每列的和,还知道一些矩阵元素的约束如A[i][j]<x, 或者A[i][j]>y等, 判断该是否存在满足上述条件的可行矩阵。 输入描述 第一行是测试用例的数目c 每个测试用例的第一行是n,m 表示行和列 接下来一行是n个行和 接下来一行是m个列和 然后一行是约束个数k 接下来k行是约束,每个约束如 a b c d, 其中a,b是某个元素的行列坐标,c是一个字符(>,=,<), d是一个整数, 2 3 > 4 表示的意思是A[2][3]>4。矩阵左上角坐标规定为(1,1),所以一个约束的a为0,则表示b列所有的元素, 而如果b为0,则表示a行所有的元素。 输出描述 如果存在,则输出这个矩阵;否则输出“不存在” */ //#include<fstream> // 文件读写头文件 #include<iostream> #include<stdio.h> #include<string.h> #include <time.h> #include <stdlib.h> using namespace std; #define maxM 50000 #define maxN 500 #define inf 1

终极模板“水题”——网络流

怎甘沉沦 提交于 2019-12-04 16:16:52
其实这篇https://www.cnblogs.com/victorique/p/8560656.html写得很完整了,但是为了加深自己的印象,就自己写一篇吧。 网络流的练习我用了传说中的网络流24题,我把题目都看了一遍,发现一个问题,有些题好像真的是为了网络流而搞网络流的做法出来,导致题目的可做性不强,虽然如此,但是除了这些题目之外,其他的题目,也挺让我耳目一新的。 其实,网络流把两套模板给记住,基本上就算是完成了一道题的一大半了。第一套模板是网络最大流(dinic+当前弧优化);第二套模板是最小费用最大流(利用spfa找出最短路的同时保证网络最大流)但是如果像我这么说的话,把网络流单纯看成模板题,是肯定是做不出来题目的,那么网络流关键的点在哪?网络流通常的套路又是什么? 1.建图 基本上网络流的题都难在这里了。如何建图,是一个难点。 (1)二分图 一类建图即是以二分图的形式来建,其包括操作拆点,建立超级源点、汇点,等等。那么在二分图上,能进行解决什么问题呢?最普遍的莫过于匹配问题,通过二分图,求出点与点之间的最大匹配值,然后再利用二分图上的性质,来求出问题的解,而二分图上的性质有下面几种常用的:最大匹配=最小点覆盖;最小路径覆盖=|G|-最大匹配数(G为点数);最大独立集=点数-最大匹配=最小边覆盖。我们可以看到求出一个最大匹配,基本上就能算出其他的问题出来