Go: package main

import (
	"strconv"
	"fmt"
	"bufio"
	"os"
)
const INF = 1 << 21
var scanner = bufio.NewScanner(os.Stdin)

func nextInt() int {
	scanner.Scan()
	i, e := strconv.Atoi(scanner.Text())
	if e != nil {
		panic(e)
	}
	return i
}


func main() {
	scanner.Split(bufio.ScanWords)
	n := nextInt()
	adj := make([][]int, n)
	for i := range adj {
		adj[i] = make([]int, n)
	}

	for i := 0;i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			v := nextInt()
			c := nextInt()
			adj[u][v] = c
		}
	}

	// for i := range adj {
	// 	fmt.Println(adj[i])
	// }

	dist := make([]int, n)
	prev := make([]int, n)
	visited := make([]bool, n)
	dijkstra := func(start int, size int) {
		for i := 0; i < size; i++ {
			dist[i] = INF
			prev[i] = -1
			visited[i] = false
		}
		dist[start] = 0
		for {
			u := -1
			tmp_min := INF
			for i := 0; i < size; i++ {
				if visited[i] == false && dist[i] < tmp_min {
					tmp_min = dist[i]
					u = i
				}
			}
			if u == -1 {
				break
			}
			visited[u] = true
			for i := 0; i < size; i++ {
				if adj[u][i] > 0 {
					w := adj[u][i]
					newlen := dist[u] + w
					if newlen < dist[i] {
						dist[i] = newlen
						prev[i] = u
					}
				}
			}
		}
	}
	dijkstra(0, n)
	for i, cost := range dist {
		fmt.Printf("%d %d\n", i, cost)
	}
}

C++: #include <bits/stdc++.h>
static const int MAX = 100;
static const int INFTY = 10000000;
int V;
std::vector<std::pair<int, int> > G[MAX];

void dijkstra() {
    std::priority_queue<std::pair<int, int> > pq;
    bool visited[MAX];
    int d[MAX];
    for (int i = 0; i < V; i++) {
        visited[i] = false;
        d[i] = INFTY;
    }
    d[0] = 0;
    pq.push({0, 0}); //dist from target * -1, current node
    visited[0] = true;

    while ( !pq.empty() ) {
        std::pair<int, int> f = pq.top(); pq.pop();
        int current = f.second;
        visited[current] = true;

        if (d[current] < f.first * (-1)) continue;

        for (unsigned long i = 0; i < G[current].size(); i++) {
            int target = G[current][i].first;
            if (visited[target]) continue;
            if (d[target] > d[current] + G[current][i].second) {
                d[target] = d[current] + G[current][i].second;
                pq.push({d[target] * (-1), target});
            }
        }
    }

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



int main()
{
    int source, target, cost, edges;
    std::scanf("%d", &V);
    for (int i = 0; i < V; i++) {
        std::scanf("%d %d", &source, &edges);
        for (int j = 0; j < edges; j++) {
            std::scanf("%d %d", &target, &cost);
            G[source].push_back({target, cost});
        }
    }
    dijkstra();
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
template<class T> using V = vector<T>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1;
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }


void Dijkstra(int s, const vector<vector<P>> &g, vector<int> &dis) {
  priority_queue<P, vector<P>, greater<P>> que; // pair = (cost, v);
  dis[s] = 0;
  que.emplace(0,s);
  while(!que.empty()) {
    P p = que.top(); que.pop();
    int now = p.second;
    if(dis[now] < p.first) continue;
    for(P np : g[now]) {
      int next = np.second;
      if(dis[next] > dis[now] + np.first) {
        dis[next] = dis[now] + np.first;
        que.emplace(dis[next], next);
      }
    }
  }
}


int main(){
    
    int n;
    cin >> n;
    V<V<P>> g(n); // pair = (cost, to);
    rep(i,n) {
        int a, k;
        cin >> a >> k;
        rep(j,k) {
            int b, v;
            cin >> b >> v;
            g[a].emplace_back(v,b);
        }
    }

    V<int> d(n, inf);
    Dijkstra(0,g,d);
    rep(i,n) {
        printf("%d %d\n", i, d[i]);
    }
}

C++: #include <bits/stdc++.h>
#define ll long long int
#define P pair<int,int>
struct edge{int to,cost;};
using namespace std;


class CompareDist
{
public:
    bool operator()(pair<int,int> n1,pair<int,int> n2) {
        return n1.second>n2.second;
    }
};

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

void dijkstra(int s){
    priority_queue<P,vector<P>,CompareDist> q;
    //fill(d,d+V,INT_MAX);
    d=vector<int>(V,INT_MAX);
    d[s]=0;
    q.push(P(0,s));
    
    while(!q.empty()){
        P p=q.top();q.pop();
        int v=p.first;
        if(d[v]<p.second)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;
                q.push(P(e.to,d[e.to]));
            }
        }
    }
}
int main(void){
    int n,in,t,c;
    cin>>n;
    V=n;
    G.resize(n);
    d.resize(n);
    for(int i=0;i<n;i++){
        cin>>in;
        int node=in;
        cin>>in;
        for(int j=0;j<in;j++){
            cin>>t>>c;
            G[node].emplace_back((edge){t,c});
        }
        
    }
    dijkstra(0);
    for(int i=0;i<d.size();i++){
        cout<<i<<" "<<d[i]<<endl;
    }
}

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

const static int MAX = 100;
const static int INFTY = (1<<21);
const static int WHITE = 0;
const static int GRAY = 1;
const static int BLACK = 2;

int n, M[MAX][MAX];

void dijkstra(){
  int minv;
  int d[MAX], color[MAX];

  for(int i=0; i<n; ++i){
    d[i] = INFTY;
    color[i] = WHITE;
  }

  d[0] = 0;
  color[0] = GRAY;

  while(true){
    minv = INFTY;
    int u = -1;
    for(int i=0; i<n; ++i){
      if(minv>d[i] && color[i] != BLACK){
	u = i;
	minv = d[i];
      }
    }
    if(u == -1) break;
    color[u] =BLACK;
    for(int v = 0; v < n; ++v){
      if(color[v] != BLACK && M[u][v] != INFTY){
	if(d[v] > d[u] + M[u][v]){
	  d[v] = d[u] + M[u][v];
	  color[v] = 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 j=0; j<n; ++j){
      M[i][j] = INFTY;
    }
  }

  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;
      M[u][v] = c;
    }
  }

  dijkstra();
  

  return 0;
}