Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

var infty = 1000000000

type DirectedGraphNodeID int

type DirectedGraphNode struct {
	key         DirectedGraphNodeID
	adjacents   []*DirectedGraphNode
	costs       []int
	distance    int
	predecessor *DirectedGraphNode
}

func newDirectedGraph(nNodes int) *DirectedGraph {
	graph := DirectedGraph{}
	graph.nodes = make([]*DirectedGraphNode, nNodes)
	for i := 0; i < nNodes; i++ {
		node := DirectedGraphNode{}
		node.key = DirectedGraphNodeID(i)
		node.distance = infty
		graph.nodes[i] = &node
	}
	return &graph
}

type DirectedGraph struct {
	nodes []*DirectedGraphNode
}

func (g *DirectedGraph) setAdjacency(i, j DirectedGraphNodeID, cost int) {
	g.nodes[i].adjacents = append(g.nodes[i].adjacents, g.nodes[j])
	g.nodes[i].costs = append(g.nodes[i].costs, cost)
}

func (g *DirectedGraph) print() {
	for i := 0; i < len(g.nodes); i++ {
		node := g.nodes[i]
		key := node.key
		strings := []string{strconv.Itoa(int(key)), strconv.Itoa(node.distance)}
		for j := 0; j < len(node.adjacents); j++ {
			nodeJ := fmt.Sprintf("(%v, %v)", node.adjacents[j].key, node.costs[j])
			strings = append(strings, nodeJ)
		}
		fmt.Printf("%v\n", strings)
	}
}

func (g *DirectedGraph) relaxEdge(l, r *DirectedGraphNode, w int) {
	if r.distance > l.distance+w {
		// fmt.Printf("%v -> %v, %v\n", l.key, r.key, l.distance+w)
		r.distance = l.distance + w
		r.predecessor = l
	}
}

func main() {
	scanner.Split(bufio.ScanWords)

	nNodes := nextInt()
	graph := newDirectedGraph(nNodes)

	for i := 0; i < nNodes; i++ {
		iNode := DirectedGraphNodeID(nextInt())
		nAdjacent := nextInt()
		for j := 0; j < nAdjacent; j++ {
			jNode := DirectedGraphNodeID(nextInt())
			cost := nextInt()
			graph.setAdjacency(iNode, jNode, cost)
		}
	}

	graph.nodes[0].distance = 0
	for iNode := 0; iNode < nNodes; iNode++ {
		for iLeft := 0; iLeft < len(graph.nodes); iLeft++ {
			nodeLeft := graph.nodes[iLeft]
			for iRight := 0; iRight < len(nodeLeft.adjacents); iRight++ {
				nodeRight := nodeLeft.adjacents[iRight]
				cost := nodeLeft.costs[iRight]
				graph.relaxEdge(nodeLeft, nodeRight, cost)
			}
		}
	}

	// graph.print()

	for iNode := 0; iNode < len(graph.nodes); iNode++ {
		node := graph.nodes[iNode]
		fmt.Println(node.key, node.distance)
	}
}

C++: #include <bits/stdc++.h> 
using namespace std; 
typedef long long ll; 
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I) 
const ll INF=1e9+7; 
const ll N = 101; 


class dijkstra{ 
//0indexed 
public: 
	int v,startv; 
	vector<int> d; 
	vector<vector<pair<int,int> > >e; 

void initsize(int n0){ 
	d.resize(n0); 
	for(int i=0;i<n0;i++)d[i]=INT_MAX; 
	vector<pair<int,int> > ep; 
	for(int i=0;i<n0;i++)e.push_back(ep); 
	v=n0; 
} 

void initstart(int s){ 
	startv=s;
	d[s]=0;
}

void make_edge(int x,int y,int cost){ 
	e[x].push_back(make_pair(y,cost)); 
	e[y].push_back(make_pair(x,cost)); 
}

void make_edgedir(int x,int y,int cost){ 
	e[x].push_back(make_pair(y,cost)); 
}

void calcdistance(){ 
	// <-cost,x> 
	//cost startv->x 
	priority_queue<pair<int,int> > q; 
	pair<int,int> p; 
	vector<bool> did; 
	for(int i=0;i<v;i++)did.push_back(false); 
	did[startv]=true; 
	for(int i=0;i<e[startv].size();i++){ 
		p.first=-e[startv][i].second; 
		p.second=e[startv][i].first; 
		q.push(p); 
	}

	while(q.size()!=0){ 
		pair<int,int> p; 
		p=q.top(); 
		q.pop(); 
		int x,costsx; 
		x=p.second; 
		costsx=-p.first; 
		if(did[x]==true)continue; 
		did[x]=true; 
		d[x]=costsx; 

		for(int i=0;i<e[x].size();i++){ 
			p.first=-(d[x]+e[x][i].second); 
			p.second=e[x][i].first; 
			if(did[p.second]==false)q.push(p); 
		} 
	} 
} 

}; 



int main(){ 
	dijkstra di;
	int n;
	cin>>n;
	di.initsize(n);
	di.initstart(0);
	FOR(i,0,n){
		int u,k;
		cin>>u>>k;
		FOR(j,0,k){
			int x,co;
			cin>>x>>co;
			di.make_edgedir(u,x,co);
		}
	}
	di.calcdistance();
	FOR(i,0,n){
		cout<<i<<" "<<di.d[i]<<endl;
	}
	return 0; 
}

C++: #include <stdio.h>
#define INF (1<<21)

int main(void)
{
 int i,j,n,m,u,k,l,v,c,a[10000],b[10000],d[10000][10000];

 scanf("%d",&n);
 
 for(i=0;i<n;i++){
 	scanf("%d %d",&u,&k);
 	a[i]=INF;
 	b[i]=0;
 	for(j=0;j<k;j++){
 		scanf("%d %d",&v,&c);
 		d[u][v]=c;
 	}
 }
 a[0]=0;
 
 while(1){
 	
 	l=-1;
 	m=INF;
 	
 	for(i=0;i<n;i++){
 		if(a[i]<m && b[i]==0){
 			m=a[i];
 			l=i;
 		}
 	}
 	
 	if(l==-1) break;
 	
 	b[l]=1;
 	
 	for(i=0;i<n;i++){
 		if(a[i]>=d[l][i]+a[l]) a[i]=d[l][i]+a[l];
 	}
 }
 
 for(i=0;i<n;i++){
 	if(a[i]==INF) printf("%d -1\n",i);
 	else printf("%d %d\n",i,a[i]);
 }
 
 return 0;
}

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

#define INF 1<<29
#define MAXV 100

int V;      //????????°
int cost[MAXV+10][MAXV+10];

vector<int> dijkstra(int s){
    vector<int> d(V,INF);
    bool used[MAXV+10];
    for(int i=0;i<MAXV+10;i++) used[i]=false;

    d[s]=0;
    while(true){
        //??????????????????????????????????????§s??????????????¢???????°?????????????v??¨??????
        int v=-1;
        for(int u=0;u<V;u++){
            if(!used[u] and (v==-1 or d[u]<d[v])) v=u;
        }

        if(v==-1) break;
        
        used[v]=true;
        for(int u=0;u<V;u++){
            d[u] = min(d[u],d[v]+cost[v][u]);
        }
    }
    return d;
}

int main(){
    // initialize
    for(int i=0;i<MAXV+10;i++) for(int j=0;j<MAXV+10;j++) cost[i][j]=INF;

    cin >> V;
    for(int i=0;i<V;i++){
        int u,k;cin>>u>>k;
        for(int j=0;j<k;j++){
            int v,c;cin>>v>>c;
            cost[i][v]=c;
//            cost[v][i]=c;
        }
    }

    vector<int> dis = dijkstra(0);
    for(int i=0;i<dis.size();i++){
        printf("%d %d\n",i,dis[i]);
    }
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>

#define MAX 100
static const int INFTY =(1<<21);

int n,M[MAX][MAX];
int unvisited=0,decide=1,comp_decide=2;

void dijkstra(){
	int d[MAX];
	int state[MAX];
	for(int i=0;i<n;i++){
		d[i]=INFTY;
		state[i]=unvisited;
	}
	d[0]=0;
	state[0]=0;
	while(1){
		int mincost=INFTY,u=-1;
		for(int i=0;i<n;i++){
			if(d[i]<mincost&&state[i]!=comp_decide){
				u=i;
				mincost=d[i];
			}
		}
		if(u==-1) break;
		state[u]=comp_decide;
		for(int v=0;v<n;v++){
			if(state[v]!=comp_decide&&M[u][v]!=INFTY){
				if(d[v]>d[u]+M[u][v]){
					d[v]=d[u]+M[u][v];
					state[v]=decide;
				}
			}
		}
	}
	for(int i=0;i<n;i++){
			printf("%d %d\n",i,(d[i]==INFTY ? -1:d[i]));
	}
}

int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			M[i][j]=INFTY;
		}
	}
	int u,k,v,c;
	for(int i=0;i<n;i++){
		scanf("%d %d",&u,&k);
		for(int j=0;j<k;j++){
			scanf("%d %d",&v,&c);
			M[u][v]=c;
		}
	}
	dijkstra();
}

C++: #include<iostream>
#define INF 1<<30
using namespace std;
int main() {
  int n; cin >> n;
  int graph[100][100];
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) graph[i][j] = INF;
  for (int i = 0; i < n; ++i) {
    int u, k, v; cin >> u >> k;
    for (int j = 0; j < k; ++j) {
      cin >> v;
      cin >> graph[u][v];
    }
  }
  int mincost[100], flag[100] = {};
  mincost[0] = 0;
  for (int i = 1; i < n; ++i) mincost[i] = INF;
  while (1) {
    int x = -1;
    for (int i = 0; i < n; ++i)
      if (!flag[i] && (x == -1 || mincost[x] > mincost[i])) x = i;
    if (x == -1) break;
    flag[x] = 1;
    for (int i = 0; i < n; ++i)
      if (mincost[i] > mincost[x]+graph[x][i]) mincost[i] = mincost[x]+graph[x][i];
  }
  for (int i = 0; i < n; ++i) cout << i << " " << mincost[i] << endl;
  return 0;
}