
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。
让我们先从一道题开始。
1、例题
| Source | Problem | TimeLimit | MemoryLimit |
|---|---|---|---|
Codeforces Round #406 (Div. 2) |
Legacy | 2 2 2 seconds | 256 256 256 megabytes |
Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them. So Rick wants to give his legacy to Morty before bad guys catch them.
There are n planets in their universe numbered from 1 1 1 to n n n. Rick is in planet number s s s (the earth) and he doesn’t know where Morty is. As we all know, Rick owns a portal gun. With this gun he can open one-way portal from a planet he is in to any other planet (including that planet). But there are limits on this gun because he’s still using its free trial.
By default he can not open any portal by this gun. There are q q q plans in the website that sells these guns. Every time you purchase a plan you can only use it once but you can purchase it again if you want to use it more.
Plans on the website have three types:
- With a plan of this type you can open a portal from planet v v v to planet u.
- With a plan of this type you can open a portal from planet v v v to any planet with index in range [ l , r ] [l, r] [l, r].
- With a plan of this type you can open a portal from any planet with index in range [ l , r ] [l, r] [l, r] to planet v v v.
Rick doesn’t known where Morty is, but Unity is going to inform him and he wants to be prepared for when he finds and start his journey immediately. So for each planet (including earth itself) he wants to know the minimum amount of money he needs to get from earth to that planet.
Input
The first line of input contains three integers n, q and s ( 1 ≤ n , q ≤ 1 0 5 , 1 ≤ s ≤ n 1 ≤ n, q ≤ 10^5, 1 ≤ s ≤ n 1 ≤ n, q ≤ 105,1 ≤ s ≤ n) — number of planets, number of plans and index of earth respectively.
The next q lines contain the plans. Each line starts with a number t, type of that plan ( 1 ≤ t ≤ 3 1 ≤ t ≤ 3 1 ≤ t ≤ 3). If t = 1 t = 1 t = 1 then it is followed by three integers v , u v, u v,u and w w w where w w w is the cost of that plan ( 1 ≤ v , u ≤ n , 1 ≤ w ≤ 1 0 9 1 ≤ v, u ≤ n, 1 ≤ w ≤ 10^9 1 ≤ v, u ≤ n,1 ≤ w ≤ 109). Otherwise it is followed by four integers v , l , r v, l, r v,l,r and w w w where w is the cost of that plan ( 1 ≤ v ≤ n , 1 ≤ l ≤ r ≤ n , 1 ≤ w ≤ 1 0 9 1 ≤ v ≤ n, 1 ≤ l ≤ r ≤ n, 1 ≤ w ≤ 10^9 1 ≤

本文通过一道例题介绍了线段树优化建图的方法,解决了一对多和多对一连边导致的复杂度问题。利用线段树,将建图规模从O(nq)降低到O(n+q),并提供了解题思路和代码实现,强调线段树在区间操作中的应用价值。
最低0.47元/天 解锁文章
289

被折叠的 条评论
为什么被折叠?



