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 <list>
#include <string>
#include <queue>
#include <set>

/*
?§????????????¢0??§???????¢????
??????????¢????????????????????????¶????????????????????§???????°????????????§??°?????????????????£???????????????????¢????
while(????????????????????????????????§)
    list = ???????????????.Get????°?????????????????????????
    for(list)
        ?????????????????????
*/

class TVertex;
struct TPath
{
    int Cost;
    TVertex* Target;
    TPath(TVertex* ATarget, int ACost)
     : Target(ATarget), Cost(ACost) {}
    bool operator<(const TPath& Right)const
    {
        if(this->Cost  < 0) return false;
        if(Right.Cost < 0) return true;
        return this->Cost < Right.Cost;
    }
    bool operator>(const TPath& Right)const
    {
        return !((*this) < Right);
    }
};
class TVertex
{
    int FIndex;
    std::priority_queue<TPath, std::vector<TPath>, std::greater<TPath> > FList;
    int FDistance;

public:
    TVertex(int Index)
     : FIndex(Index), FDistance(INT_MAX)
    {}
    void Reserve(int Size)
    {
        // FList.reserve(Size);
    }
    void AddPath(const TPath& Path)
    {
        FList.push(Path);
    }
    void SortPath()
    {
        // std::sort(FList.begin(), FList.end(), Compare);
    }
    void PrintDistance(std::ostream& out)const
    {
        out << FIndex << " " << FDistance << std::endl;
    }
    int GetMinCostPath()const
    {
        if(FList.empty()) return INT_MAX;
        return FList.top().Cost + FDistance;
    }
    void PickupMinCostPath(int MinCost, std::list<TPath>& List)
    {
        while(FList.empty() == false && FList.top().Cost + FDistance == MinCost){
            List.push_back(TPath(FList.top().Target, FList.top().Cost + FDistance));
            FList.pop();
        }
    }
    bool SetDistance(int Distance)
    {
        if(FDistance < Distance) return false;
        FDistance = Distance;
        return true;
    }
};
class TGraph
{
    std::vector<TVertex*> FTable;
    std::set<TVertex*> FShortest;
    void Init(int n)
    {
        assert(n > 0);
        FTable.resize(n);
        for(int i = 0; i < n; ++i){
            FTable[i] = new TVertex(i);
        }
    }
    static bool CompareMinCost(const TVertex* Left, const TVertex* Right)
    {
        return Left->GetMinCostPath() < Right->GetMinCostPath();
    }
    void AddShortest(TVertex* Vertex, int Distance)
    {
        if(Vertex->SetDistance(Distance)){
            FShortest.insert(Vertex);
        }
    }
    std::list<TPath> PickupMinCostPath(int MinCost)
    {
        std::list<TPath> Result;
        for(std::set<TVertex*>::const_iterator It = FShortest.begin();
            It != FShortest.end(); ++It)
        {
            (*It)->PickupMinCostPath(MinCost, Result);
        }
        return Result;
    }
    void SetShortest(const std::list<TPath>& MinCostPathList)
    {
        for(std::list<TPath>::const_iterator It = MinCostPathList.begin();
            It != MinCostPathList.end(); ++It)
        {
            AddShortest(It->Target, It->Cost);
        }
    }
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;
        FTable[Index]->Reserve(k);
        for(int i = 0; i < k; ++i){
            int v, c;
            in >> v >> c;
            FTable[Index]->AddPath(TPath(FTable[v], c));
        }
        FTable[Index]->SortPath();
    }
    void SearchShortest()
    {
        FTable[0]->SetDistance(0);
        FShortest.insert(FTable[0]);
        while(FShortest.size() != FTable.size()){
            int MinCost = (*std::min_element(FShortest.begin(), FShortest.end(), CompareMinCost))->GetMinCostPath();
            std::list<TPath> PathList = PickupMinCostPath(MinCost);
            SetShortest(PathList);
        }
    }
    void PrintDistance(std::ostream& out)
    {
        for(int i = 0; i < FTable.size(); ++i){
            FTable[i]->PrintDistance(out);
        }
    }
};

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

C++: #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <math.h>
#include <ctype.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <functional>
using namespace std;

typedef long long ll;
typedef pair<ll, int> pp;
#define mp(a, b) make_pair(a, b)

struct edge {
	int v, w;
	int next;
	edge() {}
	edge(int v_, int w_, int n_) : v(v_), w(w_), next(n_) {}
};

int n, cnt;
int head[10001];
edge e[600000];
bool vis[10001];
ll dis[10001];

void init()
{
	cnt = 0;
	memset(head, -1, sizeof head);
}
void addedge(int u, int v, int w)
{
	e[++cnt] = edge(v, w, head[u]); head[u] = cnt;
}
void dijkstra(int s)
{
	for (int i = 0; i < n; i++)
		vis[i] = 0, dis[i] = ll(1e18);
	dis[s] = 0; vis[s] = 1;
	for (int i = head[s]; i != -1; i = e[i].next)
		dis[e[i].v] = e[i].w;
	while(1)
	{
		int idx = -1;
		ll dist = 1e18;
		for (int j = 0; j < n; j++)
			if (!vis[j] && dist > dis[j])
			{
				idx = j;
				dist = dis[j];
			}
		if (idx == -1)
			break;
		vis[idx] = 1;
		for (int j = head[idx]; j != -1; j = e[j].next)
		{
			int v = e[j].v;
			ll w = e[j].w;
			if (dis[v] > dis[idx] + w)
				dis[v] = dis[idx] + w, vis[v] = 0;
		}
	}
}

int id[10001];

int main()
{
	int s;
	while (scanf("%d", &n) == 1)
	{
		init();
		for (int i = 0; i < n; i++)
		{
			int u, k;
			scanf("%d%d", &u, &k);
			id[i] = u;
			if (u == 0)
				s = i;
			while (k--)
			{
				int v, w;
				scanf("%d%d", &v, &w);
				addedge(u, v, w);
			}
		}
		dijkstra(s);
		for (int i = 0; i < n; i++)
			printf("%d %lld\n", id[i], dis[i]);
	}
	return 0;
}