{"problem":{"name":"B. 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":"CF786B"},"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 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.\n\n<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.\n\nPlans on the website have three types:\n\n1.  With a plan of this type you can open a portal from planet _v_ to planet _u_.\n2.  With a plan of this type you can open a portal from planet _v_ to any planet with index in range \\[_l_, _r_\\].\n3.  With a plan of this type you can open a portal from any planet with index in range \\[_l_, _r_\\] to planet _v_.\n\nRick 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.\n\n## Input\n\nThe 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.\n\nThe 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).\n\n## Output\n\nIn 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.\n\n[samples]\n\n## Note\n\nIn the first sample testcase, Rick can purchase 4th plan once and then 2nd plan in order to get to get to planet number 2.","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"里克和他的同事研制出了一种新的放射性配方，许多坏人正在追捕他们。因此，里克希望在坏人抓住他们之前，将他的遗产交给莫蒂。\n\n在他们的宇宙中有 #cf_span[n] 个星球，编号从 #cf_span[1] 到 #cf_span[n]。里克位于编号为 #cf_span[s] 的星球（地球）上，但他不知道莫蒂在哪里。众所周知，里克拥有一把传送枪。使用这把枪，他可以从当前所在的星球向任意其他星球（包括当前星球）开启一个单向传送门。但由于他仍在使用免费试用版，这把枪存在一些限制。\n\n默认情况下，他无法使用这把枪开启任何传送门。网站上出售这种枪的计划共有 #cf_span[q] 种。每次购买一个计划后，你只能使用一次，但如果你需要多次使用，可以再次购买。\n\n网站上的计划有三种类型：\n\n里克不知道莫蒂在哪里，但Unity将会通知他，他希望在找到莫蒂时能立即启程。因此，对于每个星球（包括地球本身），他都想知道自己从地球到达该星球所需的最少金钱。\n\n输入的第一行包含三个整数 #cf_span[n]、#cf_span[q] 和 #cf_span[s]（#cf_span[1 ≤ n, q ≤ 10^5]，#cf_span[1 ≤ s ≤ n]）——分别表示星球数量、计划数量和地球的编号。\n\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]）。\n\n在输出的第一行，也是唯一一行，输出 #cf_span[n] 个用空格分隔的整数。第 #cf_span[i] 个整数应表示从地球到达第 #cf_span[i] 个星球所需的最少金钱，如果无法到达则输出 #cf_span[ - 1]。\n\n在第一个样例中，里克可以购买第 #cf_span[4] 个计划一次，然后购买第 #cf_span[2] 个计划，以到达第 #cf_span[2] 个星球。\n\n## Input\n\n输入的第一行包含三个整数 #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]）。\n\n## Output\n\n在输出的第一行，也是唯一一行，输出 #cf_span[n] 个用空格分隔的整数。第 #cf_span[i] 个整数应表示从地球到达第 #cf_span[i] 个星球所需的最少金钱，如果无法到达则输出 #cf_span[ - 1]。\n\n[samples]\n\n## Note\n\n在第一个样例中，里克可以购买第 #cf_span[4] 个计划一次，然后购买第 #cf_span[2] 个计划，以到达第 #cf_span[2] 个星球。","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 = \\{1, 2, \\dots, n\\} $ represents planets.  \n\nEach plan defines one or more directed edges:  \n- **Type 1**: Given $ v, u, w $, add edge $ v \\to u $ with weight $ w $.  \n- **Type 2**: Given $ v, l, r, w $, add edges $ v \\to i $ for all $ i \\in [l, r] $, each with weight $ w $.  \n- **Type 3**: Given $ v, l, r, w $, add edges $ i \\to v $ for all $ i \\in [l, r] $, each with weight $ w $.  \n\n**Constraints**  \n1. $ 1 \\le n, q \\le 10^5 $  \n2. $ 1 \\le s \\le n $  \n3. For all plans: $ 1 \\le w \\le 10^9 $  \n4. For Type 2 and 3: $ 1 \\le l \\le r \\le n $, $ 1 \\le v \\le n $  \n\n**Objective**  \nCompute the shortest path distances from vertex $ s $ to all vertices $ i \\in \\{1, 2, \\dots, n\\} $.  \nLet $ d[i] $ be the minimum cost to reach planet $ i $ from planet $ s $.  \nOutput $ d[1], d[2], \\dots, d[n] $, where $ d[i] = -1 $ if $ i $ is unreachable from $ s $.","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF786B","tags":["data structures","graphs","shortest paths"],"sample_group":[["3 5 1\n2 3 2 3 17\n2 3 2 2 16\n2 2 2 3 3\n3 3 1 1 12\n1 3 3 17","0 28 12"],["4 3 1\n3 4 1 3 12\n2 2 3 4 10\n1 2 4 16","0 -1 -1 12"]],"created_at":"2026-03-03 11:00:39"}}