Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := makeGraph(n)
	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			c := getNextInt(scanner)
			g[u][v] = c
		}
	}

	ans := make([]int, n)
	im := make(IntMap, 1)
	im.increment(0)
	for len(im) < n {
		min := 1 << 30
		x := -1
		for u := range im {
			for v := 0; v < n; v++ {
				if im.get(v) != 0 {
					continue
				}
				if min > ans[u]+g[u][v] {
					min = ans[u] + g[u][v]
					x = v
				}
			}
		}
		im.increment(x)
		ans[x] = min
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i, ans[i]))
	}
	writer.Flush()
}

func makeGraph(n int) [][]int {
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = 1 << 30
		}
	}

	return g
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

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

static const int MAX =100;
static const int INFTY = (1<<21);
static const int black= 2;
static const int gray= 1;
static const int white= 0;

int n,M[MAX][MAX];

void d()
{
  int i,u,j,minv;
  int d[MAX],iro[MAX];

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

  d[0] = 0;
  iro[0] = gray;
  while(1)
    {
      minv = INFTY;
      u = -1;
      for(i=0;i<n;i++)
	{
	  if(minv > d[i] && iro[i] != black)
	    {
	      u = i;
	      minv = d[i];
	    }
	}
      if(u==-1) break;
      iro[u] = black;
      for(j=0;j<n;j++)
	{
	  if(iro[j] != black && M[u][j] != INFTY)
	    {
	      if( d[j] > d[u] + M[u][j])
		{
		  d[j] = d[u] + M[u][j];
		  iro[j] = gray;
		}
	    }
	}
    }

  for(int i =0;i < n;i++)
    {
      cout << i << " " << ( d[i] == INFTY ? -1 : d[i] ) << endl;
    }
}

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

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

  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#define F first
#define S second
using namespace std;
typedef pair<int,int>p;

vector<p>g[103];
int d[103];
main()
{
  int n;
  int u, k, v, c;
  
  scanf("%d", &n);
  for (int i = 0; i < n ; ++i) {
    scanf("%d %d", &u, &k);
    for(int j = 0; j < k ; ++j) {
      scanf("%d %d", &c, &v);
      g[u].push_back(p(c,v));
    }
    d[i] = 1 << 21;
  }
  
  priority_queue< p, vector<p>, greater<p> >p_que;
  p_que.push(p(0,0));
  d[0] = 0;
  while ( !p_que.empty() ) {
    p now = p_que.top(); p_que.pop();
    int pos = now.F;
    int cost = now.S;
    for ( int i = 0; i < g[pos].size() ; ++i) {
      int ncost = cost + g[pos][i].S;
      if ( ncost < d[g[pos][i].F] ) {
	d[g[pos][i].F] = ncost;
	p_que.push(p(g[pos][i].F,ncost));
      }
    }
  }

  for ( int i = 0; i < n ; ++i) {
    printf("%d %d\n", i, d[i]);
  }

  return (0);
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <stack>
#include <complex>
#include <stdlib.h>
#include <stdio.h>
#include <functional>
#include <cfloat>
#include <math.h>
#include <numeric>
#include <string.h>
#include <sys/time.h>
#include <random>


#define fs first
#define sc second

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<ll, ll> P;

ll squareDis(P x, P y){
    return (x.fs - y.fs) * (x.fs - y.fs) + (x.sc - y.sc) * (x.sc - y.sc);
}

bool judgeAcute(P x, P y, P z){
    ll dis1 = squareDis(x, y);
    ll dis2 = squareDis(x, z);
    ll dis3 = squareDis(y, z);

    return dis1 + dis2 > dis3;

}

class Graph{
    struct edge{
        int vertice;
        ll weight;
    };

    vector<vector<edge>> g;
    unsigned int n;

public:
    Graph(unsigned int n): n{n}{
        g.resize(n);
    }

    void add_edge(int u, int v){
        g[u].emplace_back(edge{v, 1});
    };

    void add_edge(int u, int v, ll w){
        g[u].emplace_back(edge{v, w});
    }

    vector<edge>& get_edge(int v){
        return g[v];
    }

    unsigned int get_num_vertices(){
        return n;
    }

    vector<edge>& operator[](int v){
        return get_edge(v);
    }
};


/**
 *
 * @param s 始点
 * @param g グラフ
 * @return sから各頂点への距離
 */
vector<ll> dijkstra1(int s, Graph g, ll INF=LONG_LONG_MAX / 3){
    unsigned int n = g.get_num_vertices();
    vector<bool> used(n, false);
    vector<ll> d(n, INF);

    d[s] = 0;
    while(true){
        int v = -1;
        for(int u = 0; u < n; u++) {
            if (!used[u] && (v == -1 || d[u] < d[v])) {
                v = u;
            }
        }

        if(v == -1) break;

        used[v] = true;
        for(auto e: g[v]){
            d[e.vertice] = min(d[e.vertice], d[v] + e.weight);
        }
    }

    return d;
}


vector<ll> dijkstra2(int s, Graph g, ll INF=LONG_LONG_MAX / 3){
    typedef pair<ll, int> Ptmp;  // firstは最短距離、secondは頂点の番号

    unsigned int n = g.get_num_vertices();
    priority_queue<Ptmp, vector<Ptmp>, greater<Ptmp>> que;
    vector<ll> d(n, INF);

    d[s] = 0;
    que.push(Ptmp(d[s], s));

    while(!que.empty()){
        Ptmp p = que.top();   que.pop();
        int v = p.second;
        if(d[v] < p.first) continue;
        for(auto e: g[v]){
            if(d[e.vertice] > d[v] + e.weight){
                d[e.vertice] = d[v] + e.weight;
                que.push(Ptmp(d[e.vertice], e.vertice));
            }
        }
    }

    return d;
}



int main(){
    uint n;  cin >> n;
    Graph g(n);

    for(int i = 0; i < n; i++){
        int u, k;   cin >> u >> k;
        for(int j = 0; j < k; j++){
            int v, c;   cin >> v >> c;
            g.add_edge(u, v, c);
        }
    }

    vector<ll> res = dijkstra2(0, g);
    for(int i = 0; i < n; i++){
        cout << i << " " << res[i] << endl;
    }
}


//int main(){
//    int n, m;   cin >> n >> m;
//
//    vector<vector<edge>> g(100);
//    vector<P> cor(n);
//
//    for(int i = 0; i < n; i++){
//        int x, y;   cin >> x >> y;
//        cor[i] = P(x, y);
//    }
//    for(int i = 0; i < m; i++){
//        int a, b, c;    cin >> a >> b >> c;
//        a--; b--;
//        g[a].emplace_back(edge{c, b});
//        g[b].emplace_back(edge{c, a});
//    }
//
//    vector<vector<edge>> g2;
//    map<P, int> mp;
//    g2.emplace_back(vector<edge>(0));
//    int n2 = 0;
//    mp[P(0, 0)] = n2++;
//
//    for(int i = 0; i < n; i++){
//        for(auto v: g[i]){
//            g2.emplace_back(vector<edge>(0));
//            mp[P(i, v.v)] = n2++;
//        }
//    }
//
//    for(int i = 0; i < n; i++){
//        for(auto v: g[i]){
//            for(auto u: g[i]){
//                if(!judgeAcute(cor[i], cor[u.v], cor[v.v])){
//                    g2[mp[P(i, v.v)]].emplace_back(edge{u.w, mp[P(u.v, i)]});
//                }
//            }
//        }
//    }
//
//
//
//    return 0;
//}