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.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;
        //    node.groupId = 1;

        //    while (true)
        //    {
        //        var selectedNodes = nodes.Where(n => n.groupId == 1);

        //        if (selectedNodes.Count() == nodes.Count)
        //        {
        //            break;
        //        }

        //        var edges = selectedNodes
        //            .SelectMany(n => n.edges).Where(n => !selectedNodes.Select(m => m.id).Contains(n.toId));

        //        foreach (var e in edges)
        //        {
        //            var fromNode = (Node)nodes.Where(n => n.id == e.fromId).FirstOrDefault();
        //            var toNode = (Node)nodes.Where(n => n.id == e.toId).FirstOrDefault();
        //            toNode.cost = Math.Min(toNode.cost, e.cost + fromNode.cost);
        //        }

        //        var selectedEdges = selectedNodes
        //            .SelectMany(n => n.edges).Where(n => !selectedNodes.Select(m => m.id).Contains(n.toId));

        //        if (selectedEdges.Count() != 0)
        //        {
        //            var edge = selectedEdges.OrderBy(n => n.cost).First();
        //            var nextNode = nodes.Where(n => edge.toId == n.id).FirstOrDefault();
        //            nextNode.groupId = 1;
        //        }
        //        else
        //        {
        //            break;
        //        }
        //    }

        //    #region output

        //    foreach (var n in nodes)
        //    {
        //        if (n.groupId == 1)
        //        {
        //            Console.WriteLine(n.cost);
        //        }
        //        else
        //        {
        //            Console.WriteLine("INF");
        //        }
        //    }

        //    #endregion output
        //}

        public static void dijkstra(List<Node> nodes, Node firstNode)
        {
            var node = firstNode;
            node.cost = 0;
            //node.groupId = 1;

            while (true)
            {
                //Complete Condition
                var unSelectedNodes = nodes.Where(n => n.groupId != 1);
                var nextNode = unSelectedNodes.OrderBy(n => n.cost).FirstOrDefault();

                if (nextNode == null)
                {
                    break;
                }

                nextNode.groupId = 1;

                var edges = nextNode.edges
                    .Where(n => n.fromId == nextNode.id && unSelectedNodes.Select(m => m.id).Contains(n.toId));

                if (edges == null)
                {
                    break;
                }

                foreach (var e in edges)
                {
                    var fromNode = (Node)nodes.Where(n => n.id == e.fromId).FirstOrDefault();
                    var toNode = (Node)nodes.Where(n => n.id == e.toId).FirstOrDefault();

                    toNode.cost = fromNode.cost == int.MaxValue ? toNode.cost : Math.Min(toNode.cost, e.cost + fromNode.cost);
                    toNode.groupId = 2;
                }
            }

            #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;
        }
    }
}