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 static System.Math;
using static System.Console;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AOJ
{
    class Program
    {
        static void Main(string[] args)
        {
            var firstLine = ReadLine().Split(' ').Select(value => int.Parse(value)).ToArray();
            var v = firstLine[0];
            var e = firstLine[1];
            var r = firstLine[2];
            var edges = new Dictionary<int, List<int[]>>();
            var costs = new int[v];
            var isFixed = new bool[v];

            for (var i = 0; i < v; i++)
            {
                edges.Add(i, new List<int[]>());
                costs[i] = int.MaxValue;
            }
            for(var i = 0; i < e; i++)
            {
                var line = ReadLine().Split(' ').Select(value => int.Parse(value)).ToArray();
                edges[line[0]].Add(new int[2] { line[1], line[2] });
            }

            costs[r] = 0;
            var queue = new PriorityQueue(10 * v);
            queue.Enqueue(r, costs[r]);

            while(queue.Count > 0)
            {
                var node = queue.Dequeue();
                if (isFixed[node])
                {
                    continue;
                }
                var destinations = edges[node];
                isFixed[node] = true;
                v--;

                foreach(var destination in destinations)
                {
                    if(!isFixed[destination[0]] && costs[destination[0]] > costs[node] + destination[1])
                    {
                        costs[destination[0]] = costs[node] + destination[1];
                        queue.Enqueue(destination[0], costs[destination[0]]);
                    }
                }

                if(v == 0)
                {
                    break;
                }
            }

            foreach(var cost in costs)
            {
                WriteLine(cost != int.MaxValue ? $"{cost}" : "INF");
            }
        }

        private class PriorityQueue
        {
            private int[][] heap;


            public int Count { get; private set; } = 0;

            public PriorityQueue(int size)
            {
                this.heap = new int[size][];
            }

            public void Enqueue(int node, int distance)
            {
                if(this.Count == 0)
                {
                    heap[0] = new int[2] { node, distance };
                    this.Count++;
                    return;
                }

                var nodeNo = this.Count;

                while(nodeNo > 0)
                {
                    var parentNo = (nodeNo - 1) / 2;
                    if(distance >= heap[parentNo][1])
                    {
                        break;
                    }
                    heap[nodeNo] = heap[parentNo];
                    nodeNo = parentNo;
                }

                heap[nodeNo] = new int[2] { node, distance };
                this.Count++;
            }

            public int Dequeue()
            {
                this.Count--;

                var node = heap[0];
                var last = heap[this.Count];

                var parentNo = 0;
                while(parentNo * 2 + 1 < this.Count)
                {
                    var leftChild = parentNo * 2 + 1;
                    var rightChild = parentNo * 2 + 2;

                    if(rightChild < this.Count && heap[leftChild][1] > heap[rightChild][1])
                    {
                        leftChild = rightChild;
                    }

                    if(heap[leftChild][1] > last[1])
                    {
                        break;
                    }

                    heap[parentNo] = heap[leftChild];
                    parentNo = leftChild;
                }

                heap[parentNo] = last;

                return node[0];
            }
        }
    }
}