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<iostream>
#include<climits>
using namespace std;

typedef struct{
  int A[1000];
  int ans;
  bool c;
}node;
node T[1000];
int Q[10000000];
int head=0;
int tail=1;
int n;

void function(int num){
  while(true){
    if(head==tail)break;
    num=Q[head++];
    T[num].c=true;
    for(int i=0;i<n;i++){
      if(T[num].A[i]!=0){
	//	cout<<T[num].ans<<endl;
	if(T[i].ans>T[num].ans+T[num].A[i]){
	  T[i].ans=T[num].ans+T[num].A[i];
	  T[num].A[i]=0;
	Q[tail++]=i;
	}
      }
    }
    //    cout<<"Q "<<Q[head]<<endl;
    //  cout<<head<<" "<<tail<<endl;
  }

}

int main(){
  int num;
  int son;
  int z,w;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>num;
    cin>>son;
    T[num].ans=INT_MAX;
    for(int j=0;j<son;j++){
      cin>>z>>w;
      T[num].A[z]=w;
      }
  }
  /*  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cout<<i<<" "<<j<<" "<<T[i].A[j]<<endl;
    }
    }*/
  Q[0]=0;
 T[0].ans=0;
  function(0);
  T[0].ans=0;

  /* 
for(int i=2;i<=n;i++){
    if(T[i].ans==0){
      T[i].ans=-1;
    }
    }
*/

  for(int i=0;i<n;i++){
      cout<<i<<" "<<T[i].ans<<endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int N;
int G[100][100];
int d[100];
int used[100];
const int INF=1000000000;
void dijkstra(int s){
  for(int u=0;u<N;u++)d[u]=INF;
  d[s]=0;
  while(1){   
    int v=-1;
    for(int u=0;u<N;u++){
      if((v==-1 || d[v]>d[u]) && !used[u])v=u;
    }
    if(v==-1)break;
    used[v]=1;
    for(int u=0;u<N;u++){
      d[u]=min(d[u],d[v]+G[v][u]);
    }
  }
}
int main(){

  for(int i=0;i<100;i++){
    for(int j=0;j<100;j++){
      G[i][j]=INF;
      if(i==j)G[i][j]=0;
    }
  }
  scanf("%d",&N);
  for(int i=0;i<N;i++){
    int u,k,c,v;
    scanf("%d %d",&u,&k);
    for(int j=0;j<k;j++){
      scanf("%d %d",&v,&c);
      G[u][v]=c;
     
    }
  }
  dijkstra(0);
  for(int i=0;i<N;i++)printf("%d %d\n",i,d[i]);
  return 0;
}

C++: #include <vector>
#include <iostream>
#include <algorithm>
#include <limits>

using namespace std;
using graph = vector<vector<int>>;


int main() {
    int n;
    cin >> n;

    graph G(n, vector<int>(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[u][v] = c;
        }
    }

    // Dijkstra's algorithm
    vector<int> d(n, -1);
    vector<bool> visited(n, false);

    d[0] = 0;
    visited[0] = true;
    while (1) {
        int u = -1;
        int v = -1;
        int minw = numeric_limits<int>::max();
        for (int i = 0; i < n; ++i) {
            if (!visited[i])
                continue;
            for (int j = 0; j < n; ++j) {
                int w = G[i][j] + d[i];
                if (!visited[j] && 0 < w && w < minw) {
                    u = i;
                    v = j;
                    minw = w;
                }
            }
        }
        if (v < 0) {
            break;
        }
        d[v] = minw;
        visited[v] = true;
    }

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

    return 0;
}

C++: #include <bits/stdc++.h>
#define MAX_E 1000000
#define MAX_V 100000
#define INF 1e9
typedef long long ll;
using namespace std;
/*??????????????????????????? O(V*E)*/

//??????from????????????to???????????????cost??????
struct edge{int from, to, cost;};
edge es[MAX_E]; //???
ll d[MAX_V];   //???????????¢
int V,E;        //V???????????°???E????????°

//s???????????????????????????????????????????????¢????±???????.
void Bellman_Ford(int s){
  for(int i=0; i<V ;i++) d[i]=INF;
  d[s]=0;
  while(true){
    bool update=false;
    for(int i=0; i<E ;i++){
      edge e = es[i];
      if(d[e.from] != INF && d[e.to]> d[e.from] + e.cost){
	d[e.to] = d[e.from] + e.cost;
	update = true;
      }
    }
    if(!update) break;
  }
}

//true??????????????????????????¨??????
bool find_negative_loop(){
  memset(d,0,sizeof(d));

  for(int i=0; i<V ;i++){
    for(int j=0; j<E ;j++){
      edge e = es[j];
      if(d[e.to] > d[e.from] + e.cost){
	d[e.to] = d[e.from] + e.cost;
	
	//n??????????????´??°???????????????????????????????????¨?????????
	if(i == V-1) return true;
      }
    }
  }
  return false;
}

int main(){
  cin>>V;
  for(int i=0;i<V;i++){
    int u,k;
    cin>>u>>k;
    for(int j=0,v,c;j<k;j++){
      cin>>v>>c;
      es[E++]=(edge){u,v,c};
    }
  }
  Bellman_Ford(0);
  for(int i=0;i<V;i++)
    cout<<i<<' '<<d[i]<<endl;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cctype>
#define rep(i,a,b) for(int i=(a);i<b;i++)
#define INF 1000000000
#define MAX_N 100000000
#define MAX 
using namespace std;
bool debug=false;

int cost[105][105];
int d[105];
bool used[105];
int n;

void Input(){
  cin>>n;
  rep(i,0,n)rep(j,0,n)cost[i][j]=INF;
  for(int i=0;i<n;i++){
  	int from,to,num,c;
  	cin>>from>>num;
  	for(int j=0;j<num;j++){
  		cin>>to>>c;
  		cost[from][to]=c;
  	}
  }
  rep(i,0,105){
    used[i]=false;
    d[i]=INF;
  }
}

void solve(){
  d[0]=0;
  while(1){
    int vertex=-1;

    for(int i=0;i<n;i++){
      if(!used[i]&&(vertex==-1||d[i]<d[vertex])){
	    vertex=i;
      }
    }
    if(vertex==-1)break;
    used[vertex]=true;
    for(int i=0;i<n;i++){
      d[i]=min(d[i],d[vertex]+cost[vertex][i]);
    }
  }
}

int main(){
  Input();
  solve();
  for(int i=0;i<n;i++)cout<<i<<" "<<d[i]<<endl;
  return 0;
}