Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const BIG_COST = 200000

type Path struct {
	node int
	cost int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)

	// 各頂点につながる頂点一覧を得るための隣接リスト
	graph := make([][]Path, nNodes)

	for i := 0; i < nNodes; i++ {
		u := scanInt(sc)
		k := scanInt(sc)
		graph[i] = make([]Path, k)
		for d := 0; d < k; d++ {
			graph[u][d] = Path{
				node: scanInt(sc),
				cost: scanInt(sc),
			}
		}
	}

	// 0 から各頂点への最小コストを保存する配列
	minCosts := make([]int, nNodes)

	minCosts[0] = 0
	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	// 各頂点が調査済みかどうかを保持する配列
	checked := make([]bool, nNodes)

	// 最小コストのパスを高速に取得するためのヒープ。
	// コストが小さいものほどルートに来るようにする。
	heap := NewHeap(func(a, b Path) bool {
		return a.cost < b.cost
	})

	heap.insert(Path{node: 0, cost: 0})
	for heap.size > 0 {
		u := heap.extractMax()

		// 隣接行列を使っていた時とは違い、ヒープには
		// 同じノードへのパスが複数含まれうる。そのため既によりコストの低い
		// パスが見つかっている場合は無視する。
		if minCosts[u.node] < u.cost {
			continue
		}

		checked[u.node] = true

		for _, v := range graph[u.node] {
			cost := v.cost + u.cost
			if !checked[v.node] && cost < minCosts[v.node] {
				minCosts[v.node] = cost
				heap.insert(Path{node: v.node, cost: cost})
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i, cost := range minCosts {
		fmt.Fprintf(w, "%d %d\n", i, cost)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

// --- From ALDS1_9_C: Priority Queue

type Heap struct {
	size   int
	heap   []Path
	isHigh func(a, b Path) bool
}

func NewHeap(isHigh func(a, b Path) bool) Heap {
	heap := make([]Path, 1) // 先頭は不使用
	return Heap{size: 0, heap: heap, isHigh: isHigh}
}

func (h *Heap) insert(v Path) {
	h.size += 1
	if h.size > len(h.heap)-1 {
		h.heap = append(h.heap, v)
	} else {
		h.heap[h.size] = v
	}

	for i := h.size; i > 1; {
		parent := i / 2
		if h.isHigh(h.heap[parent], h.heap[i]) {
			break
		}
		h.heap[i], h.heap[parent] = h.heap[parent], h.heap[i]
		i = parent
	}
}

func (h *Heap) extractMax() Path {
	if h.size <= 0 {
		panic("cannot extract from empty heap")
	}

	max := h.heap[1]
	h.heap[1] = h.heap[h.size]
	h.size -= 1
	h.maxHeapify(1)

	return max
}

func (h *Heap) maxHeapify(idx int) {
	left := idx * 2
	right := left + 1
	largest := idx

	if left <= h.size {
		if h.isHigh(h.heap[left], h.heap[idx]) {
			largest = left
		}
		if right <= h.size && h.isHigh(h.heap[right], h.heap[largest]) {
			largest = right
		}
	}

	if largest != idx {
		h.heap[idx], h.heap[largest] = h.heap[largest], h.heap[idx]
		h.maxHeapify(largest)
	}
}

C++: #include <iostream>
#include <limits.h>
#include <vector>

using namespace std;

struct Node {
    int cost = INT_MAX;
    int color = 0;
    vector<int> next_node;
    vector<int> len_cost;
} node[100];

int main() {
    int n, id, k;

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> id >> k;
        node[id].next_node.resize(k);
        node[id].len_cost.resize(k);
        for (int j = 0; j < k; j++) {
            cin >> node[id].next_node[j] >> node[id].len_cost[j];
        }
    }

    int size1, size2;
    vector<int> vec;
    node[0].cost = 0;
    node[0].color = 1;
    vec.push_back(0);
    while (1) {
        size1 = vec.size();
        if (size1 == n) {
            break;
        }
        for (int i = 0; i < size1; i++) {
            size2 = node[vec[i]].next_node.size();
            for (int j = 0; j < size2; j++) {
                if (node[node[vec[i]].next_node[j]].cost > node[vec[i]].cost + node[vec[i]].len_cost[j]) {
                    node[node[vec[i]].next_node[j]].cost = node[vec[i]].cost + node[vec[i]].len_cost[j];
                }
            }
        }
        int min = INT_MAX, mini;
        for (int i = 0; i < size1; i++) {
            size2 = node[vec[i]].next_node.size();
            for (int j = 0; j < size2; j++) {
                if (min > node[node[vec[i]].next_node[j]].cost && node[node[vec[i]].next_node[j]].color == 0) {
                    min = node[node[vec[i]].next_node[j]].cost;
                    mini = node[vec[i]].next_node[j];
                }
            }
        }
        vec.push_back(mini);
        node[mini].color = 1;
    }

    for (int i = 0; i < n; i++) {
        cout << i << " " << node[i].cost << endl;
    }
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int inf = 1e9;
#define rep(i, n) for (int i = 0; i < n; i++)
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dxx[8] = {1, 1, 1, 0, 0, -1, -1, -1}, dyy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

int n;
vector<vector<pint>> g (10005);

void dijkstra() {
    int d[n];
    bool seen[n] = {};
    rep (i, n) d[i] = inf;
    d[0] = 0;
    priority_queue<pint, vector<pint>, greater<pint>> pq;
    pq.push ({0, 0});

    while (!pq.empty()) {
        pint f = pq.top();
        pq.pop();
        int u = f.second; // firstはコスト,secondは場所
        seen[u] = 1;

        if (d[u] < f.first) continue;
        // cout << u << " " << d[u] << "\n";
        rep (i, g[u].size()) {
            int v = g[u][i].second;
            if (!seen[v] && d[v] > d[u] + g[u][i].first) {
                d[v] = d[u] + g[u][i].first;
                pq.push ({d[v], v});
            }
        }
    }
    rep (i, n) cout << i << " " << d[i] << "\n";
}

int main() {
    cin >> n;
    rep (i, n) {
        int now, k, v, c;
        cin >> now >> k;
        rep (j, k) {
            cin >> v >> c;
            g[now].push_back ({c, v});
        }
    }
    dijkstra();
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <utility>
#include <queue>
#include <functional>
#define N 101
#define INF 2000000000
using namespace std;

int dist[N];
typedef pair<int,int> pii;

std::vector<pii> e1[N];
std::vector<pii> e2[N];

int dijk1(int a,int b){
	dist[a]=0;
	std::priority_queue<pii> pq;
	for(int i=0;i<e2[a].size();i++){
		pq.push(e2[a][i]);
	}
	while(!pq.empty()){
		pii temp=pq.top();
		int now=temp.second;
		int c=temp.first;
		pq.pop();
		if(dist[now]!=INF)continue;
		dist[now]=-c;
		if(now==b)break;
		for(int i=0;i<e2[now].size();i++){
			pq.push(make_pair(c+e2[now][i].first,e2[now][i].second));
		}
	}
	return dist[b];
}

int dijk(int a,int b){
	dist[a]=0;
	std::priority_queue<pii> pq;
	for(int i=0;i<e1[a].size();i++){
		pq.push(e1[a][i]);
	}
	while(!pq.empty()){
		pii temp=pq.top();
		int now=temp.second;
		int c=temp.first;
		pq.pop();
		if(dist[now]!=INF)continue;
		dist[now]=-c;
		if(now==b)break;
		for(int i=0;i<e1[now].size();i++){
			pq.push(make_pair(c+e1[now][i].first,e1[now][i].second));
		}
	}
	return dist[b];
}

int main(void){
	int a,b,c,d;
	int n,m;
	while(cin>>n>>m,n||m){
		for(int i=0;i<n;i++){
			cin>>a>>b>>c>>d;
			e1[a].push_back(make_pair(-c,b));
			e2[a].push_back(make_pair(-d,b));
		}
		int k;
		cin>>k;
		while(k--){
			for(int i=1;i<=m;i++)dist[i]=INF;
			cin>>a>>b>>c;
			if(c) cout<<dijk1(a,b)<<endl;
			else cout<<dijk(a,b)<<endl;
			// for(int i=1;i<=m;i++)printf("dist[%d]:%d\n",i,dist[i]);
		}
	}
	return 0;
}