Go: package main

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

const NIL = -1

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	route := make([][]Node, n)
	weight := make([]int, n)
	for i := 0; i < n; i++ {
		weight[i] = NIL
	}

	for i := 0; i < n; i++ {
		u := scanInt()
		k := scanInt()
		route[i] = make([]Node, k)
		for j := 0; j < k; j++ {
			route[u][j] = Node{id: scanInt(), weight: scanInt()}
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if weight[node.id] != NIL {
			continue
		}
		weight[node.id] = node.weight

		for _, nextNode := range route[node.id] {
			if weight[nextNode.id] != NIL {
				continue
			}
			nextNode.weight += node.weight
			heap.Push(h, nextNode)
		}
	}

	for i, w := range weight {
		fmt.Println(i, w)
	}
}

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

#define MAX 10000
#define INF (1<<29)

struct Edge{
  int c, to;
};

struct state{
  int node, c;
  bool operator <(const state &b)const{
    return c > b.c;
  }
};

int n;
int d[MAX+1];
vector<Edge> G[MAX+1];
priority_queue<state> q;

void dijkstra(){

  fill(d,d+MAX+1,INF);
  q.push((state){0,0});
  d[0] = 0;

  while(!q.empty()){

    state info;
    info = q.top();
    q.pop();

    for(int i = 0; i < G[info.node].size(); i++){
      if(d[G[info.node][i].to] > G[info.node][i].c + d[info.node]){
        d[G[info.node][i].to] = G[info.node][i].c + d[info.node];
        q.push((state){G[info.node][i].to,d[info.node]});
      }
    }
  }

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



}

int main(){

  cin >> n;
  for(int i = 0; i < n; i++){
    int k, v, cc, u;
    cin >> u >> k;
    for(int j = 0; j < k; j++){
      cin >> v >> cc;
      G[u].push_back((Edge){cc,v});
    }
  }

  dijkstra();

}

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

#define INF 2000000001
#define MAX 10001
#define NIL -1

using namespace std;

typedef struct {
    int toid;
    int far;
} PATH;

vector<PATH> D[MAX];
int N;
int Dist[MAX];

void dijkstra(){//0??????????????¢??????????????£?????????
    
    bool complete[MAX];
    vector<int> S;
    
    for (int i = 0; i < N; i++) {
        complete[i] = false;
        Dist[i] = INF;
    }
 
    //???????¨????
    Dist[0] = 0;
    S.push_back(0);
    int u=0,minf;
    complete[u] = true;
    
    while (1) {
        if (S.size() == N) break;
        
        //????????????
        for (int j = 0; j < D[u].size(); j++) {
            if (!complete[D[u][j].toid] && Dist[u] + D[u][j].far < Dist[D[u][j].toid]) {
                Dist[D[u][j].toid] = Dist[u] + D[u][j].far;
            }
        }
        
        
        
        u = NIL;
        minf = INF;
        
        //?¨??????´??°
//        for (int i = 0; i < S.size(); i++) {
//            
//            for (int j = 0; j < D[S[i]].size(); j++) {
//                if (Dist[D[S[i]][j].toid] < minf && !complete[D[S[i]][j].toid]) {
//                    minf = Dist[D[S[i]][j].toid];
//                    u = D[S[i]][j].toid;
//                }
//            }
//            
//        }
        
        for (int i = 0; i < N; i++) {
            if (!complete[i] && Dist[i] < minf) {
                minf = Dist[i];
                u = i;
            }
        }
        
        S.push_back(u);
        complete[u] = true;
        //u = S.size()-1;//????????§u????????§??¨????????????????????????
    }
}

int main(){
    cin >> N;
    for (int i = 0; i < N; i++) {
        int bb1,bb2;
        cin >> bb1 >> bb2;
        for (int j = 0; j < bb2; j++) {
            PATH bb3;
            cin >> bb3.toid >> bb3.far;
            D[i].push_back(bb3);
        }
    }
    
    dijkstra();
    
    for (int i = 0; i < N; i++) {
        printf("%d %d\n",i,Dist[i]);
    }
    
    return 0;
}

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 <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

const int maxn=10000;
const int inf=(1<<20);

int n;
vector<pair<int,int> > adj[maxn];

void Dijkstra(){
    priority_queue<pair<int,int> > PQ;
 //   int color[maxn];
    bool vis[maxn];
    int d[maxn];
    for(int i=0;i<n;i++){
        d[i]=inf;
     //   color[i]=white;
        vis[i]=false;
    }

    d[0]=0;
    PQ.push(make_pair(0,0));
 //   color[0]=gray;
  //  vis[0]=1;

    while(!PQ.empty()){
        pair<int,int> f=PQ.top();PQ.pop();
        int u=f.second;

      //  color[u]=black;
        vis[u]=true;

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

        for(int j=0;j<adj[u].size();j++){
            int v=adj[u][j].first;
            if(vis[v]==true) continue;
            if(d[v]>d[u]+adj[u][j].second){
                d[v]=d[u]+adj[u][j].second;
    // priority_queue priority_queue 默认优先较大值， 因此要乘以-1。
                PQ.push(make_pair(d[v]*(-1),v));
              //  vis[v]=true;这句不能加
              //  color[v]=gray;
            }
        }
    }

    for(int i=0;i<n;i++){
        printf("%d %d\n",i,(d[i]==inf?-1:d[i]));
    }
}

int main(){
    int k,u,v,c;

    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d%d",&u,&k);
        for(int j=0;j<k;j++){
            scanf("%d%d",&v,&c);
            adj[u].push_back(make_pair(v,c));
        }
    }

    Dijkstra();

    return 0;
}