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 JAVA (judge verdict: Accepted)

public class
Main
{
  @SuppressWarnings("unchecked")
  public void
  run (
    java.io.InputStream in,
    java.io.PrintStream out
    )
  {
    java.util.Scanner sc = new java.util.Scanner ( in );
    java.util.List[ ] G, p;
    int[ ] res;
    int n, m, r;
    int i;

    n = sc.nextInt ( );
    m = sc.nextInt ( );
    r = sc.nextInt ( );

    G = new java.util.List[ n ];
    p = new java.util.List[ n ];
    for ( i = 0; i < n; ++i )
    {
      G[ i ] = new java.util.LinkedList ( );
      p[ i ] = new java.util.LinkedList ( );
    }
    for ( i = 0; i < m; ++i )
    {
      int s, t, d;

      s = sc.nextInt ( );
      t = sc.nextInt ( );
      d = sc.nextInt ( );
      G[ s ].add ( t );
      p[ s ].add ( d );
    }

    res = dijkstra ( convertGraph ( G ), convertGraph ( p ), r );
    for ( i = 0; i < res.length; ++i )
    {
      if ( res[ i ] < 0 )
        out.println ( "INF" );
      else
        out.println ( res[ i ] );
    }

    sc.close ( );
  }

  /** ??°?????????List ??¨???????¨?????????????????????? */
  private static int[ ][ ]
  convertGraph (
    java.util.List[ ] G
    )
  {
    int[ ][ ] r = new int[ G.length ][ ];
    java.util.Iterator it;
    int i, j;

    for ( i = 0; i < r.length; ++i )
    {
      r[ i ] = new int[ G[ i ].size ( ) ];
      for ( j = 0, it = G[ i ].iterator ( ); it.hasNext ( ); ++j )
        r[ i ][ j ] = ( (Integer)it.next ( ) ).intValue ( );
    }

    return ( r );
  }

  /** ??????????????°??????(G,p) ???????????????
    * ??????s ????§??????¨????????????????????????????????????????±??????????
    * 
    * r[ t ]: ??????s ????????????t ????????????????????? (???????????°?????????)
    */
  private static int[ ]
  dijkstra (
    int[ ][ ] G,
    int[ ][ ] p,
    int s
    )
  {
    class Status implements Comparable<Status> {
      int v, d;
      public Status ( int v, int d ) { this.v = v; this.d = d; }
      public int compareTo ( Status o ) { return ( this.d - o.d ); }
    }
    java.util.PriorityQueue<Status> lis;
    int[ ] r;
    int i;

    r = new int[ G.length ];
    java.util.Arrays.fill ( r, -1 );

    lis = new java.util.PriorityQueue<Status> ( );
    lis.add ( new Status ( s, 0 ) );
    while ( !lis.isEmpty ( ) )
    {
      Status st = lis.poll ( );

      if ( r[ st.v ] >= 0 )
        continue ;

      r[ st.v ] = st.d;
      for ( i = 0; i < G[ st.v ].length; ++i )
        lis.add ( new Status ( G[ st.v ][ i ], r[ st.v ] + p[ st.v ][ i ] ) );
    }

    return ( r );
  }

  public static void
  main (
    String[ ] args
    )
  {
    ( new Main ( ) ).run ( System.in, System.out );
  }
}