网络流

洛谷P4015 运输问题 网络流24题

匿名 (未验证) 提交于 2019-12-02 23:47:01
看了下SPFA题解,一个一个太麻烦了,另一个写的很不清楚,而且注释都变成了"????"不知道怎么过的,于是自己来一发SPFA算法。 Part 1.题意 M 个仓库,卖给 N 显然是一个最小费用最大流(MCMF)。 Part 2.˼· S c o s t [ i ] [ j n e e d [ j [ n e e d [ j ] . . . I N F n e e d [ j m i n ( h w [ i ] , n e e d [ j ] 0 1……n n+1……n+m 10000 ) T T Part 3.代码 现在代码就好办了 注释给的很清楚 1 #include<iostream> 2 #include<cmath> 3 #include<cstdio> 4 #include<cstring> 5 #include<queue> 6 #include<stack> 7 #include<vector> 8 #include<map> 9 #include<set> 10 #include<algorithm> 11 12 #define I_copy_this_answer return 0; 13 14 using namespace std; 15 16 int n,m,head[1100],size=1; 17 int mmx=1000,mincost

网络流初步详解2

匿名 (未验证) 提交于 2019-12-02 23:39:01
网络流初步详解 中大致谈了一下最大流的一些算法,其中Dinic是非常重要的,补一句:最大流 == 最小割。 //假设您已经理解了 Dinic算法和EK算法 二者之一。 对于我们原来的网络流,现在给你一个网络,每条边除了有流量(容量)的限制外,还有一个 单位费用 ,经过这条边,我们不仅要符合 可行流 的条件,还要付出一个 费用 = 单位费用和该边流量之积 。 而对于费用流,现在主要可分为 “最小费用最大流” 和 “最大费用最大流” 两种基本题型。(至于最小流一般会出现在有上下界的网络流中,这个会在网络流初步详解3中讲解) 最重要的,上述两种基本题型模板基本相同,并且是以 最大流为前提 。 再次重复:您需要理解了 Dinic算法和EK算法 二者之一,这样你可以在5分钟内学会费用流模板。 我们在这里考虑,为什么不能在Dinic算法的基础上求出费用流。 先说一下,为了满足我们最大流的 “斜对称” 原则,及我们可以反悔,以求出最大流,反边的单位费用稍作处理, 赋值为 -w void add_edge(int x, int y, int z, int c) {// 流量限制z,单位费用c to[++tot] = y, w[tot] = z, cost[tot] = c; nex[tot] = head[x], head[x] = tot; to[++tot] = x, w[tot] = 0,

P4016 负载平衡问题 网络流

匿名 (未验证) 提交于 2019-12-02 23:26:52
P4016 负载平衡问题 题目描述 G n n n个仓库的库存数量相同。搬运货物时,只能在相邻的仓库之间搬运。 输入输出格式 输入格式: 1 1 n n 2 n n 输出格式: 输出最少搬运量。 输入输出样例 复制 5 17 9 14 16 4 复制 11 说明 1 \leq n \leq 100 1 ≤ n ≤ 1 0 0 这个题目比较简单 #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <vector> #include <cstring> #include <map> #include <iostream> #define inf 0x3f3f3f3f using namespace std; typedef long long ll; const int INF = 0x3f3f3f3f; const int maxn = 1000 + 10; struct edge { int u, v, c, f, cost; edge(int u, int v, int c, int f, int cost) :u(u), v(v), c(c), f(f), cost(cost) {} }; vector<edge>e; vector<int>G[maxn];

【PowerOJ1756&网络流24题】最长k可重区间集问题(费用流)

时间秒杀一切 提交于 2019-12-02 23:19:17
题意: 思路: 【问题分析】 最大权不相交路径问题,可以用最大费用最大流解决。 【建模方法】 方法1 按左端点排序所有区间,把每个区间拆分看做两个顶点<i.a><i.b>,建立附加源S汇T,以及附加顶点S'。 1、连接S到S'一条容量为K,费用为0的有向边。 2、从S'到每个<i.a>连接一条容量为1,费用为0的有向边。 3、从每个<i.b>到T连接一条容量为1,费用为0的有向边。 4、从每个顶点<i.a>到<i.b>连接一条容量为1,费用为区间长度的有向边。 5、对于每个区间i,与它右边的不相交的所有区间j各连一条容量为1,费用为0的有向边。 求最大费用最大流,最大费用流值就是最长k可重区间集的长度。 方法2 离散化所有区间的端点,把每个端点看做一个顶点,建立附加源S汇T。 1、从S到顶点1(最左边顶点)连接一条容量为K,费用为0的有向边。 2、从顶点2N(最右边顶点)到T连接一条容量为K,费用为0的有向边。 3、从顶点i到顶点i+1(i+1<=2N),连接一条容量为无穷大,费用为0的有向边。 4、对于每个区间[a,b],从a对应的顶点i到b对应的顶点j连接一条容量为1,费用为区间长度的有向边。 求最大费用最大流,最大费用流值就是最长k可重区间集的长度。 【建模分析】 这个问题可以看做是求K条权之和最大的不想交路径,每条路径为一些不相交的区间序列。由于是最大费用流

网络流输出二分匹配方案

ε祈祈猫儿з 提交于 2019-12-02 16:38:15
题意: https://www.luogu.org/problem/P2756 1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin); 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr 13 #include <string> 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include

洛谷 P1402 酒店之王

不打扰是莪最后的温柔 提交于 2019-12-02 14:17:14
题目传送门: https://www.luogu.org/problem/P1402 题目描述 XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化。由于很多来住店的旅客有自己喜好的房间色调、阳光等,也有自己所爱的菜,但是该酒店只有p间房间,一天只有固定的q道不同的菜。 有一天来了n个客人,每个客人说出了自己喜欢哪些房间,喜欢哪道菜。但是很不幸,可能做不到让所有顾客满意(满意的条件是住进喜欢的房间,吃到喜欢的菜)。 这里要怎么分配,能使最多顾客满意呢? 输入格式 第一行给出三个正整数表示n,p,q(<=100)。 之后n行,每行p个数包含0或1,第i个数表示喜不喜欢第i个房间(1表示喜欢,0表示不喜欢)。 之后n行,每行q个数,表示喜不喜欢第i道菜。 输出格式 最大的顾客满意数。 输入输出样例 输入 #1 2 2 2 1 0 1 0 1 1 1 1 输出 #1  1 Solution: ~~本题一看就是网络流板子题嘛~~ 一开始看到这题是,二话不说,先构图,超级源点连到房间,房间连接到人,人再连接到饭菜,最后饭菜再连接到超级汇点。结果一交上去,妈耶!WA了7个。看了一遍又一遍代码,就是找不到BUG。莫非,此题有坑? 随后点开了题解,才知道我是真的菜,网络流根本就没学好。各位用网络流的读者,请注意本题有一个巨坑,请看下这幅图读者就会明白了 一个人只能吃一道菜

网络流Dinic--模板

元气小坏坏 提交于 2019-12-02 10:37:19
1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin); 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr 13 #include <string> 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include <deque> 17 #include <queue>//priority

【POJ 1273】Drainage Ditches网络流最大流模板

丶灬走出姿态 提交于 2019-12-02 06:46:54
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 of each ditch, so he can control at what rate water flows into that ditch. Farmer John knows not only how many gallons of water each ditch can transport per minute

[网络流24题] 魔术球问题

荒凉一梦 提交于 2019-12-02 02:03:00
每日热手题系列 跟最小路径覆盖差不多吧……总觉得输出方案做得很不优美,尤其是用了自己的老板子以后 #include <bits/stdc++.h> using namespace std; const int N = 16384, MAX_NODE = 262144; #define reset(x) memset(x,0,sizeof x) template <class T> class Graph_Maxflow_Dinic { public: T tInfinity; struct edge { int p, o; T c; }; int s, t; T ans=0, tans; T dis[MAX_NODE]; vector <edge> g[MAX_NODE]; Graph_Maxflow_Dinic() { tInfinity = 1e+9; } void make(int p, int q, T c) { int sz1 = g[p].size(), sz2 = g[q].size(); edge tmp; tmp.p = q; tmp.c = c; tmp.o = sz2; g[p].push_back(tmp); tmp.p = p; tmp.c = 0; tmp.o = sz1; g[q].push_back(tmp); } void reset_graph

方格取数--状压DP or 网络流

﹥>﹥吖頭↗ 提交于 2019-12-02 00:26:17
题意: http://acm.hdu.edu.cn/showproblem.php?pid=1565 取不相邻的点是权值最大。 这题可以网络流做,暂时先DP一下,网络流明天学一下~~ 1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin); 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr 13 #include <string> 14 #include <time.h>// srand(((unsigned)time(NULL)));