网络流 最大流SAPkuangbin模板】的更多相关文章

hdu 1532 求1~n的最大流 #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; ; //点数的最大值 ; //边数的最大值 const int INF = 0x3f3f3f3f; struct Node { int from, to, next; int cap; } edge[MAXM]; int tol;…
标准的大白书式模板,除了变量名并不一样……在主函数中只需要用到 init 函数.add 函数以及 mf 函数 #include<stdio.h> //差不多要加这么些头文件 #include<string.h> #include<queue> #include<vector> #include<algorithm> using namespace std; +; //点的总数 const int INF=0x3f3f3f3f; struct ed…
标准大白书式模板,代码简单但由于效率并不高,所以并不常用,就是这样 #include<stdio.h> #include<string.h> #include<queue> #include<vector> #include<algorithm> using namespace std; +; const int INF=0x3f3f3f3f; struct edge{ int from,to,c,f; edge(int a,int b,int…
#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 pi=acos(-1.0); ; ; vector<pair<int,int>>mp[K…
//非当前弧优化版 #include <iostream> #include <cstdio> #include <math.h> #include <cstring> #include <queue> #define INF 0x3f3f3f3f using namespace std; int tab[250][250];//邻接矩阵 int dis[250];//距源点距离,分层图 int N,M;//N:点数;M,边数 queue<…
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #include <queue> #define INF 0x3f3f3f3f using namespace st…
网络流 网络流是模仿水流解决生活中类似问题的一种方法策略,来看这么一个问题,有一个自来水厂S,它要向目标T提供水量,从S出发有不确定数量和方向的水管,它可能直接到达T或者经过更多的节点的中转,目前确定的是每条水管中水流的流向是确定的(单向),且每个水管单位时间内都有属于自己的水流量的上限(超过会爆水管),问题是求终点T单位时间内获得的最大水流量是多少?如下图: 1. 首先,我们用正常的思路去解决这个问题,对于上图的情况而言,我们可以先选择一条水流的路线1->2->4,而且我们得知1->2…
<题目链接> 题目大意: 一个农夫他家的农田每次下雨都会被淹,所以这个农夫就修建了排水系统,还聪明的给每个排水管道设置了最大流量:首先输入两个数n,m ;n为排水管道的数量,m为节点的数量,接下来就是n行数,每一行分为x1,x2,x3:x1,x2为节点的序号,x3为流量:然后问从1号节点到m号节点的最大流是多少? 解题分析: 网络流最大流裸题,下面用的是EK算法,bfs起搜索增广路径的作用,EK算法比较难理解的地方就是反向边的构造. #include <cstdio> #inclu…
题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio> #include<algorithm> #include<cstring> #include<queue> using namespace std; #define N 200010 #define INF 0x3fffffff inline int read()…
网络流最大流算法dinic ps:本文章不适合萌新,我写这个主要是为了复习一些细节,概念介绍比较模糊,建议多刷题去理解 例题:codevs草地排水,方格取数 [抒情一下] 虽然老师说这个多半不考,但是学了没坏处,所以我就把这算法学了(准确说是补起了QAQ) 以前一直觉得dinic的代码好长好难啊,然后就知难而退 最近学了很多和看了很多以后,咱们就学会了知男♂而上了,所以我果断的回来上dinic了 [dinic] Dinic算法是用来做最大流一类题的,代码有一丢丢的长,但其实说白了就是一个bfs+…
Power Network 时间限制: 1 Sec  内存限制: 128 MB 题目描述 A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0 <= p(u…
HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the game of playing house we used to play when we are kids. What a happy time as…
Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2909 Accepted Submission(s): 942 Problem Description Alice and Bob are playing together. Alice is crazy about art and she h…
A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 728 Accepted Submission(s): 241 Problem Description Dragon is studying math. One day, he drew a table with sev…
传送门 又get到一个新技能,好兴奋的说啊. 一道无源汇有上下界可行流的模板题. 其实这东西也不难,就是将下界变形而已. 准确来说,就是对于每个点,我们算出会从它那里强制流入与流出的流量,然后与超级源点,汇点连边,这样我们就成功去掉了下界的限制,上界从r变成了r-l,这样子跑一边最大流,看一下是不是每一条强制流完的边都流完了,如果有边没有流完,说明无法保证流出全部下界,否则的话就可以流完所有下界,又因为是要求可行流,所以只要下界留完了随便输出就行了. 代码: #include<bits/stdc…
题目描述 Alice和Bob做游戏,给出一张有向图表示运输网络,Alice先给Bob一种最大流方案,然后Bob在所有边上分配总和等于P的非负费用.Alice希望总费用尽量小,而Bob希望总费用尽量大.求两人都采取最优策略的情况下最大流及总费用. 输入 第一行三个整数N,M,P.N表示给定运输网络中节点的数量,M表示有向边的数量,P的含义见问题描述部分.为了简化问题,我们假设源点S是点1,汇点T是点N.接下来M行,每行三个整数A,B,C,表示有一条从点A到点B的有向边,其最大流量是C. 输出 第一…
题目描述 WJJ喜欢“魔兽争霸”这个游戏.在游戏中,巫妖是一种强大的英雄,它的技能Frozen Nova每次可以杀死一个小精灵.我们认为,巫妖和小精灵都可以看成是平面上的点. 当巫妖和小精灵之间的直线距离不超过R,且巫妖看到小精灵的视线没有被树木阻挡(也就是说,巫妖和小精灵的连线与任何树木都没有公共点)的话,巫妖就可以瞬间杀灭一个小精灵. 在森林里有N个巫妖,每个巫妖释放Frozen Nova之后,都需要等待一段时间,才能再次施放.不同的巫妖有不同的等待时间和施法范围,但相同的是,每次施放都可以…
题目描述 Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within his farm and needs to be able to get to the machine without being detected. He must make a total of T (1 <= T <=…
题目描述 Dicing 是一个两人玩的游戏,这个游戏在Byteotia非常流行. 甚至人们专门成立了这个游戏的一个俱乐部. 俱乐部的人时常在一起玩这个游戏然后评选出玩得最好的人.现在有一个非常不走运的家伙,他想成为那个玩的最好的人,他现在知道了所有比赛的安排,他想知道,在最好的情况下,他最少只需要赢几场就可以赢得冠军,即他想知道比赛以后赢的最多的那个家伙最少会赢多少场. 输入 第一行两个整数n 和 m, 1 <= n <= 10 000, 0 <= m <= 10 000; n 表…
Farm Tour POJ - 2135 When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 &l…
一般预流推进算法: 算法思想: 对容量网络G 的一个预流f,如果存在活跃顶点,则说明该预流不是可行流. 预流推进算法就是要选择活跃顶点,并通过它把一定的流量推进到它的邻接顶点,尽可能将正的赢余减少为0. 由于算法最终目的是尽可能将流量推进到汇点Vt,因此算法总是首先寻求将流量推进到距离汇点Vt 最近的邻接顶点中. 由于每个顶点的距离标号可以表示顶点到汇点Vt 的距离,因此算法总是将流量沿着允许弧推进. 如果从当前活跃顶点出发没有允许弧,则增加该顶点的距离标号,使得从当前活跃顶点出发至少有一条允许…
题目链接:https://vjudge.net/contest/68128#problem/H 我觉得这个题有bug,如果饮料和食物都为0,但是同时有五个人什么也不需要,按道理来讲,最多受益的人数为5,但是如果按照网络流建图的方式来说,最大流量却为0.(可能是我理解错了,希望有大佬解释一下). 还有一点不理解的地方,为什么当前这个人不需要这种食物的时候,流量设置为0.??? 具体思路:按照网络流的方式建图,因为一个人最大的流量为1,所以注意拆点.拆点是为了防止出现以下这种情况.饮料到人的流量为5…
http://poj.org/problem?id=1273 这道题很值得反思,弄了一下午,交上去先是一直编译错误,而在本地运行没有问题, 原因可能是oj的编译器版本老旧不支持这样的写法 G[from].push_back((edge){to,cap,G[to].size()}); G[to].push_back((edge){from,0,G[from].size() - 1}); 这两句交上去是不能通过的,不知道网上很多人这样子贴出代码是怎样通过的(‘白书’的模板有问题的) 如果是照着‘白书…
标准大白书式模板 #include<stdio.h> //大概这么多头文件昂 #include<string.h> #include<vector> #include<queue> #include<algorithm> using namespace std; +; //最大点数 const int INF=0x3f3f3f3f; struct edge{ //边:起点.终点.容量.流量.单位费用 int from,to,c,f,cost; e…
(点击此处查看原题) Dinic算法 Dinic算法相对于EK算法,主要区别在于Dinic算法对图实现了分层,使得我们可以用一次bfs,一次dfs使得多条增广路得到增广 普通的Dinic算法已经可以处理绝大多数最大流(最小割)的题目了,但是总是有些题目会卡住普通的Dinic算法,此时我们就需要用到当前弧优化了 当前弧优化简述 不要小看当前弧优化,这个优化效果可是很明显的,就这个例题来说,我用普通的Dinic算法用时约1.7s,而使用了当前弧优化的Dinic算法后,只用了176ms,由此可以看出这…
#include<cstdio> #include<iostream> #include<cstring> #include<queue> #define INF 9999999 #define M 330 using namespace std; int maxflow,pre[M],map[M][M],n,m; void Flow(int start,int end) { ) { queue<int> p; int minflow=INF;…
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; typedef long long ll; #define space putchar(' ') #define enter putchar('\n') #define INF 0x7fffffff template <class T> void read(…
洛谷 3376 #include<cstdio> #include<algorithm> #include<cstring> #define N 10010 #define rg register using namespace std; int n,m,S,T,tot,ans,last[N],d[N],q[N],cur[N]; struct edge{ int to,pre,f; }e[]; inline int read(){ ,f=; char c=getchar…
//500ms 秒掉洛谷推流问题 #include <algorithm> #include <iostream> #include <cstring> #include <vector> #include <queue> using namespace std; typedef long long LL; typedef long long F_type; const int MAXN = 1.2e3 + 10, INF = 0x3f3f3f3…
题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) 输入数据有N,F,D,表示牛的个数,食物的数量,饮料的数量 接着输出N行表示N个牛的数据 每个牛的数据前2个是Fi和Di表示第i个牛喜欢吃的食物种数和饮料种数,接着输出Fi个食物的编号和Di个食物的编号 ok题意就是这样,这题主要考的是建图 需要把牛拆点,一分为二 图应该是 这种形式      源…