D. Legacy

Codeforces
IDCF787D
Time2000ms
Memory256MB
Difficulty
data structuresgraphsshortest paths
English · Original
Chinese · Translation
Formal · Original
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. <center>![image](https://espresso.codeforces.com/3e33a87e8ba090af9cf4c5a8e27cb131ebefa1c7.png)</center>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: 1. With a plan of this type you can open a portal from planet _v_ to planet _u_. 2. With a plan of this type you can open a portal from planet _v_ to any planet with index in range \[_l_, _r_\]. 3. 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. ## Input 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 _w_ where _w_ is the cost of that plan (1 ≤ _v_ ≤ _n_, 1 ≤ _l_ ≤ _r_ ≤ _n_, 1 ≤ _w_ ≤ 109). ## Output 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. [samples] ## Note 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.
Rick 和他的同事研制出了一种新的放射性配方,许多坏人正在追捕他们。因此,Rick 希望在坏人抓住他们之前,将他的遗产交给 Morty。 在他们的宇宙中有 #cf_span[n] 个星球,编号从 #cf_span[1] 到 #cf_span[n]。Rick 位于编号为 #cf_span[s] 的星球(地球)上,但他不知道 Morty 在哪里。众所周知,Rick 拥有一把传送枪。用这把枪,他可以从当前所在的星球向任意其他星球(包括当前星球)开启一个单向传送门。但由于他仍在使用免费试用版,这把枪存在一些限制。 默认情况下,他无法使用这把枪开启任何传送门。网站上出售这种枪的计划共有 #cf_span[q] 种。每次购买一个计划后,你只能使用一次,但如果你想多次使用,可以再次购买。 网站上的计划有三种类型: Rick 不知道 Morty 在哪里,但 Unity 将通知他,他希望在找到 Morty 时能立即启程。因此,对于每个星球(包括地球本身),他都想知道自己从地球到达该星球所需的最少金钱。 输入的第一行包含三个整数 #cf_span[n]、#cf_span[q] 和 #cf_span[s](#cf_span[1 ≤ n, q ≤ 10^5],#cf_span[1 ≤ s ≤ n])——分别表示星球数量、计划数量和地球的编号。 接下来的 #cf_span[q] 行描述了这些计划。每行以一个数字 #cf_span[t] 开头,表示该计划的类型(#cf_span[1 ≤ t ≤ 3])。如果 #cf_span[t = 1],则后面跟着三个整数 #cf_span[v]、#cf_span[u] 和 #cf_span[w],其中 #cf_span[w] 是该计划的花费(#cf_span[1 ≤ v, u ≤ n],#cf_span[1 ≤ w ≤ 10^9])。否则,后面跟着四个整数 #cf_span[v]、#cf_span[l]、#cf_span[r] 和 #cf_span[w],其中 #cf_span[w] 是该计划的花费(#cf_span[1 ≤ v ≤ n],#cf_span[1 ≤ l ≤ r ≤ n],#cf_span[1 ≤ w ≤ 10^9])。 在输出的第一行且唯一一行中,输出 #cf_span[n] 个用空格分隔的整数。第 #cf_span[i] 个整数应为从地球到第 #cf_span[i] 个星球所需的最少金钱,如果无法到达则输出 #cf_span[ - 1]。 在第一个样例测试用例中,Rick 可以先购买第 #cf_span[4] 个计划一次,再购买第 #cf_span[2] 个计划,从而到达第 #cf_span[2] 个星球。 ## Input 输入的第一行包含三个整数 #cf_span[n]、#cf_span[q] 和 #cf_span[s](#cf_span[1 ≤ n, q ≤ 10^5],#cf_span[1 ≤ s ≤ n])——分别表示星球数量、计划数量和地球的编号。接下来的 #cf_span[q] 行描述了这些计划。每行以一个数字 #cf_span[t] 开头,表示该计划的类型(#cf_span[1 ≤ t ≤ 3])。如果 #cf_span[t = 1],则后面跟着三个整数 #cf_span[v]、#cf_span[u] 和 #cf_span[w],其中 #cf_span[w] 是该计划的花费(#cf_span[1 ≤ v, u ≤ n],#cf_span[1 ≤ w ≤ 10^9])。否则,后面跟着四个整数 #cf_span[v]、#cf_span[l]、#cf_span[r] 和 #cf_span[w],其中 #cf_span[w] 是该计划的花费(#cf_span[1 ≤ v ≤ n],#cf_span[1 ≤ l ≤ r ≤ n],#cf_span[1 ≤ w ≤ 10^9])。 ## Output 在输出的第一行且唯一一行中,输出 #cf_span[n] 个用空格分隔的整数。第 #cf_span[i] 个整数应为从地球到第 #cf_span[i] 个星球所需的最少金钱,如果无法到达则输出 #cf_span[ - 1]。 [samples] ## Note 在第一个样例测试用例中,Rick 可以先购买第 #cf_span[4] 个计划一次,再购买第 #cf_span[2] 个计划,从而到达第 #cf_span[2] 个星球。
**Definitions** Let $ n, q, s \in \mathbb{Z}^+ $ denote the number of planets, number of plans, and Earth's planet index, respectively. Let $ G = (V, E) $ be a directed weighted graph where $ V = \{1, 2, \dots, n\} $ represents planets. Each plan defines one or more directed edges: - **Type 1**: Given $ (v, u, w) $, add edge $ v \to u $ with weight $ w $. - **Type 2**: Given $ (v, l, r, w) $, add edges $ v \to i $ for all $ i \in [l, r] $, each with weight $ w $. - **Type 3**: Given $ (v, l, r, w) $, add edges $ i \to v $ for all $ i \in [l, r] $, each with weight $ w $. **Constraints** 1. $ 1 \leq n, q \leq 10^5 $ 2. $ 1 \leq s \leq n $ 3. For all plans: $ 1 \leq w \leq 10^9 $ 4. For Type 2/3: $ 1 \leq l \leq r \leq n $ **Objective** Compute the shortest path distances from vertex $ s $ to all vertices $ i \in \{1, 2, \dots, n\} $. Let $ d[i] $ be the minimum cost to reach planet $ i $ from planet $ s $. Output $ d[1], d[2], \dots, d[n] $, where $ d[i] = -1 $ if $ i $ is unreachable from $ s $.
Samples
Input #1
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
Output #1
0 28 12
Input #2
4 3 1
3 4 1 3 12
2 2 3 4 10
1 2 4 16
Output #2
0 -1 -1 12
API Response (JSON)
{
  "problem": {
    "name": "D. Legacy",
    "description": {
      "content": "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 ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF787D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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.\n\nThere are _n_ planets in their ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Rick 和他的同事研制出了一种新的放射性配方,许多坏人正在追捕他们。因此,Rick 希望在坏人抓住他们之前,将他的遗产交给 Morty。\n\n在他们的宇宙中有 #cf_span[n] 个星球,编号从 #cf_span[1] 到 #cf_span[n]。Rick 位于编号为 #cf_span[s] 的星球(地球)上,但他不知道 Morty 在哪里。众所周知,Rick 拥有一把传送枪。用这把枪,他可以...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, q, s \\in \\mathbb{Z}^+ $ denote the number of planets, number of plans, and Earth's planet index, respectively.  \nLet $ G = (V, E) $ be a directed weighted graph where $ V = ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments