Go: package main

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

//make priority queue
type Item struct {
	id       int
	priority int // this state is priority and distans from start
	index    int
}

type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

// we want the ascending-priority-queue.
// So we use less than here.
func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = j
	pq[j].index = i
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 //for safety
	*pq = old[0 : n-1]
	return item
}

func (pq *PriorityQueue) update(dist int, id int) {
	for _, i := range *pq {
		if i.id == id {
			if i.priority > dist {
				i.priority = dist
			}
		}
	}
}

// the main code
func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())
	return n
}

func dikstra(dist [][]int, n int) []int {
	// preparate priority queue
	inf := 1 << 30
	pq := make(PriorityQueue, n)
	shortests := make([]int, n)
	for i := 0; i < n; i++ {
		pq[i] = &Item{
			id:       i,
			priority: inf,
			index:    i,
		}

		shortests[i] = inf
	}
	pq[0].priority = 0
	shortests[0] = 0
	heap.Init(&pq)

	for pq.Len() > 0 {
		item := heap.Pop(&pq).(*Item)
		shortests[item.id] = item.priority
		for id, d := range dist[item.id] {
			if d != -1 {
				pq.update(item.priority+d, id)
			}
		}
		heap.Init(&pq)
	}
	return shortests
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	// init
	n := nextInt(sc)
	dist := make([][]int, n)
	for i := 0; i < n; i++ {
		dist[i] = make([]int, n)
		for j := 0; j < n; j++ {
			dist[i][j] = -1
		}
	}

	// std Input
	for i := 0; i < n; i++ {
		u := nextInt(sc)
		k := nextInt(sc)
		for j := 0; j < k; j++ {
			v, c := nextInt(sc), nextInt(sc)
			dist[u][v] = c
		}
	}

	shortests := dikstra(dist, n)
	for i, d := range shortests {
		fmt.Printf("%d %d\n", i, d)
	}
}

C++: #define _USE_MATH_DEFINES
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<deque>
#include<queue>
#include<map>
#include<set>
#define ll long long
#define str string
#define ld long double
#define vec vector
#define vll vec<ll>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,b,a) for(ll i=b;i>=a;i--)
#define ALL(a) a.begin(),a.end()
#define rALL(a) a.rbegin(),a.rend()
#define kai "\n"
#define yuque priority_queue
#define ryuque(t) priority_queue<t,vec<t>,greater<t>>
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mie min_element
#define mae max_element
#define tos to_string
#define sep setprecision
#define MOD 1000000007ll
using namespace std;
void solve();
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed;
	solve();
}

//nCk(mod p)
const ll NUM=2;
ll fac[NUM],finv[NUM],inv[NUM];
void cmodf(ll p=MOD){
	fac[0]=fac[1]=1;
	finv[0]=finv[1]=1;
	inv[1]=1;
	rep(i,2,NUM){
		fac[i]=fac[i-1]*i%p;
		inv[i]=p-inv[p%i]*(p/i)%p;
		finv[i]=finv[i-1]*inv[i]%p;
	}
}
ll cmod(ll n,ll k,ll p=MOD){
	return fac[n]*(finv[k]*finv[n-k]%p)%p;
}

//最大公約数
ll gcd(ll n,ll m){
	ll k=n%m;
	while(k!=0){n=m;m=k;k=n%m;}
	return m;
}
ll gcd(vector<ll> a){
	rep(i,1,a.size()){
		ll k=a[i-1]%a[i];
		while(k!=0){
			a[i-1]=a[i];
			a[i]=k;
			k=a[i-1]%a[i];
		}
	}
	return a.back();
}

//最小公倍数
ll lcm(ll n,ll m){
	ll t=n*m,k=n%m;
	while(k!=0){n=m;m=k;k=n%m;}
	return t/m;
}
ll lcm(vector<ll> a){
	ll n;
	rep(i,1,a.size()){
		n=a[i-1]*a[i];
		ll k=a[i-1]%a[i];
		while(k!=0){
			a[i-1]=a[i];
			a[i]=k;
			k=a[i-1]%a[i];
		}
		n/=a[i];
		a[i]=n;
	}
	return n;
}

//keyの小さい方から探していった時に初めてvalueがjoken以上になる時のkey（keyが増えたらvalueも増えるか同じになるとき）
ll value(ll key) {
	/*変更*/
	return 0;
}
ll mink(ll lkey,ll rkey/*ここも調べる*/,ll joken){
	ll mkey;
	while(lkey!=rkey){
		mkey=(lkey+rkey)/2;
		if(value(mkey)>=joken)rkey=mkey;
		else lkey=mkey+1;
	}
	return rkey;
}

//ペア配列の整理
struct pea{
	ll f,s;
	bool operator<(const pea &c)const{
		return f!=c.f?f<c.f:s<c.s;
	};
};

//変数定義

//再帰関数

//アルゴリズム
void solve(){
	ll n;cin>>n;
	vec<vll> we(n,vll(n,MOD));
	vec<set<ll>> ch(n);
	vec<vll> pa(n);
	rep(i,0,n){
		ll u,k;cin>>u>>k;
		rep(j,0,k){
			ll v,c;cin>>v>>c;
			ch[u].insert(v);
			pa[v].pub(u);
			we[u][v]=c;
		}
	}
	vll a,ans(n);
	ans[0]=0;
	a.pub(0);
	rep(i,0,pa[0].size())ch[pa[0][i]].erase(0);
	while(a.size()!=n){
		ll m=MOD;ll jj;
		rep(i,0,a.size()){
			for(auto j=ch[a[i]].begin();j!=ch[a[i]].end();j++){
				if(ans[a[i]]+we[a[i]][*j]<m){
					m=ans[a[i]]+we[a[i]][*j];
					jj=*j;
				}
			}
		}
		ans[jj]=m;
		a.pub(jj);
		rep(i,0,pa[jj].size())ch[pa[jj][i]].erase(jj);
	}
	rep(i,0,n){
		cout<<i<<' '<<ans[i]<<kai;
	}
}

C++: // ALDS1_12_B.cpp
// Graph II - Single Source Shortest Path I
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;

struct Node {
    int v;
    int c;
    Node(int v, int c): v(v), c(c) {}
};

const int maxN = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int INF = 100001;
vector<Node> G[maxN];
vector<int> dist(maxN, INF);
vector<int> p(maxN);
vector<int> color(maxN, WHITE);
int n;
int cnt = 0;
void dijkstra(int x)
{
    dist[x] = 0;
    p[x] = -1;
    while(++cnt != n) {
        int u = 0;
        int mindist = INF;
        for (int i = 0; i < n; i++) {
            if (color[i] != BLACK && dist[i] < mindist) {
                mindist = dist[i];
                u = i;
            }
        }

        for (int i = 0; i < G[u].size(); i++) {
            if (color[u] == BLACK) continue;
            int v = G[u][i].v;
            int c = G[u][i].c;
            if (dist[u] + c < dist[v]) {
                dist[v] = dist[u] + c;
                p[v] = u;
                color[v] = GRAY;
            }
        }
        color[u] = BLACK;
    }
}

int main()
{
    cin >> n;
    int u, k;
    int v, c;
    for (int i = 0; i < n; i++) {
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            cin >> v >> c;
            G[i].push_back(Node(v, c));
        }
    }
    dijkstra(0);
    for (int i = 0; i < n; i++) {
        printf("%d %d\n", i, dist[i]);
    }
    return 0;
}

C++: #include<iostream>
#include<queue>
#define INF 100000000
using namespace std;
int main(){
  int n;cin>>n;
  int u,k,v,c;
  int mp[101][101],d[101];
  for(int i=0;i<=100;i++){
    d[i]=INF;
    for(int j=0;j<=100;j++)mp[i][j]=INF;
  }
    while(u!=n-1){
    cin>>u>>k;
    for(int i=0;i<k;i++){
      cin>>v>>c;
      mp[u][v]=c;
    }
  }
     
    int mn,s,t[101]={},cn=n-1;
    d[0]=0,t[0]=1;
    while(cn){
      mn=INF;
      for(int i=0;i<n;i++){
    if(t[i]==0){
      if(d[i]>d[s]+mp[s][i])d[i]=d[s]+mp[s][i];
      if(mn>d[i])mn=d[i],k=i;
    }
      }
      if(mn!=INF)cn--,t[k]=1;
    }
    for(int i=0;i<n;i++)cout<<i<<" "<<d[i]<<endl;
     
  return 0;
}