Go: package main

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

type Node struct {
	Id int
	Dis int
}

type PriorityQueue []*Node

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

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].Dis < pq[j].Dis
}

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

func (pq *PriorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.(*Node))
}

func (pq *PriorityQueue) Pop() interface{} {
	n := len(*pq)
	pop := (*pq)[n-1]
	*pq = (*pq)[0:n-1]
	return pop
}

func main() {
	var n int
	fmt.Scan(&n)

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

	g := make([][]int, n)
	c := make([][]int, n)
	pq := make(PriorityQueue, 0, n)
	d := make([]int, n)
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		g[u] = make([]int, k)
		c[u] = make([]int, k)
		for j := 0; j < k; j++ {
			sc.Scan()
			g[u][j], _ = strconv.Atoi(sc.Text())
			sc.Scan()
			c[u][j], _ = strconv.Atoi(sc.Text())
		}

		if u == 0 {
			d[u] = 0
		} else {
			d[u] = 1 << 30
		}
		pq.Push(&Node{u, d[u]})
	}
	heap.Init(&pq)

	for pq.Len() > 0 {
		node := heap.Pop(&pq).(*Node)
		for i := 0; i < len(g[node.Id]); i++ {
			nextId := g[node.Id][i]
			nextDis := node.Dis + c[node.Id][i]
			if nextDis < d[nextId] {
				d[nextId] = nextDis
				heap.Push(&pq, &Node{nextId, nextDis})
			}
		}
	}

	wr := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		wr.WriteString(strconv.Itoa(i))
		wr.WriteString(" ")
		wr.WriteString(strconv.Itoa(d[i]))
		wr.WriteString("\n")
	}
	wr.Flush()
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define loop(i,a,b) for(i=a;i<b;i++)
#define rloop(i,a,b) for(i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
const int inf=1000000001;
const ll INF=1e16;
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;
static const int N=10000;

int n;
vector< vector< pair<int,int> > > adj(N,vector< pair<int,int> >());
vi d(N,inf);

void dijkstra();

int main(){
	int k,u,v,c,i,j;
	cin>>n;
	loop(i,0,n){
		cin>>u>>k;
		loop(j,0,k){
			cin>>v>>c;
			adj[u].push_back(make_pair(c,v));
		}
	}
	dijkstra();
	loop(i,0,n){
		cout<<i<<" "<<((d[i]==inf)? -1:d[i])<<endl;
	}
}

void dijkstra(){
	priority_queue< pair<int,int>, vector< pair<int,int> >, greater< pair<int,int> > > pq;
	int i;
	vi color(n,WHITE);
	
	d[0]=0;
	pq.push(make_pair(0,0));
	color[0]=GRAY;
	
	while(!pq.empty()){
		pair<int,int> f=pq.top();
		pq.pop();
		int u=f.second;
		color[u]=BLACK;
		if(d[u]<f.first){
			continue;
		}
		loop(i,0,adj[u].size()){
			int v=adj[u][i].second;
			if(color[v]==BLACK){
				continue;
			}
			if(d[v]>d[u]+adj[u][i].first){
				d[v]=d[u]+adj[u][i].first;
				pq.push(make_pair(d[v],v));
				color[v]=GRAY;
			}
		}
	}
}

C++: #include<iostream>
#include<cstdio>
#include<climits>
using namespace std;
 
#define N 10001
int n;
 
int g[N][N];
 
int weight[N];
bool us[N];
 
//prim
void prim(){
 
  int v;
  
  weight[0]=0;
   
  while(true){
    v=-1;
    
    for(int i=0;i<n;i++){
      if(us[i]!=true && (v==-1 ||weight[i]<weight[v]))v=i;
    }
 
    if(v==-1)break;
    us[v]=true;
  
 
    for(int i=0;i<n;i++){
      if(weight[i]>(g[v][i]+weight[v])){
 
        weight[i]=(g[v][i]+weight[v]);
        //      cout <<"fsafa"<<endl;
      }
    }
  }
  
}
 
 
int main(){
 
  int u,k1,c,v;
 
  for(int i=0;i<N;i++){
    for(int j=0;j<N;j++){
      g[i][j]=9953535;
    }
  }
 
  //load n
  cin >>n;
 
  //load data
  for(int i=0;i<n;i++){
    weight[i]=INT_MAX;
    us[i]=false;
    scanf("%d %d",&u,&k1);
    for(int j=0;j<k1;j++){
      scanf("%d %d",&v,&c);
      g[u][v]=c;
      //    cout <<g[i][j]<<" ";
    }
    //    cout <<endl;
  }
 
 
 
 
  //print
  prim();
 
  for(int i=0;i<n;i++){
    cout <<i<<" "<<weight[i]<<endl;
  }
  return 0;
 
}

C++: #include <bits/stdc++.h>
static const int MAX = 10000;
static const int INFTY = (1 << 20);
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 <iostream>
using namespace std;
static const int MAX =100;
static const int INFTY =(1<<21);
static const int WHITE =0;
static const int GRAY =1;
static const int BLACK =2;

int n, M[MAX][MAX];

int 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(1){
    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;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int num = 10000, num1 = 500000, INF = 20000000;
int n, d[num], c[num];
vector<pair<int, int>> v[num];
priority_queue<pair<int, int>> q;
inline void mini() {
	q.push(make_pair(0, 0));
	while (!q.empty()) {
		pair<int, int> p = q.top(); q.pop();
		int a = p.second;
		c[a] = 1;
		for (int i = 0; i < v[a].size(); i++) {
			int b = v[a][i].first;
			if (c[b] == 0 && d[b] > d[a] + v[a][i].second) {
				d[b] = d[a] + v[a][i].second;
				q.push(make_pair(d[b] * -1, b));
			}
		}
	}
		
}
int main() {
	int u, k, s, t;
	cin >> n;
	for (int i = 1; i < n; i++) {
		d[i] = INF;
		c[i] = 0;
	}
	d[0] = 0;
	for (int i = 0; i < n; i++) {
		scanf("%d%d", &u, &k);
		for (int j = 0; j < k; j++) {
			scanf("%d%d", &s, &t);
			v[u].push_back(make_pair(s, t));
		}
	}
	mini();
	for (int i = 0; i < n; i++) {
		printf("%d %d\n", i, d[i]);
	}
	return 0;
}