Go: package main

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

const (
	MAX_N      = 10000
	MAX_WEIGHT = 100000
)

type Edge struct {
	start  int
	end    int
	weight int
}

type ShortestInfo struct {
	edges    []Edge
	shortest int
}

type Neighbor struct {
	node   int
	weight int
}

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

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	shortestList := make([]ShortestInfo, n)
	for i := 1; i < n; i++ {
		shortestList[i] = ShortestInfo{edges: []Edge{}, shortest: MAX_N*MAX_WEIGHT + 1}
	}

	adjacentMatrix, err := createAdjacentMatrix(n, sc)
	if err != nil {
		panic(err)
	}

	sssp([]int{0}, shortestList, adjacentMatrix)
	for _, v := range shortestList {
		fmt.Printf("%d %d\n", v.getEnd(), v.shortest)
	}
}

func (si ShortestInfo) getEnd() int {
	if len(si.edges) == 0 {
		return 0
	} else {
		return si.edges[len(si.edges)-1].end
	}
}

func createAdjacentMatrix(n int, sc *bufio.Scanner) ([][]Neighbor, error) {
	adjacentMatrix := make([][]Neighbor, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			return nil, err
		}
		adjacentMatrix[id] = []Neighbor{}
		vertexNum, err := nextInt(sc)
		if err != nil {
			return nil, err
		}
		for j := 0; j < vertexNum; j++ {
			v, err := nextInt(sc)
			if err != nil {
				return nil, err
			}
			c, err := nextInt(sc)
			if err != nil {
				return nil, err
			}
			adjacentMatrix[id] = append(adjacentMatrix[id], Neighbor{v, c})
		}
	}
	return adjacentMatrix, nil
}

func sssp(nodeStack []int, shortestList []ShortestInfo, adjacentMatrix [][]Neighbor) {
	for len(nodeStack) > 0 {
		currentNode := nodeStack[0]
		nodeStack = nodeStack[1:]
		currentShortest := getShortest(currentNode, shortestList)
		adjacents := findAdjacents(currentNode, adjacentMatrix)
		for i := 0; i < len(adjacents); i++ {
			adj := adjacents[i]
			if currentShortest.shortest+adj.weight < getShortest(adj.end, shortestList).shortest {
				setShortest(adj.end, adj, currentShortest, shortestList)
				nodeStack = append(nodeStack, adj.end)
			}
		}
	}
}

func getShortest(node int, shortestList []ShortestInfo) ShortestInfo {
	return shortestList[node]
}

func setShortest(node int, edge Edge, currentShortest ShortestInfo, shortestList []ShortestInfo) {
	newEdges := make([]Edge, len(currentShortest.edges)+1)
	for i := 0; i < len(newEdges)-1; i++ {
		newEdges[i] = currentShortest.edges[i]
	}
	newEdges[len(newEdges)-1] = edge
	shortestInfo := ShortestInfo{edges: newEdges, shortest: currentShortest.shortest + edge.weight}
	shortestList[node] = shortestInfo
}

func findAdjacents(currentNode int, adjacentMatrix [][]Neighbor) []Edge {
	adjacents := adjacentMatrix[currentNode]
	edges := []Edge{}
	for _, neighbor := range adjacents {
		edges = append(edges, Edge{start: currentNode, end: neighbor.node, weight: neighbor.weight})
	}
	return edges
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=1000000;
const int infly=(1<<21);
const int white=0;
const int grey=1;
const int black=-1;

struct node{
    int k;
    int to;
    int next;
};

struct node m[2*maxn+5];
int head[maxn+5],n=0;
int color[maxn+5],d[maxn+5],p[maxn+5];
int tu,k,v,c,nn;
int cnt=0;

void add(int u,int v,int tot)
{
    m[++n].k=v;
    m[n].next=head[u];
    m[n].to=tot;
    head[u]=n;
}

void ds()
{
    priority_queue<pair<int, int> > p;
    for(int i=0;i<=maxn;i++)
    {
        color[i]=white;
        d[i]=infly;
    }

    d[0]=0;
    p.push(make_pair(0,0));
    color[0]=grey;

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

        color[u]=black;

        for(int j=head[u];j;j=m[j].next)
        {
            int v=m[j].k;
            if(color[v]==black) continue;
            if(d[v]>d[u]+m[j].to)
            {
                d[v]=d[u]+m[j].to;
                p.push(make_pair(d[v]*(-1),v));
                color[v]=grey;
            }
        }
    }
}

int main()
{
    scanf("%d",&nn);
    for(int i=1;i<=nn;i++)
    {
        scanf("%d%d",&tu,&k);
        for(int j=1;j<=k;j++)
        {
            scanf("%d%d",&v,&c);
            add(tu,v,c);
        }
    }


    ds();
    for(int i=0;i<nn;i++)
    {
        printf("%d ",i);
        if(d[i]!=infly) printf("%d\n",d[i]);
        else printf("-1\n");
    }

    return 0;
}

C++: #include<iostream>
#include<climits>
using namespace std;

typedef struct{
  int A[10010];
  long long ans;
}node;
node T[10010];
int Q[100300003];
int head=0;
int tail=1;
int n;

void function(int num){
  while(true){
    if(head==tail)break;
    num=Q[head++];
        if(head==100300002)head=1;
    for(int i=0;i<n;i++){
      if(T[num].A[i]!=-1){
	if(T[i].ans>T[num].ans+T[num].A[i]){
	  T[i].ans=T[num].ans+T[num].A[i];
	  Q[tail++]=i;
	  	  if(tail==100300002)tail=1;
	}
      }
    }
  }
}

int main(){
  int num,son,z,w;
  cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      T[i].A[j]=-1;
    }
  }


  for(int i=0;i<n;i++){
    cin>>num>>son;
    T[num].ans=LONG_LONG_MAX;
    for(int j=0;j<son;j++){
      cin>>z>>w;
      T[num].A[z]=w;
      }
  }
  Q[0]=0;
  T[0].ans=0;
  function(0);
 
  for(int i=0;i<n;i++){
      cout<<i<<" "<<T[i].ans<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;

void solve(){
  int n; cin >> n;
  vector<vector<pair<int, int> > > v(n);
  for(int i = 0; i < n; i++){
    int u, k; cin >> u >> k;
    for(int j = 0; j < k; j++){
      int a, b; cin >> a >> b;
      v[u].push_back(make_pair(a, b));
    }
  }
  int d[n];
  bool c[n];
  for(int i = 0; i < n; i++){
    c[i] = 0;
    d[i] = 10000000;
  }
  priority_queue<pair<int, int > > pq;
  pq.push(make_pair(0, 0));
  d[0] = 0;
  while(pq.empty() == false){
    int x = pq.top().second;
    pq.pop();
    c[x] = 1;
    for(int i = 0; i < v[x].size(); i++){
      int y = v[x][i].first;
      if(c[y] == 0){
        if(d[y] > d[x] + v[x][i].second){
          d[y] = d[x] + v[x][i].second;
          pq.push(make_pair(d[y] * (-1), y));
        }
      }
    }
  }
  for(int i = 0; i < n; i++){
    cout << i << " " << d[i] << endl;
  }
  return;
}

int main(){
  solve();
  return 0;
}

C++: #include<stdio.h>
int v[10001][10001];
int a[10001][10001];
int d[10001];
int pi[10001];
char color[10001];
int n,mincost;
int k[10001];

void dijkstra(void);

int main(){
  int i,j,u;

  scanf("%d",&n);
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      a[i][j]=0;
    }
  }

  for(i=0;i<n;i++){
    scanf("%d %d" ,&u,&k[i]);      
  for(j=1;j<=k[i];j++){
     scanf("%d",&v[u][j]);
     scanf("%d",&a[u][v[u][j]]);   
    }
  }
  dijkstra();

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

  return 0;
}

void dijkstra(void){
  int i,j,u,min;
  
  for(i=0;i<n;i++){
    d[i]=2000000000;
    pi[i] = -1;
    color[i]='W';
  }
  d[0]=0;
  while(1){
    mincost = 2000000000;
    for(i=0;i<n;i++){
      if(color[i]!='B' && d[i] < mincost){
	mincost = d[i];
        u = i;
      }
    }
    if(mincost==2000000000) break;
    color[u]='B';
  
  for(i=0;i<n;i++){
    if(color[i]!='B' && a[u][i]!=0  && d[u]+a[u][i]<d[i]){
      pi[i] = u;
      d[i]=d[u]+a[u][i];
     }
   }
  }
}