Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

const NIL = -1

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	list := make([][]int, n)
	weight := make([]int, n)
	for i := range list {
		list[i] = make([]int, n)
		weight[i] = NIL
		for j := range list[i] {
			list[i][j] = NIL
		}
	}

	for i := 0; i < n; i++ {
		u := scanInt()
		k := scanInt()
		for j := 0; j < k; j++ {
			v := scanInt()
			w := scanInt()
			list[u][v] = w
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if weight[node.id] != NIL {
			continue
		}
		weight[node.id] = node.weight

		for i, w := range list[node.id] {
			if w == NIL {
				continue
			}
			heap.Push(h, Node{id: i, weight: w + node.weight})
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		fmt.Fprint(w, i)
		fmt.Fprint(w, " ")
		fmt.Fprint(w, weight[i])
		fmt.Fprintln(w)
	}
	w.Flush()
}

C++: #include<iostream>
#include<vector>
#define N 101
using namespace std;
int n;
vector<int> D(N,1<<30);
//bool visited[N];
vector<bool> visited(N,0);
vector<int> pre(N,0);
vector<vector<int>> M(N,vector<int>(N,0));
//int M[N][N];

void dijkstra(){
    D[0]=0;
    pre[0]=-1;

    while(1){
        int minD=1<<30;
        int u=-1;
        for(int i=0;i<n;i++){
            if(visited[i]) continue;
            if(D[i]<minD){
                u=i;
                minD=D[i];
            }
        }
        if(u==-1) break;

        visited[u]=1;
        for(int i=0;i<n;i++){
            if(visited[i]) continue;
            if(M[u][i]>0 && D[i]>D[u]+M[u][i]){
                D[i]=D[u]+M[u][i];
                pre[i]=u;
            }
        }

    }
}

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

int main(){
    init();
    /*for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cout<<M[i][j]<<' ';
        }
        cout<<endl;
    }*/
    dijkstra();
    for(int i=0;i<n;i++){
        cout<<i<<' '<<D[i]<<endl;
    }
    return 0;
}

/*
5
0 3 2 3 3 1 1 2
1 2 0 2 3 4
2 3 0 3 3 1 4 1
3 4 2 1 0 1 1 4 4 3
4 2 2 1 3 3
*/

C++: #include<stdio.h>
using namespace std;
const int maxn=1<<21;
int n;
int a[105][105];
void dan()
{
    int minx;
    int d[105],color[105];
    for(int i=0;i<n;i++)
    {
        d[i]=maxn;
        color[i]=0;
    }
    d[0]=0;
    while(1)
    {
        minx=maxn;
        int u=-1;
        for(int i=0;i<n;i++)
        {
            if(d[i]<minx&&color[i]!=1)
            {
                minx=d[i];
                u=i;
            }
        }
        if(u==-1) break;
        color[u]=1;
        for(int i=0;i<n;i++)
        {
            if(color[i]==1||a[u][i]==maxn) continue;
            if(d[i]>d[u]+a[u][i])
            {
                d[i]=d[u]+a[u][i];

            }
        }
    }
    for(int i=0;i<n;i++)
    {
        printf("%d ",i);
        printf("%d\n",(d[i]==maxn)?-1:d[i]);
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<105;i++)
    {
        for(int j=0;j<105;j++)
        {
            a[i][j]=maxn;
        }
    }
    int k,c,u,v;
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&k,&c);
        for(int j=0;j<c;j++)
        {
            scanf("%d%d",&u,&v);
            a[k][u]=v;
        }
    }
    dan();
    return 0;
}

C++: #include <iostream>
#include <queue>
#include <utility>
#include <vector>

struct Edge {
  int from, to, weight;
  Edge(int from, int to, int weight) : from(from), to(to), weight(weight) {}
};

struct Node {
  std::vector<Edge> ongoingEdges;
  void addEdge(Edge edge) {
    ongoingEdges.push_back(edge);
  }
};

class Graph {
private:
  std::vector<Node> nodes;
  int size;
public:
  Graph(int size): size(size) {
    nodes.resize(size);
  }
  void addEdge(Edge edge) {
    nodes[edge.from].addEdge(edge);
  }

  std::vector<int> calculateDist(int source) {
    std::vector<int> dist(size, (1 << 30));
    std::priority_queue< std::pair<int, int>, std::vector< std::pair<int, int> >, std::greater< std::pair<int, int> > > pq;

    pq.push(std::make_pair(0, source));
    dist[source] = 0;

    while(!pq.empty()) {
      int from = pq.top().second;
      pq.pop();

      for(Edge edge: nodes[from].ongoingEdges) {
        if(dist[edge.to] > dist[from] + edge.weight) {
          dist[edge.to] = dist[from] + edge.weight;
          pq.push({dist[edge.to], edge.to});
        }
      }

    }

    return dist;
  }
};

int main() {
  int n;
  std::cin >> n;
  Graph graph(n);

  for(int i = 0; i < n; i++) {
    int u, k;
    std::cin >> u >> k;

    for(int i = 0; i < k; i++) {
      int v, c;
      std::cin >> v >> c;
      graph.addEdge(Edge(u, v, c));
    }

  }

  std::vector<int> dist = graph.calculateDist(0);

  for(int i = 0; i < n; i++) {
    std::cout << i << ' ' << dist[i] << std::endl;
  }

}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (ll)(1E9+7)
#define PI 3.1415926535897932384

template <class T>ostream &operator<<(std::ostream &o, const vector<T> &v)
{
	rep(i, v.size()) {
		o << (i > 0 ? " " : "") << v[i];
	}
	return o;
}

#define MAX_V 100

struct edge {
	edge(int a, int b) {
		cost = a;
		to = b;
	}
	int cost;
	int to;
};

int n;
vector<vector<edge>> G;
vector<int> d;

void dijkstra(int s) {
	priority_queue<pii, vector<pii>, greater<pii>> que;
	d[s] = 0;
	que.push(pii(0, s));

	while (!que.empty()) {
		pii p = que.top();
		que.pop();
		int v = p.second;
		if (d[v] < p.first) continue;
		for (int i = 0; i < G[v].size(); i++) {
			edge e = G[v][i];
			if (d[e.to] > d[v] + e.cost) {
				d[e.to] = d[v] + e.cost;
				que.push(pii(d[e.to], e.to));
			}
		}
	}

}

int main() {
	int n,u,k,tmpp,tmpw;
	cin >> n;
	d = vector<int>(n, INF);
	G = vector<vector<edge>>(n);
	rep (i,n) {
		cin >> u >> k;
		rep(j, k) {
			cin >> tmpp>>tmpw;
			G[u].emplace_back(tmpw, tmpp);
		}
	}
	dijkstra(0);
	rep(i, n) {
		cout << i << " " << d[i] << endl;
	}
}