网络流

魔术球问题(网络流24题)

巧了我就是萌 提交于 2019-11-26 22:30:22
问题描述: 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为1,2,3,...的球。 (1)每次只能在某根柱子的最上面放球。 (2)在同一根柱子中,任何2个相邻球的编号之和为完全平方数。 试设计一个算法,计算出在n根柱子上最多能放多少个球。例如,在4 根柱子上最多可放11 个球。 «编程任务: 对于给定的n,计算在n根柱子上最多能放多少个球。 题解 考虑到当球的数量增加时柱子数量不严格单增,所以可以一个一个加球判定需要几个柱子装得下。 把每个球拆成两个点,互成平方数的两个球左右连边,为了确保只连一次从大的向小的连边。 那么问题就变成了最小路径覆盖,路径数就是柱子数。 枚举球为m个,当路径数大于柱子数时答案就是m-1,至于输出方案就从1遍历路径就好了。 为了不超时,我们新加一个球,就从这个球跑一次增广路即可,这也是为什么从大的往小的连边。 #include<bits/stdc++.h> using namespace std; const int maxn=3605; int n,m; int vis[maxn],match[maxn],timer; bool square[maxn<<1]; vector<int> e[maxn]; void init(){ timer=0; memset(match,0,sizeof(match)); memset(vis,0

浅谈网络流

六月ゝ 毕业季﹏ 提交于 2019-11-26 20:36:40
网络流是什么? 网络流(network-flows)是一种类比水流的解决问题方法,与线性规划密切相关。网络流的理论和应用在不断发展,出现了具有增益的流、多终端流、多商品流以及网络流的分解与合成等新课题。网络流的应用已遍及通讯、运输、电力、工程规划、任务分派、设备更新以及计算机辅助设计等众多领域。 图论中的一种理论与方法,研究网络上的一类最优化问题 。1955年 ,T.E.哈里斯在研究铁路最大通量时首先提出在一个给定的网络上寻求两点间最大运输量的问题。1956年,L.R. 福特和 D.R. 富尔克森等人给出了解决这类问题的算法,从而建立了网络流理论。所谓网络或容量网络指的是一个连通的赋权有向图 D= (V、E、C) , 其中V 是该图的顶点集,E是有向边(即弧)集,C是弧上的容量。此外顶点集中包括一个起点和一个终点。网络上的流就是由起点流向终点的可行流,这是定义在网络上的非负函数,它一方面受到容量的限制,另一方面除去起点和终点以外,在所有中途点要求保持流入量和流出量是平衡的。如果把下图看作一个公路网,顶点v1…v6表示6座城镇,每条边上的权数表示两城镇间的公路长度。现在要问 :若从起点v1将物资运送到终点v6去 ,应选择那条路线才能使总运输距离最短?这样一类问题称为最短路问题 。 如果把上图看作一个输油管道网 , v1 表示发送点,v6表示接收点,其他点表示中转站

HZOJ Blue

我们两清 提交于 2019-11-26 20:28:57
Blue: 贪心。 我们不妨给蛤定一个先后顺序,则贪心策略即从右至左每只蛤依次往最远的石子跳。 证明: 如果最右的蛤不往最远的石子跳,而是选择了一个较近的石子,那么必然会存在一个该蛤左边的蛤越过了它跳向其右边。因为每个蛤的能力是相同的,我们可以交换路线使得该贪心策略不变差。 接着用归纳法可以证明对于所有蛤该策略最优。 复杂度O( N ) 各种大佬用各种方法A了这道题……ooo的sb线段树,什么set,队列啥的都用上了……貌似只有我和b哥打了网络流,(B神盖世)比我厉害用了线段树优化建边+网络流,复杂度什么都好像都说的过去,而我只是冲着那30分去的。 网络流就比较好想了,建立两个超级源点S,SS,S向SS连容量为m的边,将每个石头拆开,连容量为1的边(如果每个石头可以跳k次那网络流板erb正解),然后相距不超过d的石头连边,最大流就是答案,不过复杂度好像说不过去…… 正解: 将所有的蛤(不是青蛙吗???)看作一个整体,那么每次跳都会占据一段石头,这样是最优的,而且每次青蛙都会尽量向远处跳,所以我么可以得到这样一个结论:若一只蛤在i,下次跳最远能到j,那么最多会剩下j-i+1只蛤(即把之间全占满),这样取符合条件最大值就是了。可以用单调指针实现。 1 #include<iostream> 2 #include<cstdio> 3 #define LL long long 4

图论--网络流--最大流 POJ 2289 Jamie's Contact Groups (二分+限流建图)

微笑、不失礼 提交于 2019-11-26 18:27:02
Description Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very long contact list in her cell phone. The contact list has become so long that it often takes a long time for her to browse through the whole list to find a friend's number. As Jamie's best friend and a programming genius, you suggest that she group the contact list and minimize the size of the largest group, so that it will be easier for her to search for a friend's number among the groups. Jamie takes your advice and gives you her entire contact list containing her friends' names, the number of

图论--网络流--最大流 HDU 3572 Task Schedule(限流建图,超级源汇)

这一生的挚爱 提交于 2019-11-26 18:26:08
Problem Description Our geometry princess XMM has stoped her study in computational geometry to concentrate on her newly opened factory. Her factory has introduced M new machines in order to process the coming N tasks. For the i-th task, the factory has to start processing it at or after day Si, process it for Pi days, and finish the task before or at day Ei. A machine can only work on one task at a time, and each task can be processed by at most one machine at a time. However, a task can be interrupted and processed on different machines on different days. Now she wonders whether he has a

图论--网络流--最大流 HDU 2883 kebab(离散化)

南楼画角 提交于 2019-11-26 18:24:45
Problem Description Almost everyone likes kebabs nowadays (Here a kebab means pieces of meat grilled on a long thin stick). Have you, however, considered about the hardship of a kebab roaster while enjoying the delicious food? Well, here's a chance for you to help the poor roaster make sure whether he can deal with the following orders without dissatisfying the customers. Now N customers is coming. Customer i will arrive at time si (which means the roaster cannot serve customer i until time si). He/She will order ni kebabs, each one of which requires a total amount of ti unit time to get it

图论--网络流--最大流 洛谷P4722(hlpp)

筅森魡賤 提交于 2019-11-26 18:19:38
题目描述 给定 nn 个点,mm 条有向边,给定每条边的容量,求从点 ss 到点 tt 的最大流。 输入格式 第一行包含四个正整数nn、mm、ss、tt,用空格分隔,分别表示点的个数、有向边的个数、源点序号、汇点序号。 接下来mm行每行包含三个正整数u_iui​、v_ivi​、c_ici​,用空格分隔,表示第ii条有向边从u_iui​出发,到达v_ivi​,容量为c_ici​ 输出格式 一个整数,表示ss到tt的最大流 输入输出样例 输入 #1 7 14 1 7 1 2 5 1 3 6 1 4 5 2 3 2 2 5 3 3 2 2 3 4 3 3 5 3 3 6 7 4 6 5 5 6 1 6 5 1 5 7 8 6 7 7 输出 #1 14 输入 #2 10 16 1 2 1 3 2 1 4 2 5 2 2 6 2 2 3 5 1 3 6 1 4 5 1 4 6 1 1 7 2147483647 9 2 2147483647 7 8 2147483647 10 9 2147483647 8 5 2 8 6 2 3 10 2 4 10 2 输出 #2 8 //500ms 秒掉洛谷推流问题 #include <algorithm> #include <iostream> #include <cstring> #include <vector> #include <queue>

图论--网络流--费用流POJ 2195 Going Home

左心房为你撑大大i 提交于 2019-11-26 18:19:03
Description On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man. Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a

图论 - 网络流_最大流入门题

筅森魡賤 提交于 2019-11-26 18:17:57
照着刘汝佳的训练指南,网络流从开始学习到刷了10来题,共花了10天,其中不知道为什么有好几天的断档。。 网络流,目前我就用着dinic算法,一开始是用的EK算法,即普通的增广路算法。本来还打算搞下ISAP算法,不过听铭神说这两种算法他在做题时,一般ISAP能过的,Dinic一样过了。so,那就算了,暂时先放放,时间也不多。 ———————————————————————————————————————————————— 题目: http://vjudge.net/contest/view.action?cid=48586#overview 上面的链接是我挂在虚拟OJ上的contest,都是从下面网络流题集中调出来一些的。 网络流的题集:http://blog.csdn.net/shahdza/article/details/7779537 此处很齐全。 体会: 网络流主要注重你的构图能力,一开始还以为最大流的算法会很难的,其实还可以接受。 感觉以后刷专题要写博客的话,还是A一题写一篇,到最后再整合。一次写10多题的题解,好累。。。 ———————————————————————————————————————————————— A - PIGS poj 1149 一开始不会,后来看了看题解,神一般的分析,代码就不敲了。 http://www.cnblogs.com/sleeper

网络流-最小费用最大流

为君一笑 提交于 2019-11-26 18:17:21
定义 有时,网络中每条边除了容量还有费用。最小费用最大流(MincostMaxflow,简称MCMF)就是在最大流的基础上,费用最小。 实现 和EK类似,最小费用最大流的求法也是暴力寻找增广路,但是不是用Bfs进行增广,而是用Bellman-Ford或者Spfa进行增广。寻找增广路时,先要满足可以增广,然后检查费用是否更优秀,两者都满足,才修正。这样增广下来,不难发现肯定满足费用最小。 这个算法的效率是玄学……但是实际运用中应该可能大概够了吧。 模板 实在没找到模板题,用 POJ2135 当模板题好了。 因为每条道路只能通过一次,所以我们可以认为每条道路的容量为1,费用为这条道路的长度(由于这道题是双向边,所以必须正反都建边)。但是要走回来怎么办?因为不能重复走,所以走回来就等价于走两次(已双向建边),我们可以增加两个虚拟节点0和n+1:0向1构造一条容量为2,费用为0的边,n向n+1构造一条容量为2,费用为0的边。然后直接刷最小费用最大流即可。 #include<cstdio> #include<cstring> #include<algorithm> using namespace std ; const int maxn= 1000 ,maxm= 40000 ; int n,m,E,lnk[maxn+ 5 ],son[maxm+ 5 ],nxt[maxm+ 5 ]; int