Codeforces Round #406 (Div. 1) B. Legacy(線段樹優化建圖)
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 to n. Rick is in planet number 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 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 to planet u.
- With a plan of this type you can open a portal from planet v to any planet with index in range [l, r].
- With a plan of this type you can open a portal from any planet with index in range [l, r] to planet 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.
The first line of input contains three integers n, q and s (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). If t = 1 then it is followed by three integers v, u and w where w is the cost of that plan (1 ≤ v, u ≤ n, 1 ≤ w ≤ 109). Otherwise it is followed by four integers v, l, r and wwhere w is the cost of that plan (1 ≤ v ≤ n, 1 ≤ l ≤ r ≤ n, 1 ≤ w ≤ 109).
In the first and only line of output print n integers separated by spaces. i-th of them should be minimum money to get from earth to i-th planet, or - 1 if it's impossible to get to that planet.
3 5 1 2 3 2 3 17 2 3 2 2 16 2 2 2 3 3 3 3 1 1 12 1 3 3 17
0 28 12
4 3 1 3 4 1 3 12 2 2 3 4 10 1 2 4 16
0 -1 -1 12
In the first sample testcase, Rick can purchase 4th plan once and then 2nd plan in order to get to get to planet number 2.
分析:如果直接存邊的話是放不下的,最多有n^2條邊,但是因為這道題的特殊性,我們可以用線段樹來輔助建圖,對於第一類邊我們直接連邊,第二類點對區間,我們先建棵線段樹,然後樹上所有父節點向兒子連邊,然後自頂向下的把第二類邊再加入圖中,第三類邊和第二類同理,但是注意要另建一棵線段樹,這樣建完圖後點數還是O(n)邊數是O(n+q),跑一邊dij最短路複雜度O((n+q)logn).
相關文章
- Codeforces Round #406 (Div. 1) C. Till I Collapse(可持久化線段樹)持久化
- Codeforces Round #373 (Div. 1) C. Sasha and Array 線段樹
- Codeforces Round #407 (Div. 1) B. Weird journey
- Codeforces Round #491 (Div. 2) B. Getting an A
- Codeforces Round #242 (Div. 2) B. Megacity
- Codeforces Round #249 (Div. 2) B. Pasha Maximizes
- Codeforces Round #252 (Div. 2) B. Valera and FruitsUI
- Codeforces Round #246 (Div. 2) B. Football Kit
- Codeforces Round #244 (Div. 2) B. Prison Transfer
- Codeforces Round #228 (Div. 2) B. Fox and CrossROS
- Codeforces Round #215 (Div. 2) B. Sereja and Suffixes
- Codeforces Round #243 (Div. 2) B. Sereja and Mirroring
- Codeforces Round #196 (Div. 2) B. Routine Problem
- Codeforces Round #251 (Div. 2) B. Devu, the Dumb Guydev
- Codeforces Round #209 (Div. 2) B. Permutation
- 線段樹最佳化建圖
- Codeforces Round 934 (Div. 1)
- Codeforces Round #676 (Div. 2) B. Putting Bricks in the Wall
- Codeforces Round #213 (Div. 2) B. The Fibonacci Segment
- Codeforces Round #248 (Div. 2) B. Kuriyama Mirai's StonesAI
- Codeforces Round #216 (Div. 2) B. Valera and Contest
- Codeforces Round 928 (Div. 4) B. Vlad and Shapes
- Codeforces Round 947 (Div. 1 + Div. 2)
- Codeforces Round #675 (Div. 2) 1442 F - Boring Queries 可持久化線段樹維護 區間乘法持久化
- Codeforces Round #235 (Div. 2) B. Sereja and Contests
- Codeforces Round #233 (Div. 2) B. Red and Blue Balls
- Codeforces Round #192 (Div. 2) B. Road ConstructionStruct
- Codeforces Round 709 (Div. 1)
- Codeforces Round 969 (Div. 1) 記錄
- Codeforces Round 959 sponsored by NEAR (Div. 1 + Div. 2)
- 【Codeforces Round #499 (Div. 1) B】Rocket
- 線段樹最佳化建圖學習筆記筆記
- Codeforces Round #537 (Div. 2)B. Average Superhero Gang Power (貪心+暴力)
- Codeforces Beta Round #76 (Div. 1 Only) A. Frames
- Codeforces Round 943 (Div. 3)(C-G1)
- Codeforces Testing Round #10 B. Balancer
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)