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

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;
//using System.Numerics;
using Enu = System.Linq.Enumerable;

public class Program
{
    public void Solve()
    {
        int V = Reader.Int(), E = Reader.Int(), S = Reader.Int();
        var dij = new Dijkstra(V);

        for (int i = 0; i < E; i++)
        {
            int a = Reader.Int(), b = Reader.Int(), c = Reader.Int();
            dij.AddEdge(a, b, c);
        }
        var mincost = dij.MinCost(S);
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        for (int i = 0; i < V; i++)
            Console.WriteLine(mincost[i] == Dijkstra.INF ? "INF" : mincost[i] + "");
        Console.Out.Flush();
        Console.ReadLine();
    }

    class Dijkstra
    {
        public static readonly int INF = (int)1.1e9;
        private readonly List<Edge>[] G;

        public Dijkstra(int V)
        {
            G = new List<Edge>[V];
            for (int i = 0; i < G.Length; i++) G[i] = new List<Edge>();
        }
        public void AddEdge(int from, int to, int cost)
        {
            G[from].Add(new Edge(to, cost));
        }
        public int[] MinCost(int start)
        {
            var minCost = new int[G.Length];
            for (int i = 0; i < minCost.Length; i++)
                minCost[i] = INF;
            var g = new Edge[G.Length][];
            for (int i = 0; i < G.Length; i++)
                g[i] = G[i].ToArray();
            minCost[start] = 0;
            var heap = new Heap<Vertex>();
            heap.Push(new Vertex(start, 0));

            while (heap.Count > 0)
            {
                var t = heap.Pop();
                int a = t.v;
                var cost = t.cost;
                if (cost > minCost[a]) continue;

                foreach (var e in g[a])
                {
                    int b = e.to;
                    var nextCost = cost + e.cost;
                    if (nextCost < minCost[b])
                    {
                        minCost[b] = nextCost;
                        heap.Push(new Vertex(b, nextCost));
                    }
                }
            }
            return minCost;
        }
    }

    public struct Vertex : IComparable<Vertex>
    {
        public int v;
        public int cost;
        public Vertex(int v, int cost) { this.v = v; this.cost = cost; }
        public int CompareTo(Vertex other) { return Math.Sign(cost - other.cost); }
    }
    public struct Edge
    {
        public int to;
        public int cost;
        public Edge(int to, int cost) { this.to = to; this.cost = cost; }
    }

    class Heap<T>
    {
        private const int InitialSize = 1024;
        private readonly int sign;
        private readonly Comparer<T> comp = Comparer<T>.Default;
        private T[] A = new T[InitialSize];
        public int Count { get; private set; }
        public Heap(bool greater = false) { sign = greater ? 1 : -1; }
        public T Peek() { return A[0]; }
        public bool Empty() { return Count == 0; }
        public void Push(T val)
        {
            if (Count == A.Length)
            {
                T[] next = new T[A.Length * 2];
                Array.Copy(A, next, A.Length);
                A = next;
            }
            A[Count++] = val;
            int i = Count - 1;
            while (i > 0)
            {
                int parent = (i - 1) / 2;
                if (Math.Sign(comp.Compare(val, A[parent])) == sign)
                {
                    A[i] = A[parent];
                    i = parent;
                }
                else break;
            }
            A[i] = val;
        }
        public T Pop()
        {
            T res = A[0];
            T val = A[--Count];
            if (Count == 0) return res;
            int i = 0;
            while (i * 2 + 1 < Count)
            {
                int L = i * 2 + 1;
                int R = i * 2 + 2;
                if (R < Count && Math.Sign(comp.Compare(A[R], A[L])) == sign) L = R;
                if (Math.Sign(comp.Compare(A[L], val)) == sign) { A[i] = A[L]; i = L; }
                else break;
            }
            A[i] = val;
            return res;
        }
    }
}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    static T[] Range<T>(int N, Func<T> f) { return Enu.Range(0, N).Select(i => f()).ToArray(); }
    static string[] Split(string s) { return s.Split(separator, op); }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}