Go: package main

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

const (
	MAX_N      = 100
	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++: //
// Created by tattsun on 2017/02/16.
//

#include <iostream>

#define MAX_N 101
#define NIL -1
#define INFTY 1000000

using namespace std;

enum Status {
    NotVisited,
    Visited,
};

int n;
int w[MAX_N][MAX_N];
Status st[MAX_N];

int min(int x, int y) {
    if(x < y) return x;
    return y;
}

int dfs(int v, int cost, int target) {
    if(v == target) {
        return cost;
    }

    int c = MAX_N * INFTY;

    st[v] = Visited;

    for(int i=0; i<n; i++) {
        int x = w[v][i];
        if(x == INFTY || st[i] == Visited) continue;

        c = min(c, dfs(i, cost+x, target));
    }

    st[v] = NotVisited;

    return c;
}

int solve(int i) {
    return dfs(0, 0, i);
}

int main() {
    for(int i=0; i<MAX_N; i++) {
        st[i] = NotVisited;
        for (int j = 0; j < MAX_N; j++) {
            w[i][j] = INFTY;
        }
    }

    cin >> n;

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

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

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int INF = 1 << 30;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 100;

vector<pii> G[N];
int d[N];

int main() {
	int n; cin >> n;
	rep(i, n) {
		int _, n2; cin >> _ >> n2;
		rep(j, n2) {
			int v, c; cin >> v >> c;
			G[i].push_back(pii(c, v));
		}
	}
	rep(i, n) d[i] = INF;

	priority_queue<pii, vector<pii>, greater<pii>> Q;
	d[0] = 0;
	Q.push(pii(0, 0));
	while (!Q.empty()) {
		pii p = Q.top(); Q.pop();
		for (auto i : G[p.second]) {
			if (d[i.second] <= d[p.second] + i.first) continue;
			d[i.second] = d[p.second] + i.first;
			Q.push(i);
		}
	}
	rep(i, n) cout << i << ' ' << d[i] << endl;
}

C++: #include<iostream>
using namespace std;
static const int MAX=100;
static const int INF=(1<<21);
static const int APE=0;
static const int MID=1;
static const int BOT=2;

int n, M[MAX][MAX];

void dijkstra(){
  int mv, i, u, v;
  int d[MAX], c[MAX];

  for(i=0;i<n;i++){
    d[i]=INF;
    c[i]=APE;
  }

  d[0]=0;
  c[0]=MID;
  while(1){
    mv=INF;
    u=-1;
    for(i=0;i<n;i++){
      if(mv>d[i]&&c[i]!=BOT){
        u=i;
        mv=d[i];
      }
    }
    if(u==-1)break;
    c[u]=BOT;
    for(v=0;v<n;v++){
      if(c[v]!=BOT&&M[u][v]!=INF){
        if(d[v]>d[u]+M[u][v]){
          d[v]=d[u]+M[u][v];
          c[v]=MID;
        }
      }
    }
  }
  for(i=0;i<n;i++){
    cout<<i<<" "<<(d[i]==INF ? -1 : d[i])<<endl;
  }
}

int main(){
  int i, j, k, c, u, v;

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

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

  return 0;
}

C++: #include <bits/stdc++.h>
#include <string.h>
#include <cmath>
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define max3(A,B,C) max((A),max((B),(C)))
#define popcount(X) __builtin_popcount(X)
#define popcountl(X) __builtin_popcountl(X)
#define getbit(x,i) (1&((x)>>(i)))
#define setbit(x,i,bit) ((x)|=((bit)<<(i)))
#define getbitl(x,i) (1&((x)>>((ll)i)))
#define setbitl(x,i,bit) ((x)|=((bit)<<((ll)i)))
#define log2(x) (32 - __builtin_clz(x))
#define log2l(x) (32 - __builtin_clzl(x))

using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;

const int INF = 0x3f3f3f3f;
const long long int INFL = 0x3f3f3f3f3f3f3f3f;
const double pi = atan(1.0)*4.0;
const int N = 3e5 + 500;

int dist[N], mrc[N];
vii v[N];
set<ii> st;

void Dijkstra(int o, int n) {
	memset(dist,INF,sizeof(dist));
	st.clear();
	st.insert(mp(0,o));
	dist[o] = 0;
	for(int i = 0; i < n; i++) if(i != o) st.insert(mp(INF,i));
	memset(mrc,0,sizeof(mrc));
	while(!st.empty()) {
		set<ii>::iterator it = st.begin();
		if(mrc[it->s] == 0) {
			mrc[it->s] = 1;
			for(int i = 0; i < v[it->s].size(); i++) {
				if(dist[it->s] + v[it->s][i].s < dist[v[it->s][i].f]) {
					st.erase(mp(dist[v[it->s][i].f], v[it->s][i].f));
					dist[v[it->s][i].f] = dist[it->s] + v[it->s][i].s;
					st.insert(mp(dist[v[it->s][i].f], v[it->s][i].f));
				}
			}
		}
		st.erase(it);
	}
}


int main(void) {
	int n;
	
	scanf("%d", &n);
	
	for(int i = 0; i < n; i++) {
		int u, k;
		scanf("%d %d", &u, &k);
		for(int j = 0; j < k; j++) {
			int vt, c;
			scanf("%d %d", &vt, &c);
			v[u].pb(mp(vt,c));
		}
	}
	Dijkstra(0,n);
	for(int i = 0; i < n; i++) {
		printf("%d %d\n", i, dist[i]);
	}
	
	return 0;
}