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

// ref: p302

using System;
using System.Collections.Generic;

public class Pair<K, V> {
	public K first;
	public V second;
	public Pair(K key,V value) {
		first = key;
		second = value;
	}
}

public class PriorityQueue<T>
	where T : Pair<int,int> {
	private List<T> heap = new List<T>();
	public int Count{
		get {
			return heap.Count; 
		}
	}

	// ????´?????????\
	public void Push(T x){
		heap.Add (x);
		int i = Count - 1;
		while (i > 0) {
			int p = (i - 1) / 2;
			if (Compare(x,heap [p]) <= 0) {
				break;
			}
			heap [i] = heap [p];
			i = p;
		}
		heap [i] = x;
	}
	// ?????????????´??????????
	public void Pop() {
		int largest = 0;
		heap.RemoveAt(0);
		int i = 0;
		while (i * 2 + 1 < Count) {
			int l = 2 * i;
			int r = 2 * i + 1;
			// ???????????§?????§????????????
			if (Compare (heap [l], heap [i]) >= 0) {
				largest = l;
			} else {
				largest = i;
			}
			// ???????????§?????§????????????
			if (r < Count && Compare (heap [r], heap[largest]) >= 0) {
				largest = r;
			}
			// ????????¨??????
			if (largest != i) {
				heap[i] = heap[largest];
			}
			i = largest;
		}
	}
	// ?????????????´??????¢?????????
	public T Top() {
		return heap[0];
	}

	public bool Empty() {
		if (Count == 0)
			return true;
		else
			return false;
	}
	int Compare(T x, T y) {
		if (x.first < y.first)
			return -1;
		if (x.first > y.first)
			return 1;
		else
			return 0;
	}
}

public class SingleSourceShortestPath2
{
	const int MAX = 10000;
	const int INFTY = Int32.MaxValue;
	const int WHITE = 0;
	const int GRAY = 1;
	const int BLACK = 2;

	static int vertexNum; // ????????° = V
	static List<Pair<int,int>>[] adj; // ??????????????????????¨????????????£??\?????????

	static void dijkstra() {
		PriorityQueue<Pair<int,int>> PQ = new PriorityQueue<Pair<int,int>>();
		int[] d = new int[MAX]; // ?§????s????????????v?????§??????????????????????¨????
		int[] color = new int[MAX]; // ?¨??????¶???????????§??¨???

		// ??????????????????u????????????color???WHITE????????????INFTY????????????
		for (int i = 0; i < vertexNum; i++) {
			d[i] = INFTY;
			color[i] = WHITE; 
		}

		// ????????????????????????
		d[0] = 0;
		color[0] = GRAY;
		PQ.Push(new Pair<int,int>(0,0));

		while (!PQ.Empty()) {
			// ?§??????????
			Pair<int,int> f = PQ.Top ();
			PQ.Pop ();
			int u = f.second;
			color[u] = BLACK;

			// ????°???????????????????????????????????????§???????????°??????
			if (d[u] < f.first * (-1)) {
				continue;
			}
			// 
			for (int j = 0; j < adj[u].Count; j++) {
				int v = adj[u][j].first;
				if (color[v] == BLACK) continue;
				if (d[v] > d[u] + adj[u][j].second) {
					d[v] = d[u] + adj[u][j].second;
					PQ.Push(new Pair<int,int> (d[v] * (-1), v));
					color[v] = GRAY;
				}
			}
		}
		// ????????¨?????????????????????
		for (int i = 0; i < vertexNum; i++) {
			Console.WriteLine ("{0} {1}", i, (d[i] == INFTY ? -1 : d[i]));
		}
	}
	public static void Main(String[] args) {
		
		// ????????°
		vertexNum = int.Parse(Console.ReadLine ());

		// ?????????????????????
		adj = new List<Pair<int,int>>[MAX];
		for (int i = 0; i < vertexNum; i++) {
			adj[i] = new List<Pair<int,int>> ();
		}

		int k, c, u, v;
		for (int i = 0; i < vertexNum; i++) {
			string[] readLine = Console.ReadLine().Split(' ');
			int s = 0;
			u = int.Parse(readLine[s]);	// ????????????
			s++;
			k = int.Parse(readLine[s]); 	// ????¬???°
			for (int j = 0; j < k; j++) {
				s++;
				v = int.Parse(readLine[s]);	// ??£??\?????????????????????
				s++;
				c = int.Parse(readLine[s]); // u??¨v????????????????????????
				Pair<int,int> node = new Pair<int,int>(v,c);
				adj[u].Add(node);
			}
		}
		dijkstra ();
	}
}