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 <cassert>
#include <climits>
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>


class TVertex;
struct TPath
{
    int Cost;
    TVertex* Target;
    TPath(TVertex* ATarget, int ACost)
     : Target(ATarget), Cost(ACost) {}
};
class TVertex
{
    int FIndex;
    std::vector<TPath> FList;
    int FDistance;

public:
    TVertex(int Index)
     : FIndex(Index), FDistance(INT_MAX)
    {}
    void AddPath(const TPath& Path)
    {
        FList.push_back(Path);
    }
    void PrintDistance(std::ostream& out)const
    {
        out << FIndex << " " << FDistance << std::endl;
    }
    void BreadthFirstSearch(int Distance)
    {
        if(FDistance <= Distance) return;
        FDistance = Distance;
        for(std::vector<TPath>::iterator It = FList.begin(); It != FList.end(); ++It){
            It->Target->BreadthFirstSearch(Distance + It->Cost);
        }
    }
};
class TGraph
{
    std::vector<TVertex*> FTable;
    void Init(int n)
    {
        assert(n > 0);
        FTable.resize(n);
        for(int i = 0; i < n; ++i){
            FTable[i] = new TVertex(i);
        }
    }
public:
    TGraph(int n)
    {
        Init(n);
    }
    
    void SetupVertex(std::istream& in)
    {
        int Index;
        in >> Index;
        assert(Index >= 0);
        assert(Index < FTable.size());
        int k;
        in >> k;
        for(int i = 0; i < k; ++i){
            int v, c;
            in >> v >> c;
            FTable[Index]->AddPath(TPath(FTable[v], c));
        }
    }
    void BreadthFirstSearch()
    {
        FTable[0]->BreadthFirstSearch(0);
    }
    void PrintDistance(std::ostream& out)
    {
        for(int i = 0; i < FTable.size(); ++i){
            FTable[i]->PrintDistance(out);
        }
    }
};

int main()
{
    int n;
    std::cin >> n;
    TGraph Graph(n);
    for(int i = 0; i < n; ++i){
        Graph.SetupVertex(std::cin);
    }
    Graph.BreadthFirstSearch();
    Graph.PrintDistance(std::cout);
    return 0;
    
}

C++: #include <bits/stdc++.h>
using namespace std;
#define reep(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) reep((i),0,(n))
typedef pair<int,int> pii;
#define F first
#define S second

int main(){
    int n;
    cin>>n;
    vector<vector<pii>> vv(n);
    rep(i,n){
        int a;
        cin>>a;
        int m;
        cin>>m;
        rep(j,m){
            int b,c;
            cin>>b>>c;
            vv[a].push_back(pii(b,c));
        }
    }
    priority_queue<pii> pq;
    pq.push(pii(0,0));
    vector<int> dp(n,-1);
    while(pq.size()){
        auto top=pq.top();
        pq.pop();
        if(dp[top.S]>=0) continue;
        dp[top.S]=-top.F;
        for(pii x:vv[top.S]){
            if(dp[x.F]>=0) continue;
            pq.push(pii(top.F-x.S,x.F));
        }
    }
    rep(i,n){
        cout<<i<<" "<<dp[i]<<endl;
    }
}

C++: #include <bits/stdc++.h>

#define NIL -2
#define INF 1000000001

using namespace std;
static const int BLK = 2;
static const int GRY = 1;
static const int WHT = 0;

void dijkstra(const vector<vector<pair<int,int> > > &adj_list, 
                    vector<int> &color, 
                    vector<int> &parent, 
                    vector<int> &minpath,
                    int start_node)
{
    priority_queue<pair<int,int>,vector<pair<int,int> >, greater<pair<int,int> > >  PQ;
    PQ.push(make_pair(start_node,0));

    while(!PQ.empty()){
        pair<int,int> t = PQ.top(); PQ.pop();
        int u = t.second;
        color[u] = BLK;
        if(minpath[u] < t.first) continue;
        for(int i = 0;i < adj_list[u].size();i++){
            int v = adj_list[u][i].first;
            if(color[v] != BLK){
                if(minpath[u] + adj_list[u][i].second < minpath[v]){
                    minpath[v] = minpath[u] + adj_list[u][i].second;
                    parent[v] = u;
                    color[v] = GRY;
                    PQ.push(make_pair(minpath[v],v));
                }  
            }
        }
        
    }

}


int main(){
    int mat_size;
    int node, deg;
    int to,weight;
    int temp;
    int cost = 0;

    scanf("%d",&mat_size);
    vector<vector<pair<int,int> > > adj_list(mat_size);
    vector<int> color(mat_size,WHT);
    vector<int> minpath(mat_size,INF);
    vector<int> parent(mat_size,-1);
    
    for(int i = 0;i < mat_size;i++){
        scanf("%d %d",&node,&deg);
        for(int j = 0;j < deg;j++){
            scanf("%d %d", &to, &weight);
            adj_list[node].push_back(make_pair(to,weight));
        }
    }
    
    minpath[0] = 0;
    dijkstra(adj_list,color,parent,minpath,0);

    for(int i = 0; i < mat_size;i++){
        printf("%d %d\n",i,minpath[i]);
    }

    return 0;   
}

C++: #include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
#define INF 1 << 30
struct ss{
	int next;
	int to;
	int val;
}e[100050];
int head[100050];
int tot;
void add(int x,int y,int val){
	e[tot].next=head[x];
	e[tot].to=y;
	e[tot].val=val;
	head[x]=tot++;
}
int n;
int vis[100050]={0};
int dis[10005];
void spfa(){
	for(int i=0;i<n;i++){
		dis[i]=INF;
	}
	vis[0]=1;
	dis[0]=0;
	queue<int> st;
	st.push(0);
	while(!st.empty()){
		int t=st.front();
		st.pop();
		vis[t]=0;
		for(int i=head[t];i!=-1;i=e[i].next){
			int to=e[i].to;
			if(dis[to]>dis[t]+e[i].val){
				dis[to]=dis[t]+e[i].val;
				if(!vis[to]){
					st.push(to);
					vis[to]=1;
				}
			}
		}
	}
}
int main(){
	scanf("%d",&n);
	tot=0;
	memset(head,-1,sizeof(head));
	int a,to,val,num;
	for(int i=0;i<n;i++){
		scanf("%d %d",&a,&num);
		for(int j=1;j<=num;j++){
			scanf("%d %d",&to,&val);
			add(a,to,val);
		}
	}
	spfa();
	printf("0 0\n");
	for(int i=1;i<n;i++){
		printf("%d %d\n",i,dis[i]);
	}
	return 0;
}