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.Collections.Generic;

namespace GPL1A_3
{
    public class Program
    {
        void Solve(StreamScanner ss, StreamWriter sw)
        {
            //---------------------------------
            var V = ss.Next(int.Parse);
            var E = ss.Next(int.Parse);
            var R = ss.Next(int.Parse);

            var dj = new SPFA(V);
            for (var i = 0; i < E; i++)
            {
                var S = ss.Next(int.Parse);
                var T = ss.Next(int.Parse);
                var D = ss.Next(long.Parse);
                dj.AddEdge(S, T, D);
            }

            foreach (var dist in dj.GetDistances(R))
            {
                sw.WriteLine(dist == SP.Infinity ? "INF" : dist.ToString());
            }
            //---------------------------------
        }

        static void Main()
        {
            var ss = new StreamScanner(new StreamReader(Console.OpenStandardInput()));
            var sw = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false};
            new Program().Solve(ss, sw);
            sw.Flush();
        }

        static readonly Func<string, string> String = s => s;
    }

    public abstract class SP
    {
        public static readonly long Infinity = long.MaxValue;
        public static readonly long[] NegativeCycle = null;

        protected struct Edge
        {
            public int To;
            public long Cost;
        }

        protected int V { get; }
        protected List<Edge>[] Edges { get; }

        protected SP(int v)
        {
            V = v;
            Edges = new List<Edge>[v];
            for (var i = 0; i < v; i++) Edges[i] = new List<Edge>();
        }

        public void AddEdge(int from, int to, long cost)
        {
            Edges[from].Add(new Edge {To = to, Cost = cost});
        }

        public void AddUndirectedEdge(int v1, int v2, long cost)
        {
            AddEdge(v1, v2, cost);
            AddEdge(v2, v1, cost);
        }

        public long[] GetDistances(int start)
        {
            var dist = new long[V];
            for (var i = 0; i < V; i++) dist[i] = Infinity;
            dist[start] = 0;
            dist = GetDistancesCore(start, dist);
            return dist;
        }

        protected abstract long[] GetDistancesCore(int start, long[] dist);
    }

    public class SPFA : SP
    {
        class SPFAQueue
        {
            readonly int v;
            readonly Queue<int> que;
            readonly bool[] isIns;
            readonly int[] pushCounts;

            public int Count { get { return que.Count; } }

            public SPFAQueue(int v)
            {
                this.v = v;
                que = new Queue<int>();
                isIns = new bool[v];
                pushCounts = new int[v];
            }

            public bool Enqueue(int n)
            {
                if (isIns[n]) return true;
                isIns[n] = true;
                que.Enqueue(n);
                return (++pushCounts[n]) < v;
            }

            public int Dequeue()
            {
                var ret = que.Dequeue();
                isIns[ret] = false;
                return ret;
            }
        }

        public SPFA(int v) : base(v) { }

        protected override long[] GetDistancesCore(int start, long[] dist)
        {
            var que = new SPFAQueue(V);
            que.Enqueue(start);

            while (que.Count > 0)
            {
                var from = que.Dequeue();
                foreach (var edge in Edges[from])
                {
                    if (dist[from] == Infinity || dist[edge.To] <= dist[from] + edge.Cost) continue;
                    dist[edge.To] = dist[from] + edge.Cost;

                    if (que.Enqueue(edge.To)) continue;
                    return NegativeCycle;
                }
            }

            return dist;
        }
    }

    public class StreamScanner
    {
        static readonly char[] Sep = {' '};
        readonly Queue<string> buffer = new Queue<string>();
        readonly TextReader textReader;

        public StreamScanner(TextReader textReader)
        {
            this.textReader = textReader;
        }

        public T Next<T>(Func<string, T> parser)
        {
            if (buffer.Count != 0) return parser(buffer.Dequeue());
            var nextStrings = textReader.ReadLine().Split(Sep, StringSplitOptions.RemoveEmptyEntries);
            foreach (var nextString in nextStrings) buffer.Enqueue(nextString);
            return Next(parser);
        }

        public T[] Next<T>(Func<string, T> parser, int x)
        {
            var ret = new T[x];
            for (var i = 0; i < x; ++i) ret[i] = Next(parser);
            return ret;
        }

        public T[][] Next<T>(Func<string, T> parser, int x, int y)
        {
            var ret = new T[y][];
            for (var i = 0; i < y; ++i) ret[i] = Next(parser, x);
            return ret;
        }
    }
}