Go: package main

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

var sc = bufio.NewScanner(os.Stdin)

const T = 10000000

var n int
var A [101][101]int

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}
func Shortestsearch() {
	var Minimum int
	var d, color [100]int
	var u int

	for i := 0; i < n; i++ {
		d[i] = T
		color[i] = 0
	}

	d[0] = 0
	color[0] = 1

	for {
		Minimum = T
		u = -1
		for i := 0; i < n; i++ {
			if Minimum > d[i] && color[i] != 2 {
				u = i
				Minimum = d[i]
			}
		}
		if u == -1 {
			break
		}
		color[u] = 2
		for j := 0; j < n; j++ {
			if color[j] != 2 && A[u][j] != T {
				if d[j] > d[u]+A[u][j] {
					d[j] = d[u] + A[u][j]
					color[j] = 1
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}
func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			A[i][j] = T
		}
	}
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			v := nextInt()
			c := nextInt()
			A[u][v] = c
		}
	}
	Shortestsearch()
}

Python: INF = float('inf')
N = int(input())
D = [INF] * N
Visited = [False] * N
M = [[INF] * N for _ in range(N)]
for _ in range(N):
    u, k, *cv = map(int, input().split())
    for i in range(0, len(cv), 2):
        c, v = cv[i], cv[i + 1]
        M[u][c] = v

D[0] = 0

while True:
    min_cost = INF
    u = -1
    for i in range(N):
        if not Visited[i] and D[i] < min_cost:
            u = i
            min_cost = D[i]

    if u == -1:
        break

    Visited[u] = True

    for i in range(N):
        if not Visited[i] and M[u][i] != INF and M[u][i] + D[u] < D[i]:
            D[i] = M[u][i] + D[u]

for i, d in enumerate(D):
    print(i, d)

Python: n=int(input())
M=[[-1]*n for _ in[0]*n]
for _ in[0]*n:
 e=list(map(int,input().split()))
 for i in range(e[1]):k=2*-~i;M[e[0]][e[k]]=e[k+1]
d=[0]+[1e6]*n
c=[1]*n
while 1:
 m,u=1e6,-1
 for i in range(n):
  if m>d[i]and c[i]:m,u=d[i],i
 if u<0:break
 c[u]=0
 for v in range(n):
  if c[v]and 1+M[u][v]and d[v]>d[u]+M[u][v]:
   d[v]=d[u]+M[u][v]
for i in range(n):print(i,d[i])

Python: n = int(input())

adj = [[False for j in range(n)] for i in range(n)]

for u in range(n):
    x = list(map(int, input().split()))
    for v, c in zip(x[2::2], x[3::2]):
        adj[u][v] = c

weights = [9900000] * n


def search(goal, path, weight):
    e = path[-1]
    if weight < weights[goal]:
        if e == goal:
            weights[goal] = weight
        else:
            for v, c in enumerate(adj[e]):
                if (v not in path) and (c is not False):
                    path.append(v)
                    weight += c
                    search(goal, path, weight)
                    weight -= c
                    path.pop()


for i in range(n):
    search(i, [0], 0)

for v, w in enumerate(weights):
    print(v, w)

Python: INFTY = 1 << 21
WHITE = 0
GRAY = 1
BLACK = 2
 
n = int(input())
M = [list(map(int, input().split())) for i in range(n)]
 
d = [INFTY for i in range(n)]
color = [WHITE for i in range(n)]
 
for i in range(n):
    u = int(input())
    k = int(input())
    for j in range(k):
        v = int(input())
        c = int(input())
        M[u][v] = c
        
def dijkstra():
    global M, d, color
    d[0] = 0
    color[0] = GRAY
    while(1):
        minv = INFTY
        u = -1
        for i in range(n):
            if (minv > d[i] and color[i] != BLACK):
                u = i
                minv = d[i]
        if (u == -1):
            break
        color[u] = BLACK
        for v in range(n):
            if (color[v] != BLACK and M[u][v] != INFTY):
                if (d[v] > d[u] + M[u][v]):
                    d[v] = d[u] + M[u][v]
                    color[v] = GRAY
    for i in range(n):
        print(i, (-1 if d[i] == INFTY else d[i]))
dijkstra()

Python: import sys
from collections import defaultdict


def main():
    n = int(input())
    lines = sys.stdin.readlines()
    vertex_weights = defaultdict(list)
    for i in range(n):
        u, k, *vtx_wght = map(int, lines[i].split())
        for v_i in range(k):
            vertex_weights[u].append((vtx_wght[2*v_i], vtx_wght[2*v_i+1]))

    dist_from_0 = {}
    vertices = [(0, 0)]
    while len(dist_from_0) != n:
        vertices.sort(key=lambda x: x[1], reverse=True)
        vtx, vtx_dist = vertices.pop()
        dist_from_0[vtx] = vtx_dist

        neighbor_vtx = vertex_weights[vtx]

        for n_vtx, n_dist in neighbor_vtx:
            if n_vtx not in dist_from_0.keys():
                vertices.append((n_vtx, vtx_dist + n_dist))

        [vertices.remove(x) for x in vertices[:] if x[0] in dist_from_0]


    answers = [None] * n
    sorted_dist = sorted(dist_from_0.items(), key=lambda x: x[0])
    for i in range(n):
        answers[i] = f"{sorted_dist[i][0]} {sorted_dist[i][1]}"
    [print(ans) for ans in answers]
    return


main()

Python: n=int(input())
d=[0]+[1e6]*n
c=[1]*n
M=[[-1]*n for _ in[0]*n]

for _ in[0]*n:
 data=list(map(int,input().split()))
 for i in range(data[1]):k=2*-~i;M[data[0]][data[k]]=data[k+1]

while 1:
 m,u=1e6,-1
 
 for i in range(n):
  if m>d[i]and c[i]:
      m,u=d[i],i
 if u<0:
     break
 c[u]=0
 
 for v in range(n):
  if c[v]and 1+M[u][v]and d[v]>d[u]+M[u][v]:
   d[v]=d[u]+M[u][v]
   
for i in range(n):
    print(i,d[i])

Python: n = int(input())
adj = [[float('inf')]*n for _ in range(n)]
for i in range(n):
    a = list(map(int, input().split()))
    for j,k in zip(a[2::2], a[3::2]):
        adj[i][j] = k
color = [0]*n
d = [float('inf')]*n
d[0] = 0

while 1:
    mincost = float('inf')
    for i in range(n):
        if color[i] != 1 and d[i] < mincost:
            mincost = d[i]
            u = i
    if mincost == float('inf'):
        break
        
    color[u] = 1
    
    for i in range(n):
        if color[i] != 1 and adj[u][i] != float('inf'):
            if d[u]+adj[u][i] < d[i]:
                d[i] = d[u] + adj[u][i]
                
for i in range(n):
    print(i, d[i])

Python: def dijkstra(s):
    color = ["white" for i in range(n)]

    d = [inf for i in range(n)]

    p = [-1 for i in range(n)]

    d[s] = 0

    while True:
        mincost = inf

        for i in range(n):
            if color[i] != "black" and d[i] < mincost:
                mincost = d[i]
                u = i

        if mincost == inf:
            break

        color[u] = "black"

        for v in range(n):
            if color[v] != "black" and ci[u][v] != inf:
                if d[u] + ci[u][v] < d[v]:
                    d[v] = d[u] + ci[u][v]
                    p[v] = u
                    color[v] = "gray"
    return d


inf = 10 ** 9
n = int(input())
ci = [[inf for i in range(n)] for j in range(n)]

for i in range(n):
    data = list(map(int, input().split()))
    u = data[0]
    k = data[1]

    for j in range(0, 2*k, 2):
        v, w = data[j+2], data[j+3]
        ci[u][v] = w

dList = dijkstra(0)

for i, d in enumerate(dList):
    print(i, d)