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.Linq;//リストの使用
using System.Collections.Generic;
class Program
{
static long n = long.Parse(Console.ReadLine());
static List<long[]>[] nextList = new List<long[]>[n];//頂点番号ごとの隣接リスト。距離、隣接頂点番号
static long[] colors = new long[n];//最短距離未確定の頂点が0、確定で2
static long[] distances = new long[n];//各頂点への最短距離
static long[] distanceMemos = new long[2];//キューから捨てた最短距離と頂点番号

	static void Main()
	{
    for(long i = 0; i < n; i++)
    {
      nextList[i] = new List<long[]>();
      long[] nums = Array.ConvertAll(Console.ReadLine().Split(' '),long.Parse);
      for(long j = 0; j < nums[1]; j++)
      {
        long[] nextMemo = new long[2];
        nextMemo[1] = nums[2*(j+1)];//隣接頂点番号
        nextMemo[0] = nums[2*(j+1)+1];//距離
        nextList[i].Add(nextMemo);//隣接リストの作成
      }
    }
    Dijkstra();
	}

  static void Dijkstra()//O((|頂点数|+|辺数|)log頂点数)で各頂点への最短距離を求める
  {
    List<long[]> distanceList = new List<long[]>();//優先度付きキュー
    for(long i = 0; i < n; i++)
    {
      distances[i] = long.MaxValue;//最短距離を無限とおく
    }

    long[] newDistance = new long[2];

    //スタート地点を決める
    newDistance[0] = 0;//スタート地点番号
    distances[0] = 0;//スタート距離0
    colors[0] = 1;//スタート色1
    
    newDistance[1] = 0;//スタートからスタートまでの距離0
    distanceList.Add(newDistance);

    while(distanceList.Count() != 0)
    {
      distanceList = PopHeap(distanceList);
      long minVertex = distanceMemos[1];//最短頂点の番号
      colors[minVertex] = 2;
      if(distances[minVertex] < distanceMemos[0]) continue;//最小値が最短でないなら無視

      for(long i = 0; i < nextList[minVertex].Count(); i++)
      {
        long v = nextList[minVertex][(int)i][1];
        if(colors[v] == 2) continue;
        if(distances[v] > distances[minVertex] + nextList[minVertex][(int)i][0])
        {
          distances[v] = distances[minVertex] + nextList[minVertex][(int)i][0];
          long[] newDistanceSub = new long[2];
          newDistanceSub[0] = distances[v];
          newDistanceSub[1] = v;
          distanceList = PushHeap(distanceList, newDistanceSub);
          colors[v] = 1;
        }
      }
    }

    for(long i = 0; i < n; i++)
    {
      Console.WriteLine(i + " " + (distances[i] == long.MaxValue? "-1" : distances[i].ToString()));
        //全頂点について最短距離の出力。到達不可能な頂点は-1を出力。
    }
    
  }

  //引数は、昇順にソート済みのリスト
  static List<long[]> PushHeap(List<long[]> nums, long[] addNums)//現在のヒープnumsにaddNumを追加
  {
    int n = nums.Count;
    nums.Add(addNums);

    while (n > 0)
    {
      int i = (n-1)/2;
      if (nums[n][0] < nums[i][0])//親のほうが大きいなら入れ替え。
      {
        long tmp = nums[n][0];
        long tmpSub = nums[n][1];
        nums[n][0] = nums[i][0];
        nums[n][1] = nums[i][1];
        nums[i][0] = tmp;
        nums[i][1] = tmpSub;
      }
      n = i;
    }
    return nums;
  }

  static List<long[]> PopHeap(List<long[]> nums)//ヒープnumsからポップ
  {
    int n = nums.Count - 1;
    distanceMemos[0] = nums[0][0];//捨てる値の保存
    distanceMemos[1] = nums[0][1];
    nums[0][0] = nums[n][0];//最小値である根を削除してリストの最後を持ってくる
    nums[0][1] = nums[n][1];
    nums.RemoveAt(n);

    for (int i = 0, j; (j = 2*i+1) < n; )
    {
      if ((j != n-1) && (nums[j][0] > nums[j+1][0]))// ２つの子のうち、値の小さい方を選ぶ
        j++;
      if (nums[i][0] > nums[j][0])//子が親より小さいなら入れ替え
      {
        long tmp = nums[j][0];
        long tmpSub = nums[j][1];
        nums[j][0] = nums[i][0];
        nums[j][1] = nums[i][1];
        nums[i][0] = tmp;
        nums[i][1] = tmpSub;
      }
      i = j;
    }
    return nums;
  }
  
}