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: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace AizuOnline
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var x = Console.ReadLine().Split(' ');
            var nodeNum = Convert.ToInt32(x[0]);
            var edgeNum = Convert.ToInt32(x[1]);
            var firstNodeId = Convert.ToInt32(x[2]);

            var nodes = createDirectionNodesByList2(nodeNum, edgeNum);
            var firstNode = nodes.Where(n => n.id == firstNodeId).FirstOrDefault();

            dijkstra(nodes, firstNode);

            Console.ReadLine();
        }

        public static List<Node> createDirectionNodesByList2(int nodeNum, int edgeNum)
        {
            var nodes = new List<Node>();

            for (int i = 0; i < nodeNum; i++)
            {
                var node = new Node()
                {
                    id = i
                };
                nodes.Add(node);
            }

            for (int i = 0; i < edgeNum; i++)
            {
                try
                {
                    var p = Console.ReadLine().Split(' ');
                    var fromId = Convert.ToInt32(p[0]);
                    var toId = Convert.ToInt32(p[1]);

                    var node = nodes.Where(n => n.id == fromId).FirstOrDefault();

                    node.edges.Add(new Edge()
                    {
                        fromId = fromId,
                        toId = toId,
                        cost = Convert.ToInt32(p[2]),
                    });
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            return nodes;
        }

        public static void dijkstra(List<Node> nodes, Node firstNode)
        {
            var node = firstNode;
            node.cost = 0;
            var sw = new Stopwatch();
            sw.Start();
            while (true)
            {
                var mincost = int.MaxValue;
                Node nextNode = null;
                foreach (var n in nodes)
                {
                    if (n.groupId != 1 && n.cost < mincost)
                    {
                        mincost = n.cost;
                        nextNode = n;
                    }
                }
                if (mincost == int.MaxValue)
                {
                    break;
                }
                if (nextNode != null)
                {
                    nextNode.groupId = 1;
                }

                foreach (var n in nodes)
                {
                    if (n.groupId != 1 && nextNode.edges.Select(m => m.toId).Contains(n.id))
                    {
                        var edge = nextNode.edges.Where(m => m.toId == n.id).FirstOrDefault();
                        n.cost = Math.Min(n.cost, edge.cost + nextNode.cost);
                        n.groupId = 2;
                    }
                }
            }

            //Console.WriteLine(sw.Elapsed);

            #region output

            foreach (var n in nodes)
            {
                Console.WriteLine(n.cost == int.MaxValue ? "INF" : n.cost.ToString());
            }

            #endregion output
        }

        public class Node
        {
            public int id;
            public bool isPassed = false;
            public int groupId;
            public int cost = int.MaxValue;
            public List<Edge> edges = new List<Edge>();
        }

        public class Edge
        {
            public int id = int.MinValue;
            public int fromId;
            public int toId;
            public int cost;
            public bool isPassed = false;
        }
    }
}