---
{"category_name":"easy","problem_code":"PLNTPATH","problem_name":"Planetary Path","problemComponents":{"constraints":"- $1 \\leq N \\leq 2 * 10^4$\n- $1 \\leq K \\leq min(N, 1000)$\n- $N * K \\leq 2 * 10^6$\n- $1 \\leq A_i \\leq K$\n- $-1 \\leq C_{i,j} \\leq 10^9$","constraintsState":true,"subtasks":"- 30 points : $1 \\leq R \\leq 10000$\n- 70 points : $1 \\leq R \\leq 10^9$\n","subtasksState":false,"inputFormat":"- The first line will consist of three space-separated integers $N$, $K$, and $S$ (number of planets, number of alliances, and base planet).\n- Second-line will consist of $N$ space-separated integers representing $A_{i}$, the alliance corresponding to the $i^{th}$ planet.\n- Each of the next $N$ lines will consist of $K$ space-separated integers, where the $j^{th}$ integer of the $i^{th}$ line represents $C_{i, j}$, the cost to travel from planet $i$ to alliance $j$.","inputFormatState":true,"outputFormat":"Print $N$ space separated integers, the $i^{th}$ integer representing the minimal cost to reach the $i^{th}$ planet from the base station. For unreachable planets print $-1$.","outputFormatState":true,"sampleTestCases":{"0":{"id":1,"input":"5 3 4\n1 3 1 2 2 \n0 5 -1 \n-1 4 0 \n0 3 -1 \n4 0 -1 \n4 0 3 \n","output":"4 3 4 0 0 \n","explanation":"- As $4$ and $5$ belong to the same component, the cost of traveling in-between is $0$. \n\n- There are multiple shortest paths from $4$ to $1$. They are:\n$4 \\rightarrow 3 \\rightarrow 1, 4 \\rightarrow 5 \\rightarrow 3 \\rightarrow 1$, $4 \\rightarrow 1$, etc. All of these have equal costs, that is $4$.\n\n- Only path from $4$ to $2$ is $4 \\rightarrow 5 \\rightarrow 2$, which costs $3$ units.\n\n- Similarly for node $3$, the cost is $4$ units.","isDeleted":false}}},"video_editorial_url":"","languages_supported":{"0":"CPP14","1":"C","2":"JAVA","3":"PYTH 3.6","4":"CPP17","5":"PYTH","6":"PYP3","7":"CS2","8":"ADA","9":"PYPY","10":"TEXT","11":"PAS fpc","12":"NODEJS","13":"RUBY","14":"PHP","15":"GO","16":"HASK","17":"TCL","18":"PERL","19":"SCALA","20":"LUA","21":"kotlin","22":"BASH","23":"JS","24":"LISP sbcl","25":"rust","26":"PAS gpc","27":"BF","28":"CLOJ","29":"R","30":"D","31":"CAML","32":"FORT","33":"ASM","34":"swift","35":"FS","36":"WSPC","37":"LISP clisp","38":"SQL","39":"SCM guile","40":"PERL6","41":"ERL","42":"CLPS","43":"ICK","44":"NICE","45":"PRLG","46":"ICON","47":"COB","48":"SCM chicken","49":"PIKE","50":"SCM qobi","51":"ST","52":"SQLQ","53":"NEM"},"max_timelimit":1,"source_sizelimit":50000,"problem_author":"hitch_hiker42","problem_tester":"ShivY08","date_added":"18-07-2021","tags":{"0":"cdmn2021","1":"dijkstra","2":"easy","3":"hitch_hiker42"},"problem_difficulty_level":"Easy","best_tag":"Dijkstra Algorithm","editorial_url":"https://discuss.codechef.com/problems/PLNTPATH","time":{"view_start_date":1630603800,"submit_start_date":1630603800,"visible_start_date":1630603800,"end_date":1735669800},"is_direct_submittable":false,"problemDiscussURL":"https://discuss.codechef.com/search?q=PLNTPATH","is_proctored":false,"visitedContests":{},"layout":"problem"}
---
In order to expand its reach to the outer universe and ensure universal peace, the [MIB](https://en.wikipedia.org/wiki/Men_in_black) has decided to expand its offices.
Namely, it has established its office on $N$ most important planets in the universe, with the base station on the planet $S$. Each office has an intergalactic teleporter that connects it to other planet offices. But the teleporters are bound to a planet's policy, so you cannot teleport to any planet you wish.
Due to intergalactic feuds, the planets are divided into $K$ different alliances, numbered $1$ through $K$. You know for each planet $i$, the alliance that it belongs to. In other words, you're given a sequence $A_{1}, A_{2}, \dots, A_{n}$, which says that planet $i$ belongs to alliance $A_{i}$.
Each planet $i$ has a policy of teleportation to the planets of other alliances:
- Cost to travel from one planet to another is determined by the alliance of the destination planet. More formally, the cost to reach a planet which belongs to alliance $j$ from planet $i$ is represented as $C_{i, j}$.
- Intra-alliance travel is free, i.e., if $A_{i} = j$, then $C_{i, j} = 0$.
- For a planet $i$, there are some forbidden alliances as well. For these alliances, $C_{i, j} = -1$, given that alliance $j$ is forbidden to travel from planet $i$.
As MIB is bound to follow the intergalactic rules and since they have limited funds (yes, the government is cheap everywhere), they have decided to find the most optimal way to travel to each of the planets from the base station at planet $S$. With all other agents busy with their missions, you are assigned the task to find these minimum-cost paths. This is your first task in MIB, so don't let them down!
<aside style='background: #f8f8f8;padding: 10px 15px;'><div>All submissions for this problem are available.</div></aside>