Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Single Source Shortest Path II

For a given weighted graph $G = (V, E)$, find the shortest path from a source to each vertex. For each vertex $u$, print the total weight of edges on the shortest path from vertex $0$ to $u$.

Input

In the first line, an integer $n$ denoting the number of vertices in $G$ is given. In the following $n$ lines, adjacency lists for each vertex $u$ are respectively given in the following format:

$u$ $k$ $v_1$ $c_1$ $v_2$ $c_2$ ... $v_k$ $c_k$

Vertices in $G$ are named with IDs $0, 1, ..., n-1$. $u$ is ID of the target vertex and $k$ denotes its degree. $v_i (i = 1, 2, ... k)$ denote IDs of vertices adjacent to $u$ and $c_i$ denotes the weight of a directed edge connecting $u$ and $v_i$ (from $u$ to $v_i$).

Output

For each vertex, print its ID and the distance separated by a space character in a line respectively. Print in order of vertex IDs.

Constraints

$1 \leq n \leq 10,000$

$0 \leq c_i \leq 100,000$

$|E| < 500,000$

All vertices are reachable from vertex $0$

Sample Input 1

5
0 3 2 3 3 1 1 2
1 2 0 2 3 4
2 3 0 3 3 1 4 1
3 4 2 1 0 1 1 4 4 3
4 2 2 1 3 3

Sample Output 1

0 0
1 2
2 2
3 1
4 3

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace ALDS1_12C_1{
    public class Program{
        public static void Main(string[] args){
            var sr = new StreamReader();
            //---------------------------------
            var N = sr.Next<int>();
            var dj = new Dijkstra(N);

            for(var i = 0; i < N; i++){
                var U = sr.Next<int>();
                var K = sr.Next<int>();

                for(var j = 0; j < K; j++){
                    var V = sr.Next<int>();
                    var C = sr.Next<int>();
                    dj.AddEdge(U, V, C);
                }
            }

            var sb = new StringBuilder();
            var dist = dj.Solve(0);
            for(var i = 0; i < N; i++){
                sb.AppendLine(i + " " + dist[i]);
            }
            Console.Write(sb);
            //---------------------------------
        }
    }

    public class Dijkstra{
        public class Edge{
            public int To;
            public int Cost;
        }

        public class Node : IComparable<Node>{
            public int Id;
            public int Cost;

            public int CompareTo(Node node){
                return Math.Sign(Cost - node.Cost);
            }
        }

        private readonly List<Edge>[] _adj;

        public Dijkstra(int v){
            _adj = new List<Edge>[v];
            for(var i = 0; i < v; i++) _adj[i] = new List<Edge>();
        }

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

        public int[] Solve(int start){
            var v = _adj.Length;
            var res = new int[v];

            for(var i = 0; i < v; i++) res[i] = int.MaxValue;
            res[start] = 0;

            var pq = new PriorityQueue<Node>();
            pq.Enqueue(new Node{Id = start, Cost = 0});

            while(pq.Count > 0){
                var node = pq.Dequeue();
                var from = node.Id;

                foreach(var edge in _adj[from]){
                    var to = edge.To;
                    var cost = node.Cost + edge.Cost;

                    if(cost >= res[to]) continue;
                    res[to] = cost;
                    pq.Enqueue(new Node{Id = to, Cost = cost});
                }
            }
            return res;
        }
    }

    public class PriorityQueue<T> : IEnumerable<T> where T : IComparable<T>{
        private readonly List<T> _list = new List<T>();
        public int Count{
            get { return _list.Count; }
        }

        IEnumerator<T> IEnumerable<T>.GetEnumerator(){
            return _list.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator(){
            return _list.GetEnumerator();
        }

        public void Clear(){
            _list.Clear();
        }

        public bool Contains(T value){
            return _list.Contains(value);
        }

        public T Peek(){
            return _list[0];
        }

        public void Enqueue(T value){
            _list.Add(value);
            PushHeap();
        }

        public T Dequeue(){
            var ret = _list[0];
            _list[0] = _list[Count - 1];
            _list.RemoveAt(Count - 1);
            PopHeap();
            return ret;
        }

        private void PushHeap(){
            var i = Count - 1;
            while(i != 0){
                var p = (i - 1) / 2;
                if(_list[i].CompareTo(_list[p]) > 0) return;

                SwapIndex(i, i = p);
            }
        }

        private void PopHeap(){
            var i = 0;
            while(true){
                var l = 2 * i + 1;
                var r = l + 1;

                var maxi = i;
                if(l < Count && _list[maxi].CompareTo(_list[l]) > 0) maxi = l;
                if(r < Count && _list[maxi].CompareTo(_list[r]) > 0) maxi = r;
                if(maxi == i) return;

                SwapIndex(i, i = maxi);
            }
        }

        private void SwapIndex(int left, int right){
            var tmp = _list[left];
            _list[left] = _list[right];
            _list[right] = tmp;
        }
    }

    public class StreamReader{
        private readonly char[] _c = {' '};
        private int _index = -1;
        private string[] _input = new string[0];
        private readonly System.IO.StreamReader _sr = new System.IO.StreamReader(Console.OpenStandardInput());

        public T Next<T>(){
            if(_index == _input.Length - 1){
                _index = -1;
                while(true){
                    string rl = _sr.ReadLine();
                    if(rl == null){
                        if(typeof(T).IsClass) return default(T);
                        return (T)typeof(T).GetField("MinValue").GetValue(null);
                    }
                    if(rl != ""){
                        _input = rl.Split(_c, StringSplitOptions.RemoveEmptyEntries);
                        break;
                    }
                }
            }
            return (T)Convert.ChangeType(_input[++_index], typeof(T), System.Globalization.CultureInfo.InvariantCulture);
        }

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

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