Go: package main

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

const (
	MAX_N      = 10000
	MAX_WEIGHT = 100000
)

type Edge struct {
	start  int
	end    int
	weight int
}

type ShortestInfo struct {
	edges    []Edge
	shortest int
}

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

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	shortestList := make([]ShortestInfo, n)
	for i := 1; i < n; i++ {
		shortestList[i] = ShortestInfo{edges: []Edge{}, shortest: MAX_N*MAX_WEIGHT + 1}
	}

	adjacentMatrix, err := createAdjacentMatrix(n, sc)
	if err != nil {
		panic(err)
	}

	sssp([]int{1}, shortestList, adjacentMatrix)
	for _, v := range shortestList {
		fmt.Printf("%d %d\n", v.getEnd()-1, v.shortest)
	}
}

func (si ShortestInfo) getEnd() int {
	if len(si.edges) == 0 {
		return 1
	} else {
		return si.edges[len(si.edges)-1].end
	}
}

func createAdjacentMatrix(n int, sc *bufio.Scanner) ([][]int, error) {
	adjacentMatrix := make([][]int, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			return nil, err
		}
		adjacentMatrix[id] = make([]int, n)
		for j := 0; j < n; j++ {
			adjacentMatrix[id][j] = -1
		}
		vertexNum, err := nextInt(sc)
		if err != nil {
			return nil, err
		}
		for j := 0; j < vertexNum; j++ {
			v, err := nextInt(sc)
			if err != nil {
				return nil, err
			}
			c, err := nextInt(sc)
			if err != nil {
				return nil, err
			}
			adjacentMatrix[id][v] = c
		}
	}
	return adjacentMatrix, nil
}

func sssp(nodeStack []int, shortestList []ShortestInfo, adjacentMatrix [][]int) {
	for len(nodeStack) > 0 {
		currentNode := nodeStack[0]
		nodeStack = nodeStack[1:]
		currentShortest := getShortest(currentNode, shortestList)
		adjacents := findAdjacents(currentNode, adjacentMatrix)
		for i := 0; i < len(adjacents); i++ {
			adj := adjacents[i]
			if currentShortest.shortest+adj.weight < getShortest(adj.end, shortestList).shortest {
				setShortest(adj.end, adj, currentShortest, shortestList)
				nodeStack = append(nodeStack, adj.end)
			}
		}
	}
}

func getShortest(node int, shortestList []ShortestInfo) ShortestInfo {
	return shortestList[node-1]
}

func setShortest(node int, edge Edge, currentShortest ShortestInfo, shortestList []ShortestInfo) {
	newEdges := make([]Edge, len(currentShortest.edges)+1)
	for i := 0; i < len(newEdges)-1; i++ {
		newEdges[i] = currentShortest.edges[i]
	}
	newEdges[len(newEdges)-1] = edge
	shortestInfo := ShortestInfo{edges: newEdges, shortest: currentShortest.shortest + edge.weight}
	shortestList[node-1] = shortestInfo
}

func findAdjacents(currentNode int, adjacentMatrix [][]int) []Edge {
	adjacents := adjacentMatrix[currentNode-1]
	edges := []Edge{}
	for i, v := range adjacents {
		if v > 0 {
			edges = append(edges, Edge{start: currentNode, end: i + 1, weight: v})
		}
	}
	return edges
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

const int INF = 1e8;

int n, m; // 頂点の数、辺の数

struct edge {int from, to, cost;}; // 辺の始点・終点・重み

vector<vector<int>> adj_matrix; // 隣接行列
vector<vector<edge>> adj_list; // 隣接リスト

vector<int> dist;
vector<bool> visited;

void dijkstra(int start) {
    dist.resize(n);
    visited.resize(n);
    rep(i, n) {
        if (i == start) dist[i] = 0;
        else dist[i] = INF;
        visited[i] = false;
    }
    priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
    pq.push({dist[start], start});
    while (not pq.empty()) {
        int cur = pq.top().second;
        pq.pop();
        visited[cur] = true;
        for (edge e : adj_list[cur]) {
            // int adj = e.to;
            if (visited[e.to] == false) {
                if (dist[e.to] > dist[e.from] + e.cost) {
                    dist[e.to] = dist[e.from] + e.cost;
                    pq.push({dist[e.to], e.to});
                }
            }
        }
    }
}

void solve_ALDS1_12_C() {
    cin >> n;
    adj_list.resize(n);
    rep(i, n) {
        int u, k;
        cin >> u;
        cin >> k;
        rep(j, k) {
            edge e;
            e.from = u;
            cin >> e.to;
            cin >> e.cost;
            adj_list[e.from].push_back(e);
        }
    }
    dijkstra(0);
    rep(i, n) {
        cout << i << " " << dist[i] << endl;
    }
}

int main() {
    // cin >> n;
    // cin >> m;
    // adj_matrix.resize(n, vector<int>(n, INF));
    // rep(i, n) {
    //     adj_matrix[i][i] = 0;
    // }
    // adj_list.resize(n);
    // rep(i, m) {
    //     edge e;
    //     cin >> e.from;
    //     cin >> e.to;
    //     cin >> e.cost;
    //     adj_matrix[e.from][e.to] = e.cost;
    //     adj_list[e.from].push_back(e);
    // }
    solve_ALDS1_12_C();
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define INF INT_MAX/3
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

#define MAX_N 10000
int G[MAX_N][MAX_N];
int MIN_COSTS[MAX_N];

int main() {
  int n;
  cin >> n;
  REP(i, n) {
    REP(j, n) {
      G[i][j] = INT_MAX;
    }
    MIN_COSTS[i] = INT_MAX;
  }
  int u, k;
  int v, c;
  REP(i, n) {
    cin >> u >> k;
    REP(j, k) {
      cin >> v >> c;
      G[u][v] = c;
    }
  }

  priority_queue<P> q;
  int position;
  int new_cost;
  q.push(P(0, 0));
  while (q.size() != 0){
    position = q.top().first;
    MIN_COSTS[position] = q.top().second;
    q.pop();
    REP(i, n) {
      if (G[position][i] != INT_MAX) {
        new_cost = MIN_COSTS[position] + G[position][i];
        if (new_cost < MIN_COSTS[i]) {
          MIN_COSTS[i] = new_cost;
          q.push(P(i, new_cost));
        }
      }
    }
  }

  REP(i, n) {
    cout << i << ' ' << MIN_COSTS[i] << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define N 10001
#define INF 1500001 
int n;

vector<pair<int, int>> A[N];
int color[N];
int d[N];

void dijkstra(int root) {
    priority_queue<pair<int, int>> Q;
    Q.push(make_pair(0, root));

    int treeSize = 1;
    while(!Q.empty()) {
        pair<int, int> addedInfo = Q.top();
        Q.pop();
        int added = addedInfo.second;

        if (color[added] != 1) {
            d[added] = -1 * addedInfo.first;
            color[added] = 1;

            for (vector<pair<int, int>>::iterator it = A[added].begin(); it != A[added].end(); it++) {
                pair<int, int> node_pair = *it;
                if (color[node_pair.first] == 0 && (d[added] + node_pair.second) < d[node_pair.first]) {
                    d[node_pair.first] = d[added] + node_pair.second;
                    Q.push(make_pair(-1 * d[node_pair.first], node_pair.first));
                }
            }
        }
    }
}

int main() {
    cin >> n;

    int w;
    int node;
    int k;
    int ad;
    for (int i = 0; i < n; i++) {
        color[i] = 0;
        d[i] = INF;

        cin >> node >> k;
        for (int j = 0; j < k; j++) {
            cin >> ad >> w;
            A[node].push_back(make_pair(ad, w));
        }
    }

    dijkstra(0);

    for (int i = 0; i < n; i++) {
        cout << i << " " << d[i] << endl;
    }

    return 0;
}