Codeforces Round #406 (Div. 1) B. Legacy(线段树优化建图)
来源:互联网 发布:太原淘宝电动三轮价 编辑:程序博客网 时间:2024/06/11 12:45
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 12 3 2 3 172 3 2 2 162 2 2 3 33 3 1 1 121 3 3 17
0 28 12
4 3 13 4 1 3 122 2 3 4 101 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) B. Legacy(线段树优化建图)
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
- Codeforces Round #275 Div.1 B Interesting Array --线段树
- Codeforces Round #399 (Div. 1 + Div. 2, combined) B. Code For 1 线段树
- Codeforces-786B-Legacy (线段树+最短路)
- Codeforces Round #275 (Div. 1)B(线段树+位运算)
- Codeforces Round #305 (Div. 1)B - Mike and Feet 线段树
- Codeforces Round #310 (Div. 1)B,C,D(set+线段树)
- Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树+二分
- Codeforces Round #426 (Div. 1) B. The Bakery (DP + 线段树)
- Codeforces Round #426 (Div. 1) B. The Bakery(dp+线段树)
- [SPFA的SLF优化] Codeforces Round #257 (Div. 1) B
- Codeforces Round #318(Div. 1) 573 D. Bear and Cavalry【dp+矩阵+线段树优化】
- 【线段树延迟更新】Codeforces Round #104 (Div. 1) E
- Codeforces Round #200 (Div. 1) (树上的线段树)
- Codeforces Round #179 (Div. 1) A题 线段树
- 线段树Codeforces Beta Round #99 (Div. 1)C
- Codeforces Round #250 (Div. 1) D 线段树
- SpringMVC Restful风格的CURD
- Flume NG 简介及配置实战
- iptables介绍
- UML类图几种关系的总结
- [机器学习]机器学习笔记整理04-术语概念解释
- Codeforces Round #406 (Div. 1) B. Legacy(线段树优化建图)
- 网络安全
- org.apache.catalina.loader.DevLoader overrides final method start.()V
- php
- 二叉树笔试面试常见算法题
- Reverse String II
- 编程时遇到的报错总结(持续更新)
- 第六章 图
- DAGScheduler类的功能注释翻译