Problem:
Single Source Shortest Path

For a given weighted graph G(V, E) and a source r, find the source shortest path to each vertex from the source (SSSP: Single Source Shortest Path).

Input

An edge-weighted graph G (V, E) and the source r.

|V| |E| r
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1

|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the source of the graph.

si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.

Output

Print the costs of SSSP in the following format.

c0
c1
:
c|V|-1

The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, ... |V|-1 in order. If there is no path from the source to a vertex, print INF.

Constraints

1 ≤ |V| ≤ 100000

0 ≤ di ≤ 10000

0 ≤ |E| ≤ 500000

There are no parallel edges

There are no self-loops

Sample Input 1

4 5 0
0 1 1
0 2 4
1 2 2
2 3 1
1 3 5

Sample Output 1

0
1
3
4

Sample Input 2

4 6 1
0 1 1
0 2 4
2 0 1
1 2 2
3 1 1
3 2 5

Sample Output 2

3
0
2
INF

Solution in OCaml (judge verdict: Accepted)

module ArrayL = ArrayLabels
module ListL = ListLabels

let dbg = Printf.printf "[debug]%s"

let max_num = 1_000_000_000

let id = fun x -> x
let tuple2 x y = (x,y)
let tuple3 x y z = (x,y,z)
let succ x = x + 1
let pred x = x - 1

let (++) n m =
  let rec aux i =
    if i = m then [m]
    else i :: aux (i+1) in
  if n > m then [] else aux n

let (++^) n m = n ++ (m-1)

let scan fmt f = Scanf.sscanf (read_line ()) fmt f

let scan_lines n fmt f =
  List.map (fun _ -> scan fmt f) (0++^n)

let scan_matrix n m e conv =
  let arr = Array.make_matrix n m e in
  Array.iteri (fun i line ->
      let s = Scanf.Scanning.from_string @@ read_line () in
      Array.iteri (fun j _ ->
          arr.(i).(j) <- Scanf.bscanf s " %s" conv;
        ) line) arr; arr

let between n x m = n <= x && x < m

let string_to_list s =
  List.map (String.get s) (0 ++^ String.length s)

let (v,e,r) = scan "%d %d %d" tuple3
let ls = scan_lines e "%d %d %d" tuple3
let edges =
  let arr = Array.make v [] in
  ListL.iter ls ~f:(fun (s,t,d)->
      arr.(s) <- (t,d)::arr.(s));
  arr

let dists = Array.make v max_num
let visited = Array.make v false

module Heap = Set.Make(struct
    type t = int*int
    let compare (x0, y0) (x1,y1) =
      match compare y0 y1 with
      | 0 -> compare x0 x1
      | c -> c
  end)

let rec solve h =
  if Heap.is_empty h then ()
  else
    let (t, dist) = Heap.min_elt h in
    if visited.(t) then solve @@ Heap.remove (t, dist) h
    else
      begin
        visited.(t) <- true; dists.(t) <- dist;
        solve @@ ListL.fold_left edges.(t)
          ~init:(Heap.remove (t, dist) h) ~f:(fun h (s, d) ->
              if not visited.(s) then Heap.add (s, dists.(t) + d) h
              else h)
      end

let () =
  solve @@ (Heap.empty |> Heap.add (r,0));
  ArrayL.iter dists ~f:(fun v ->
      if v = max_num then print_string "INF\n"
      else Printf.printf "%d\n" v)