August Qing North Academy Training Day5

Today is Yang Siqi teachers teach ~

 

Shortest exercises:

 

POJ 1125 Stockbroker Grapevine

There are N stockbroker can pass messages to each other, there are a number of one-way communication path between them. Now there is a message to be delivered by someone began to everyone else, ask which person should be delivered by the order in the shortest possible time so that everyone receives the message.

answer:

The overall shortest, naked Floyd Needless to say, the time complexity of O (n- . 3 );

 

POJ 1502 MPI Maelstrom

It gives the N transmitting information between a lower triangular matrix, the time required for the processor, request information transmitted to all other processors from the first processor time required maximum.

answer:

We have to build n- 2  sides (the adjacency matrix), the shortest path requesting unit, again like Dijkstra, the time complexity of O (n- 2  log n-);

 

POJ 1511 Invitation Cards

N points of M have edges to FIG asking for all i, point 1 to point i from the time point 1 and return to the minimum required.

answer:

Forward edge construction, again a single source shortest seek point 1 to each of the shortest point;

The second question that every point we can not find a single source shortest again it QwQ ~

Then we can build the anti-side, do it again seek a single source shortest shortest path to each point just fine.

 

POJ 1724 ROADS

With N and M unidirectional urban road, each road having a length and two charge properties. Under the premise of seeking total cost does not exceed K Shortest from city 1 to the city N.

answer:

The idea seemed a comparison:

I can point to each split into K + 1 points:

Then run again Dijkstra, during the time it takes note there are no more than K.

Fairy practice:

Dijkstra, while maintaining the current path of the total cost, cost more than the upper limit of the state no longer transfer. 

 

POJ 1797 Heavy Transportation

Point M given by N to FIG edges, each edge has a maximum load. Seeking maximum weight from the points 1 through N.

answer:

1. We can answer maximum weight bipartite mid, and then we will delete all of the maximum load is less than the mid-side, and then run again bfs can be determined whether the connectivity 1 and n;

2. The maximum spanning tree, if adding one edge after 1 and n in the same collection, the then newly added side of the maximum load is the answer;

3. Shortest Path Algorithm: Dijkstra

D [u] from point 1 to point u maximum weight;

So when we run Dijkstra's just change it like a loose condition:

d [ v ] = min { d [ u ], w < u , v > } ;

 

POJ 1062 expensive dowry

Young explorers came to an Indian tribe. Where he and the chief's daughter fell in love, so go Qiuqin Bianxiang chiefs. Emirates to him as dowry was promised his daughter to him with 10,000 gold coins. Explorer not so much gold, it requested chiefs reduce the requirements. Emirates said:. "Ah, if you can get the coats of the high priest for me, I can as long as 8000 coins if you can got his crystal ball, so long as the 5000 gold on the line." Explorers went to the high priest there, she asked him for a fur coat or a crystal ball, the high priest asked him to use gold coins to change, or got other things for him, he can lower the price. Explorer so he went to other places, other people have made similar demands, or change in gold directly, or anything else you can find lower prices. But explorers did not need to replace it with something diverse things, because it would not get a lower price. Explorers now really need your help, let him marry his sweetheart with a minimum of gold. In addition, he will tell you that in this tribe, hierarchy is very strict. Will not carry out any form of direct contact between the position of the gap exceeds a certain limit of two people, including transaction. He is an outsider, it can be exempted from these restrictions. But if he had a deal and lower-status individuals, higher status people will not deal with him, they think it is tantamount to indirect contact, and vice versa. So you need to provide the best option for him after considering all the circumstances. 
For convenience, we put all the items are numbered starting from 1, the chief's promise also be seen as an object, and the number is always 1. Each item has a corresponding price P, the owner's position level L, and a range of alternatives and the alternatives Ti corresponding to the "preferential" Vi. If more than two people standing gap between the level of M, it can not be "indirect trade." You must be calculated based on these data explorer requires a minimum number of coins in order to marry the chief's daughter. 

answer:

It can be understood:

We have a thing u, will be able to spend less money, you can buy just w v a; substitutes are side, lower prices is the length of an article from the exchange to the chiefs promise is one path; 

Irrespective of the status of limit, starting from the chiefs promised to seek a single source shortest know from any point desired gold;

Enumeration status level interval, the interval length is M, the point is not within the range not through the level position, enum O (N);  

 

BZOJ 3040 * Shortest

N points, directed graph, for the most short-circuit point 1 to point N (the assurance exists) M of edges. 1 ≤ N ≤ 1000000, 1 ≤ M ≤ 10000000

By lydrainbowcat

Edge set divided into two parts: 

1. randomly generated; 2. given input;

answer:

Efficient heap to optimize Dijkstra's algorithm: 

Fibonacci heap;

Pairing heap;

In fact, ignoring the side randomly generated (which account for the bulk side) can get the right solution.

priority_queue STL achieve merger:

Heuristic merge, two stacks, A and B, and the smaller cut pile, placed inside a larger heap;

Each point log n times the cost of generating a maximum;

The total time complexity: O (n-(n-log) 2 );

 

Minimum spanning tree algorithm

Prim 

Kruskal

 

Prim

All points are divided into two sets, a set S and the point has been connected, and the set point T is not a communication;

Calculating a set of distance T u and each set S:

Selected from the set T S nearest point u, corresponding to the selected edge to the collection S; 

The above process is repeated until all the points are added to the set S simple time complexity of an inferior writing, the stack may be employed to optimize O ((N + M) log N);

Correctness proof:

Prim is a greedy algorithm, and may be required to prove using induction correctness.

First prove first selected edge E . 1 must contain a certain optimum embodiment;

If the optimal solution does not contain the edge e 1 , e is added 1 will appear ring on the ring and there ratio e 1 larger side, with e 1 answers Alternatively the better;

假设最优解包含前 k 个选中的边,e1, e2, . . . , ek,则类似地 可证明 ek+1 存在于最优解中;

运用归纳法,Prim 算法得到的 n − 1 条边构成最优解;

 

Kruskal 

将所有边按照边权从小到大排序; 

依次考虑每一条边 < ui , vi >,如果这条边和之前选中的边形成环,则不选中此边;反之,选中此边;

当考虑遍所有边后,选中的边一定构成了一棵最小生成树需要并查集的支持,时间复杂度一般认为是 O ( M log M );

正确性的证明:

证明依赖于拟阵的知识。

拟阵:只要具有遗传性和交换性就说明是个拟阵;

遗传性:若 S 是一个独立集,那么 S 的子集 S ′ 是独立集。

遗传性的推论:空集是独立集。

交换性:若 A 和 B 是 S 的两个独立集且 |A| < |B|(集合元素个数),那么存在一 个元素 x 满足 x  A 且 x ∈ B,使得 A ∪ {x} 是一个独立集

交换性的推论:一个集合的所有极大独立集大小都相同。

例:线性无关组、无向图生成森林。

拟阵最优化问题:将集合中每个元素赋予一个权值,求权值和最 小 (大) 的极大独立集。

拟阵最优化的贪心算法:

维护当前独立集 G,初始为空。将元素按照权值排序,从小到大 枚举元素 x,若 G ∪ {x} 是一个独立集,那么就将 x 加入独立集 并将 x 的权值累加入答案。最后的结果就是权值和最小的及大 独立集。

证明: 如果最优解不包含最小元素 x1,记该集合为 A,创建新的集合 B = {x1},利用交换性不断将 A 中元素加入 B 直到 |A| = |B|, 则有 B 集合为更优解,矛盾。故 x 一定属于最优解。利用数学归纳法,假设已经证明 x1, x2, . . . , xk−1 属于最优解,如果存在最 优解不包含 xk,还是创建新的集合 B {x1, x2, . . . , xk−1, xk }, 利用交换性将元素加入 B 得到更优解,矛盾。

 

POJ 1258 Agri-Net

有 N 个村庄,村庄之间形成完全图。现给出邻接矩阵,选择总长度尽可能小的边将 N 个村庄连通。

题解:

裸的最小生成树算法,直接 Kruskal;

 

POJ 2421 Constructing Roads

有 N 个村庄,有一些道路已经存在,现在希望用最少的总长度 将所有村庄连通。

题解:

把存在的道路的边权设置成 0,然后一遍 Kruskal

 

POJ 2560 Freckles

给出平面上 N 个点,求将所有点连通的最小距离和。

题解:

n2 建边,跑一遍 Kruskal ;

 

POJ 1789 Truck History

有 N 个编号,每个编号均为 7 位数。两个编号之间的距离定义为其不同位个数,由一个编号生成另一个编号代价为两个编号的 距离。希望用最小总代价从某一编号开始生成所有编号。

题解:

n2 建边,边权是两个编号之间不同位的个数;

 

BZOJ 1601 灌水

Farmer John 已经决定把水灌到他的 n (1 ≤ n ≤ 300) 块农田,农田被数字 1 到 n 标记。把一块土地进行灌水有两种方法,从其他农田引水,或者这块土地建造水库。建造一个水库需要花费 wi(1 ≤ wi ≤ 100000), 连接两块土地需要花费 pi,j(1 ≤ pi,j ≤ 100000, pi,j = pj,i , pi,i = 0)。

计算 Farmer John 所需的最少代价。

题解:

建立超级水库点,在某点建立水库视为选择长度为 wi 的边将其和超级水库连通,引水就是选择长度为 pi,j 的边。目标是选择长度和尽可能小的边,使得所有点和超级水库连通。

 

BZOJ 2753 滑雪与时间胶囊

a 来到雪山滑雪,这里分布着 M 条供滑行的轨道和 N 个轨道之间的交点 (同时也是景点),而且每个景点都有一编号 i 和一高度 Hi。a 能从景点 i 滑到景点 j 当且仅当存在一条 i 和 j 之间的边, 且 i 的高度不小于 j 。a 喜欢用最短的滑行路径去访问尽量多的 景点。如果仅仅访问一条路径上的景点,他会觉得数量太少。于是 a 拿出了他随身携带的时间胶囊。这是一种很神奇的药物,吃下之后可以立即回到上个经过的景点(不用移动也不被认为是 a 滑行的距离)。请注意,这种神奇的药物是可以连续食用的,即能够回到较长时间之前到过的景点(比如上上个经过的景点和上上上个经过的景点)。现在,a 站在 1 号景点望着山下的目标,心潮澎湃。他〸分想知道在不考虑时间胶囊消耗的情况下,以最短滑行距离滑到尽量多的景点的方案 (即满足经过景点数最大的前提下使得滑行总距离最小) 。你能帮他求出最短距离和景点数吗?

题解:

能够到达的景点容易通过 DFS 或 BFS 求出。

最优解应当构成树形,所需要的时间即为所有边长度之和。易联想到用最小生成树 Kruskal 算法解决本问题。

如何设置 Kruskal 时边的优先级?如何保证选出的有向边集使得 每个点从 1 出发可达?

以到达点高度为第一关键字,边长度为第二关键字。到达点高度高的边优先,同样高时边长度短的优先。

 

BZOJ 2561 最小生成树 *

给定一个边带正权的连通无向图 G < V, E >,其中 N = |V|, M = |E|,N 个点从 1 到 N 依次编号,给定三个正整 数 u, v, L (u ≠ v),假设现在加入一条边权为 L 的边 (u, v),那 么需要删掉最少多少条边,才能够使得这条边既可能出现在最小生成树上,也可能出现在最大生成树上?

前置技能:网络流最小割

题解:

考虑 Kruskal 的过程,如果加入所有权值小于 L 的边后,u 和 v 连通,则新边不可能出现在最小生成树中;

反之,如果 u 和 v 没有连通,则新边存在于至少一棵最小生成树中。

故加入所有权值小于 L 的边,求从 u 到 v 的最小割,删去这些 边以保证新边可能出现在最小生成树中。

同理,加入所有权值大于 l 的边,再求从 u 到 v 的最小割,删 去这些边使新边可能出现在最大生成树中。 答案即为两次最小割删边数量之和。

 

树上倍增 

有根树(随意定根) 

最近公共祖先 

链上信息(和、最值) 

优秀的时间复杂度

 

序列倍增

回忆普通的序列倍增思想,以 ST 表为例。 

Fi,j 记录区间 [ i, i + 2j − 1 ] 内信息(区间和或区间最值) 

Fi,j = merge (Fi,j−1, Fi+2 j−1,j−1 ) # 取出区间 [l, r] 答案时,使用若干个 Fi,j 即可 # 如果求区间最值,那么取 Fl,k 和 Fr−2 k ,k 即可,其中 k ⌊lo g2(r − l + 1⌋ # 如果求区间和,可以取 Fl,k1 , Fl+2 k1 ,k2 , Fl+2 k1+2 k2 ,k3 , . . . 即可

 

树上倍增

树上从每个点出发到根结点的链也具有类似的形式。 
Fi,j 表示点 i 向上走 2j 步的结点编号;

Fi,0 就是点 i 的父亲节点;

 

如何求解 u 向上移动 k 步是哪个点?

将 k 写作 2 的幂次之和,如 11 = 23 + 21 + 20。 用 Gi,j 表示 i 向上移动 j 步的结果。

在 O( log N )  步内完成。

 

最近公共祖先(LCA)

树上倍增最常见的用处是求解两个点的最近公共祖先。

求解 a 和 b 的最近公共祖先 :

将 a 和 b 调整到相同高度;

判断 a 和 b 是否重合,若重合则该点即为答案;

令 a 和 b 一起向上移动尽可能大的距离,保证移动后两点不重合

此时两点的父亲结点即为答案,单次询问时间复杂度 O( log N );

 

最近公共祖先的常见求解方法有:

1. 树上倍增; 

2. 树链剖分; 

3. DFS 序 + RMQ;

构造 DFS 序:

我们在 dfs 搜索的时候,假如我们搜到了第 u 个点,我们将 u 放进 dfs 序里面,搜索它的子树然后回溯到点 u 的时候我们再将 u 放进 dfs 序里面,这样算过来一个点 u 在 dfs 序中出现的次数为:(u 的儿子个数 + 1);

考虑这样一件事情:

我们在 dfs 序中看到了这么一段:   

aaaaaysqnpbbbbb

我们发现了一段连续的 a 和 b ;

那么在最后一个a ~ 第一个b 这段区间内,一定有一个是 a 和 b 的 LCA !

根据 dfs 的顺序可知:递归右子树之前一定会先经过 LCA,那么 LCA 会被扔进 dfs 里面!

所以我们在这一段区间内求出深度最小的点,那个点就是 a 和 b 的 LCA

 

 

向上路径

如何求解从 u 到 v 路径上边权最值?保证 v 是 u 的祖先。

在树上倍增向上走时取移动区间最值更新答案即可。

 

树上路径

树上路径 记 g = LCA(u, v),则树上从 u 到 v 的路径可以拆分为两段: 

从 u 到 g 的路径;

从 g 到 v 的路径 。

如何求解从 u 到 v 路径上的边权和?

将路径拆分为两段向上路径,分别求解其答案再合并。

 

树链剖分 

树链:不拐弯的路径;

剖分:每个点属于一条链;

应当具有优良的性质。

重儿子:子树大小最大的子结点;

重链:从一点出发,一直选择重儿子向下走,走到叶子;

轻边:不属于任何一条重链的边;

每一条边要么属于重链要么属于轻边;

分析:从任意一点 u 走到根结点,经过的重链、轻边个数的量级是多少?

走一条重链、轻边,子树大小至少翻倍,故易知 O ( log N ) 。

 

Guess you like

Origin www.cnblogs.com/xcg123/p/11331000.html