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++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <queue>
#include <functional>
using namespace std;
const int INF = 2000000000;
const int num = 10000;
int n, k, u, v, w;
int c[num];
int d[num];
vector <pair<int, int> > ve[num];
int main() {
	priority_queue <pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
	fill(d, d + num, INF);
	d[0] = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v >> w;
			ve[u].push_back(make_pair(v, w));
		}
	}
	fill(c, c + num, 0);
	q.push(make_pair(0, 0));
	while (!q.empty()) {
		int f = q.top().first;
		int s = q.top().second;
		q.pop();
		c[s] = 1;
		if (d[s] < f) continue;
		for (int i = 0; i < ve[s].size(); i++) {
			int fi = ve[s][i].first;
			int se = ve[s][i].second;
			if (c[fi]) continue;
			if (d[fi] > d[s] + se) {
				d[fi] = d[s] + se;
				q.push(make_pair(d[fi], fi));
			}
		}
	}
	for (int i = 0; i < n; i++) cout << i << " " << d[i] << endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
static const int MAX = 10000; 
int n;
vector<pair<int, int>> adj[MAX];
void dij(){
	priority_queue<pair<int,int>> que;
	int d[MAX],color[MAX];
	for(int i = 0;i < n;i++){
		d[i] = INT_MAX;
		color[i] = 0;
	}
	d[0] = 0;
	que.push(make_pair(0,0));
	color[0] = 1;
	while(!que.empty()){
		pair<int,int> f = que.top();
		que.pop();
		int u = f.second;
		color[u] = 2;
		if(d[u] < f.first * (-1))continue;
		for(int j = 0;j <  adj[u].size();j++){
			int v = adj[u][j].first;
			if(color[v] == 2)continue;
			if(d[v] > d[u] + adj[u][j].second){
				d[v] = d[u] + adj[u][j].second;
				que.push(make_pair(d[v]*(-1),v));
				color[v] = 1;
			}
		}
	}
	for(int i = 0;i < n;i++){
		cout << i << " " << (d[i] == INT_MAX ? -1 : d[i]) << endl;
	}
}
int main(){
	cin >> n;
	int k, c, u,v;
	for(int i = 0;i < n;i++){
		cin >> u >> k;
		for(int j = 0;j < k;j++){
			cin >> v >> c;
			adj[u].push_back(make_pair(v,c));
		}
	}
	dij();
}

C++: #define RangeFor(i,n) for(int i=0; i<n; ++i)
#define RvRngFor(i,n) for(int i=n-1; i>=0; --i)

#include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <numeric>

using std::vector;
using std::cin;
using std::priority_queue;
using std::pair;
using std::make_pair;


vector<int> sssp(vector<vector<pair<int, int> > > c, int start_node = 0){
    int n = c.size();
    priority_queue<pair<int, int>, vector<pair<int,int> >, std::greater<pair<int, int>> > pq;
    vector<int> temp_dist(n, 100000000);
    pq.push(make_pair(0, start_node));

    for(;!pq.empty();pq.pop()){
        auto t = pq.top();
        auto d = t.first;
        auto u = t.second;


        if(d < temp_dist[u]){
            temp_dist[u] = d;
            auto k = c[u].size();
            RangeFor(j, k) {
                pq.push(make_pair(d + c[u][j].second, c[u][j].first));
            }
        }
    }

    return temp_dist;
}

int main(){
    int n; cin >> n;
    vector<vector< pair<int,int> > > c(n);
    RangeFor(i, n) {
        int u;
        cin >> u;
        int k;
        cin >> k;
        c[u] = vector<pair<int, int> >(k);
        RangeFor(j, k) {
            int v;
            cin >> v;
            int d;
            cin >> d;
            c[u][j] = make_pair(v, d);
        }
    }

//    RangeFor(i, n){
//        RangeFor(j, c[i].size()){
//            std::cout << c[i][j].first << ' ' << c[i][j].second;
//        }
//        std::cout << std::endl;
//    }
    
    auto res = sssp(c);
    RangeFor(i, n){
        std::cout << i << ' ' << res[i] << std::endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;

#define MAX 1001
#define INF 5000001
#define NIL -1

int a[MAX][MAX];
int n;
int d[MAX];
int pi[MAX];

void Relax(int u, int v){
  if(d[v]> d[u]+a[u][v]){
    d[v]=d[u]+a[u][v];
    pi[v]=u;
  }
}

void ISS(int s){
  int v;
  for(v=0; v<n; v++){
    d[v]=INF;
    pi[v]=NIL;
  }
  d[s]=0;
}

void Dijkstra(int s){
  int i, v1, v, minv, u; 
  int flag[MAX];
  ISS(s);
  
  for(i=0; i<n; i++) flag[i]=0;

  for(v1=0; v1<n; v1++){
    
    minv = INF;
    // decide u
    for ( i = 0; i < n; i++ ){
      if(flag[i]==1) continue; 
      if(d[i]<=minv){
    u=i;
    minv=d[i];
      }
    }
    
    for(v=0; v<n; v++){
      if(a[u][v]==-1) continue;
      if(flag[v]==1) continue;
      /* if(a[u][v]<d[v]){
    pi[v]=u;
    d[v]=a[u][v];
    }*/
      Relax(u, v);
    }
    flag[u]=1;
  }
}

int main(){

  int i, j ,u ,k,v,c;
  // int a[MAX][MAX];
  cin>>n;

  for(i=0; i<n; i++){
    for(j=0; j<n; j++){
      a[i][j]=-1;
    }
  }

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

  /*for(i=0; i<n; i++){
    for(j=0; j<n; j++){
    cout<<a[i][j]<<endl;
    }
    cout<<endl;
    }*/
  Dijkstra(0);
  for(i=0; i<n; i++){
      cout<<i<<" "<<d[i]<<endl;
  }

  return 0;
}