Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

var infty = 1000000000

type DirectedGraphNodeID int

type DirectedGraphNode struct {
	key         DirectedGraphNodeID
	adjacents   []*DirectedGraphNode
	costs       []int
	distance    int
	predecessor *DirectedGraphNode
}

func newDirectedGraph(nNodes int) *DirectedGraph {
	graph := DirectedGraph{}
	graph.nodes = make([]*DirectedGraphNode, nNodes)
	for i := 0; i < nNodes; i++ {
		node := DirectedGraphNode{}
		node.key = DirectedGraphNodeID(i)
		node.distance = infty
		graph.nodes[i] = &node
	}
	return &graph
}

type DirectedGraph struct {
	nodes []*DirectedGraphNode
}

func (g *DirectedGraph) setAdjacency(i, j DirectedGraphNodeID, cost int) {
	g.nodes[i].adjacents = append(g.nodes[i].adjacents, g.nodes[j])
	g.nodes[i].costs = append(g.nodes[i].costs, cost)
}

func (g *DirectedGraph) print() {
	for i := 0; i < len(g.nodes); i++ {
		node := g.nodes[i]
		key := node.key
		strings := []string{strconv.Itoa(int(key)), strconv.Itoa(node.distance)}
		for j := 0; j < len(node.adjacents); j++ {
			nodeJ := fmt.Sprintf("(%v, %v)", node.adjacents[j].key, node.costs[j])
			strings = append(strings, nodeJ)
		}
		fmt.Printf("%v\n", strings)
	}
}

func (g *DirectedGraph) relaxEdge(l, r *DirectedGraphNode, w int) {
	if r.distance > l.distance+w {
		// fmt.Printf("%v -> %v, %v\n", l.key, r.key, l.distance+w)
		r.distance = l.distance + w
		r.predecessor = l
	}
}

func main() {
	scanner.Split(bufio.ScanWords)

	nNodes := nextInt()
	graph := newDirectedGraph(nNodes)

	for i := 0; i < nNodes; i++ {
		iNode := DirectedGraphNodeID(nextInt())
		nAdjacent := nextInt()
		for j := 0; j < nAdjacent; j++ {
			jNode := DirectedGraphNodeID(nextInt())
			cost := nextInt()
			graph.setAdjacency(iNode, jNode, cost)
		}
	}

	graph.nodes[0].distance = 0
	for iNode := 0; iNode < nNodes; iNode++ {
		for iLeft := 0; iLeft < len(graph.nodes); iLeft++ {
			nodeLeft := graph.nodes[iLeft]
			for iRight := 0; iRight < len(nodeLeft.adjacents); iRight++ {
				nodeRight := nodeLeft.adjacents[iRight]
				cost := nodeLeft.costs[iRight]
				graph.relaxEdge(nodeLeft, nodeRight, cost)
			}
		}
	}

	// graph.print()

	for iNode := 0; iNode < len(graph.nodes); iNode++ {
		node := graph.nodes[iNode]
		fmt.Println(node.key, node.distance)
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
 
#define INF (1 << 30)
 
struct edge{
 
  int cost, to;
 
};
 
struct state{
 
  int node, cost;
  bool operator <(const state &b)const{
    return cost > b.cost;
  }
};
 
int n;
int d[10001];
vector<edge> glaph[10001];
priority_queue<state> qu;
 
void sss(){
 
  fill(d,d+10001,INF);
  qu.push((state){0,0});
  d[0] = 0;
 
  while(!qu.empty()){
 
    state info;
    info = qu.top();
    qu.pop();
 
    for(int i = 0; i < (double)glaph[info.node].size(); i++){
      if(d[glaph[info.node][i].to] > glaph[info.node][i].cost + d[info.node]){
      d[glaph[info.node][i].to] = glaph[info.node][i].cost + d[info.node];
      qu.push((state){glaph[info.node][i].to,d[info.node]}); 
      }
    }
  }
 
  for(int i = 0; i < n; i++){
    cout << i << " " << d[i] << endl;
  }
   
 
 
 
}
 
int main(){
 
  cin >> n;
 
  for(int i = 0; i < n; i++){
    int k, v, c, u;
    cin >> u >> k;
    for(int j = 0; j < k; j++){
      cin >> v >> c;
      glaph[u].push_back((edge){c,v});
    }
  }
 
  sss();
 
 
}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <cmath>
#include <algorithm>
#include <string>
#include <iomanip>
#include <cstdio>
#include <queue>

using namespace std;
using ll = long long;
using ull = unsigned long long;

constexpr ll LL_MAX = numeric_limits<ll>::max();
constexpr ull ULL_MAX = numeric_limits<ull>::max();
constexpr ll I_MAX = numeric_limits<int>::max();
constexpr ull UI_MAX = numeric_limits<unsigned int>::max();

#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a-1;i>=b;i--)

auto sumdig(ll num) {
	ll dig, sum;
	sum = 0;
	while (num) {
		dig = num % 10;
		sum = sum + dig;
		num = num / 10;
	}
	return sum;
}



struct edge {
	int from;
	int to;
	int cost=1;
};


auto dijkstra(vector<vector<edge>> &g, int start = 0) {
	vector<int> d(g.size(), I_MAX);
	vector<bool> flag(g.size(), false);
	d[start] = 0;
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int,int>>> nodes;//first dist, second edgenum
	nodes.push(pair<int, int>(0, start));


	while (!nodes.empty()) {
		pair<int, int> n = nodes.top();
		int s_node = n.second;
		nodes.pop();
		if (n.first > d[n.second])continue;
		rep(i, 0, g[s_node].size()) {
			int new_dist = d[s_node] + g[s_node][i].cost;
			int n_num = g[s_node][i].to;
			if (new_dist < d[n_num]) {
				nodes.push(make_pair(new_dist, n_num));
				d[n_num] = new_dist;
			}
		}
	}
	return d;
}

int main() {
	int n;
	cin >> n;
	vector<edge> empty;
	vector<vector<edge>> g(n, empty);
	vector<int> ans;
	
	rep(i, 0, n) {
		int temp;
		cin >> temp;//頂点番号はいらん
		cin >> temp;

		rep(j, 0, temp) {
			int tc, tt;
			cin >> tt >> tc;
			edge tn = { i,tt,tc };
			g[i].push_back(tn);
		}
	}

	ans = dijkstra(g, 0);
	
	rep(i, 0, ans.size()) {
		cout << i << " " << ans[i] << endl;
	}

	return 0;
}

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

static const int MAX = 100;
static const int INFTY = (1<<21);

int n, M[MAX][MAX];
int d[MAX];
int color[MAX];

void dijkstra(){
  int minv;

  for(int i = 0; i < n; i++){
    d[i] = INFTY;
    color[i] = 0;
  }

  d[0] = 0;
  color[0] = 1;
  while(1){
    minv = INFTY;
    int u = -1;
    for(int i = 0; i < n; i++){
      if(minv > d[i] && color[i] != 2){
        u = i;
        minv = d[i];
      }
    }

    if(u == -1){
      break;
    }
    color[u] = 2;

    for(int v = 0; v < n; v++){
      if(M[u][v] != INFTY && color[v] != 2){
        if(d[u] + M[u][v] < d[v]){
          d[v] = d[u] + M[u][v];
          color[v] = 1;
        }
      } 
    }
  }
}

int main(){
  cin >> n;
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      M[i][j] = INFTY;
    }
  }

  int u, k;
  for(int i = 0; i < n; i++){
    cin >> u >> k;
    for(int j = 0; j < k; j++){
      int v, c;
      cin >> v >> c;
      M[i][v] = c;
    }
  }

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

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define MOD 1000000007

typedef long long int ll;

//const ll INF=(ll)1e18;
const int INF=(1<<21);

int main(){
  int n;
  cin >> n;

  vector<pair<int, int>> e[n];
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int,int>>> pq;

  int d[n];
  bool visited[n];

  REP(i,n)d[i]=INF;
  REP(i,n)visited[i]=false;

  REP(i,n){
    int u,k;
    cin >> u >> k;

    REP(j,k){
      int v,c;
      cin >> v >> c;;
      e[i].push_back(make_pair(v,c));
    }
  }

  d[0]=0;
  visited[0]=true;

  pq.push(make_pair(0,0));
  while(!pq.empty()){
    int c=pq.top().first;
    int u=pq.top().second;
    pq.pop();

    visited[u] = true;
    if(d[u] < c)continue;

    REP(i,e[u].size()){
      int v = e[u][i].first;
      if(visited[v])continue;

      if(d[u] + e[u][i].second < d[v]){
        d[v] = d[u] + e[u][i].second;
        pq.push(make_pair(d[v], v));
      }
    }

  }

  REP(i,n){
    cout << i << " " << d[i] << endl;
  }


}