Problem:


スパイダー人

正義のヒーロー「スパイダー人」は、腕からロープを出してビルからビルへ飛び移ることができます。しかし、ロープが短いので自分からの距離が 50 以下のビルにしか移動できません。それより遠くのビルに移動するには、一旦別のビルに飛び移らなくてはなりません。

ビルの数 n、n 個のビルの情報、スパイダー人の移動開始位置及び目的地を入力とし、その移動の最短経路を出力するプログラムを作成してください。どのようにビルを経由しても目標のビルに移動できない場合は NA と出力してください。各ビルは点として扱い、最短距離で移動するビルの経由方法が２つ以上存在することはないものとします。

Input

複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。
各データセットは以下の形式で与えられます。

n
b1 x1 y1
b2 x2 y2
:
bn xn yn
m
s1 g1
s2 g2
:
sm gm

１行目にビルの数 n (1 ≤ n ≤ 100)、続く n 行に i 番目のビルのビル番号 bi (1 ≤ bi ≤ n)、そのビルのx座標とy座標を表す整数 xi, yi (-1000 ≤ xi, yi ≤ 1000) が空白区切りで与えられます。

続く行に移動情報の個数 m (1 ≤ m ≤ 100)、続く m 行にi  番目の移動情報が与えられます。各移動情報として、移動を開始するビルの番号 si と目的地ビルの番号 gi が空白区切りで与えられます。

データセットの数は 10 を超えません。

Output

入力データセットごとに次の形式で出力します。

i 行目に i 番目の移動情報に対する経路または NA を１行に出力します。各経路は以下の形式で出力します。

si bri1 bri2 ... gi

brij は i 番目の移動情報における、j 番目に経由するビルの番号を表します。

Sample Input

4
1 0 0
2 30 0
3 60 40
4 0 60
2
1 3
1 4
22
1 0 0
2 150 40
3 30 20
4 180 150
5 40 80
6 130 130
7 72 28
8 172 118
9 50 50
10 160 82
11 90 105
12 144 131
13 130 64
14 80 140
15 38 117
16 190 90
17 60 100
18 100 70
19 130 100
20 71 69
21 200 110
22 120 150
1
1 22
0

Output for the Sample Input

1 2 3
NA
1 3 9 20 11 6 22



Sample input:
4
1 0 0
2 30 0
3 60 40
4 0 60
2
1 3
1 4
22
1 0 0
2 150 40
3 30 20
4 180 150
5 40 80
6 130 130
7 72 28
8 172 118
9 50 50
10 160 82
11 90 105
12 144 131
13 130 64
14 80 140
15 38 117
16 190 90
17 60 100
18 100 70
19 130 100
20 71 69
21 200 110
22 120 150
1
1 22
0


Sample output:
1 2 3
NA
1 3 9 20 11 6 22

Submission status: Accepted

Solution code:
import copy, math

def f(n, d, D, res, buil):
    global sd, ans
    d += D
    res.append(n)
    if sd is not None and sd <= d:
        return
    elif n == g:
        ans = res
        sd = d
        return
    else:
        new_buil = copy.deepcopy(buil)
        A = new_buil[n]
        del new_buil[n]
        for m in new_buil:
            B = new_buil[m]
            D = py(A, B)
            if D:
                f(m, d, D, res[:], new_buil)
            
def py(A, B):
    x = 0
    y = 1
    D = math.sqrt((B[x]-A[x])**2+(B[y]-A[y])**2)
    if D <= 50:
        return D

while True:
    N = input()
    if N == 0: break
    buil = {}
    for i in range(N):
        n, x, y = map(int, raw_input().split())
        buil[n] = (x, y)
    for i in range(input()):
        s, g = map(int, raw_input().split())
        sd = None
        ans = None
        f(s, 0, 0, [], buil)
        if ans:
            print ' '.join(map(str, ans))
        else:
            print 'NA'

Submission status: Accepted

Solution code:
import copy, math

def f(n, d, D, res, buil):
    global sd, ans
    d += D
    res.append(n)
    if sd is not None and sd <= d:
        return
    elif n == g:
        ans = res
        sd = d
        return
    else:
        new_buil = copy.deepcopy(buil)
        A = new_buil[n]
        del new_buil[n]
        for m, B in new_buil.items():
            D = py(A, B)
            if D:
                f(m, d, D, res[:], new_buil)
            
def py(A, B):
    x = 0
    y = 1
    D = math.sqrt((B[x]-A[x])**2+(B[y]-A[y])**2)
    if D <= 50:
        return D

while True:
    N = input()
    if N == 0: break
    buil = {}
    for i in range(N):
        n, x, y = map(int, raw_input().split())
        buil[n] = (x, y)
    for i in range(input()):
        s, g = map(int, raw_input().split())
        sd = None
        ans = None
        f(s, 0, 0, [], buil)
        if ans:
            print ' '.join(map(str, ans))
        else:
            print 'NA'

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0155

"""
import sys
from sys import stdin
from math import sqrt
input = stdin.readline


def dijkstra(s, G):
    BLACK, GRAY, WHITE = 0, 1, 2
    d = [float('inf')] * 101
    color = [WHITE] * 101
    p = [-1] * 101

    d[s] = 0
    while True:
        mincost = float('inf')
        # ??\??????????????§??????????????¨?????????????????????u???????????????
        for i in range(101):
            if color[i] != BLACK and d[i] < mincost:  # S????±???????????????????S??¨??\?¶?????????????????????????????????????????°???????????????????
                mincost = d[i]
                u = i  # u??????????????????ID
        if mincost == float('inf'):
            break
        color[u] = BLACK  # ?????????u???S????±???????????????´???
        for v in range(101):
            if color[v] != BLACK and G[u][v] != float('inf'):
                # v????????????????????????????????°??????S???????????£???u????????????????????????????????????????????°??????????????±??§??´??°??????
                if d[u] + G[u][v] < d[v]:
                    d[v] = d[u] + G[u][v]
                    p[v] = u
                    color[v] = GRAY
    return d, p


def main(args):
    while True:
        n = int(input())
        if n == 0:
            break

        G = [[float('inf')] * (100+1) for _ in range(100+1)]
        data = []
        for _ in range(n):
            b, x, y = map(int, input().strip().split(' '))
            data.append([b, x, y])

        for i in range(n):
            b1, x1, y1 = data[i]
            for j in range(n):
                b2, x2, y2 = data[j]
                if b1 == b2:
                    continue
                dist = sqrt((x1 - x2)**2 + (y1 - y2)**2)
                if dist <= 50.0:
                    G[b1][b2] = dist
                    G[b2][b1] = dist

        m = int(input())
        for _ in range(m):
            s, g = map(int, input().strip().split(' '))
            if s == g:
                print(s)
                continue
            if (not 1<= s <= 100) or (not 1<= g <= 100):
                print('NA')
                continue
            d, p = dijkstra(s, G)
            if d[g] == float('inf'):
                print('NA')
            else:
                path = [g]
                while p[g] != s:
                    path.append(p[g])
                    g = p[g]
                path.append(s)
                rev = path[::-1]
                print(' '.join(map(str, rev)))


def main2(args):
    while True:
        n = int(input())
        if n == 0:
            break

        for _ in range(n):
            b, x, y = map(int, input().strip().split(' '))

        m = int(input())
        for _ in range(m):
            s, g = map(int, input().strip().split(' '))
            print('NA')


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0155

"""
import sys
from sys import stdin
from math import sqrt
input = stdin.readline


def dijkstra(s, G):
    BLACK, GRAY, WHITE = 0, 1, 2
    d = [float('inf')] * 101
    color = [WHITE] * 101
    p = [-1] * 101

    d[s] = 0
    while True:
        mincost = float('inf')
        # ??\??????????????§??????????????¨?????????????????????u???????????????
        for i in range(101):
            if color[i] != BLACK and d[i] < mincost:  # S????±???????????????????S??¨??\?¶?????????????????????????????????????????°???????????????????
                mincost = d[i]
                u = i  # u??????????????????ID
        if mincost == float('inf'):
            break
        color[u] = BLACK  # ?????????u???S????±???????????????´???
        for v in range(101):
            if color[v] != BLACK and G[u][v] != float('inf'):
                # v????????????????????????????????°??????S???????????£???u????????????????????????????????????????????°??????????????±??§??´??°??????
                if d[u] + G[u][v] < d[v]:
                    d[v] = d[u] + G[u][v]
                    p[v] = u
                    color[v] = GRAY
    return d, p


def main(args):
    while True:
        n = int(input())
        if n == 0:
            break

        G = [[float('inf')] * (100+1) for _ in range(100+1)]
        data = []
        for _ in range(n):
            b, x, y = map(int, input().split())
            data.append([b, x, y])

        for i in range(n):
            b1, x1, y1 = data[i]
            for j in range(n):
                b2, x2, y2 = data[j]
                if b1 == b2:
                    continue
                dist = sqrt((x1 - x2)**2 + (y1 - y2)**2)
                if dist <= 50.0:
                    G[b1][b2] = dist
                    G[b2][b1] = dist

        m = int(input())
        for _ in range(m):
            s, g = map(int, input().split())
            if s == g:
                print(s)
                continue
            if (not 1<= s <= 100) or (not 1<= g <= 100):
                print('NA')
                continue
            d, p = dijkstra(s, G)
            if d[g] == float('inf'):
                print('NA')
            else:
                path = [g]
                while p[g] != s:
                    path.append(p[g])
                    g = p[g]
                path.append(s)
                rev = path[::-1]
                print(' '.join(map(str, rev)))


def main2(args):
    while True:
        n = int(input())
        if n == 0:
            break

        for _ in range(n):
            b, x, y = map(int, input().split())

        m = int(input())
        for _ in range(m):
            s, g = map(int, input().split())
            print('NA')


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

INF = 10 ** 20

while True:
  n = int(input())
  if n == 0:break

  buil_point = [None] * n
  for _ in range(n):
    b, x, y = map(int, input().split())
    buil_point[b - 1] = (x, y)
  
  edges = [[] for _ in range(n)]
  for i in range(n):
    for j in range(i + 1, n):
      ix, iy = buil_point[i]
      jx, jy = buil_point[j]
      cost = ((jx - ix) ** 2 + (jy - iy) ** 2) ** (1 / 2)
      if cost <= 50:
        edges[i].append((cost, j))
        edges[j].append((cost, i))

  m = int(input())
  for _ in range(m):
    s, g = map(int, input().split())
    costs = [INF] * n
    costs[s - 1] = 0
    paths = [[]] * n
    paths[s - 1] = [s - 1]
    que = []
    heappush(que, (0, [s - 1]))
    while que:
      dist, path = heappop(que)
      last = path[-1]
      for cost, to in edges[last]:
        if costs[to] > dist + cost:
          costs[to] = dist + cost
          paths[to] = path + [to]
          heappush(que, (dist + cost, path + [to]))
    
    if paths[g - 1]:
      print(*list(map(lambda x:x + 1, paths[g - 1])))
    else:
      print("NA")

Submission status: Accepted

Solution code:
# AOJ 0155 Spider Jin
# Python3 2018.6.23 bal4u
# 最短経路問題

MAXLEN = 50
MAX = 100
INF = 0x7ffffffffffffff
x = [0]*(MAX+2)
y = [0]*(MAX+2)
d = [[INF for y in range(MAX+2)] for x in range(MAX+2)]
dist = [INF]*(MAX+2)
pre = [0]*(MAX+2)
	
import heapq
def dijkstra(V, to, start, goal):
	global dist
	dist = [INF]*V
	Q = []
	dist[start] = 0
	heapq.heappush(Q, (0, start))
	while len(Q):
		t, s = heapq.heappop(Q)
		if s == goal: break
		if dist[s] < t: continue
		for e in range(1, V):
			if to[s][e] < INF:
				nt = t + to[s][e]**0.5
				if dist[e] > nt:
					dist[e] = nt
					heapq.heappush(Q, (nt, e))
					pre[e] = s
while 1:
	n = int(input())
	if n == 0: break
	for i in range(n):
		j, x_, y_ = map(int, input().split())
		x[j], y[j] = x_, y_
	n += 1
	d = [[INF for y in range(n)] for x in range(n)]
	for i in range(1, n):
		for j in range(1, n):
			dx, dy = x[i]-x[j], y[i]-y[j]
			dd = dx**2 + dy**2		
			if dd <= MAXLEN**2:
				d[i][j] = d[j][i] = dd
	m = int(input())
	for i in range(m):
		s, g = map(int, input().split())
		if s == g: print(s)
		else:
			dijkstra(n, d, s, g)
			if dist[g] == INF: print("NA")
			else:
				ans = []
				ans.append(g)
				while g != s:
					g = pre[g]
					ans.append(g)
				print(*ans[::-1])

Submission status: Accepted

Solution code:
# AOJ 0155 Spider Jin
# Python3 2018.6.23 bal4u
# 最短経路問題

MAXLEN = 50
MAX = 100
INF = 0x7ffffffffffffff
x = [0]*(MAX+2)
y = [0]*(MAX+2)
dist = [INF]*(MAX+2)
pre = [0]*(MAX+2)
	
import heapq
def dijkstra(V, to, start, goal):
	global dist
	dist = [INF]*V
	Q = []
	dist[start] = 0
	heapq.heappush(Q, (0, start))
	while len(Q):
		t, s = heapq.heappop(Q)
		if s == goal: break
		if dist[s] < t: continue
		for i in to[s]:
			e, cost = i
			nt = t + cost
			if dist[e] > nt:
				dist[e] = nt
				heapq.heappush(Q, (nt, e))
				pre[e] = s
while 1:
	n = int(input())
	if n == 0: break
	for i in range(n):
		j, x_, y_ = map(int, input().split())
		x[j], y[j] = x_, y_
	n += 1
	to = [[] for i in range(n)]
	for i in range(1, n):
		for j in range(1, n):
			dx, dy = x[i]-x[j], y[i]-y[j]
			dd = (dx**2 + dy**2)**0.5
			if dd <= MAXLEN:
				to[i].append((j, dd))
				to[j].append((i, dd))
	m = int(input())
	for i in range(m):
		s, g = map(int, input().split())
		if s == g: print(s)
		else:
			dijkstra(n, to, s, g)
			if dist[g] == INF: print("NA")
			else:
				ans = []
				ans.append(g)
				while g != s:
					g = pre[g]
					ans.append(g)
				print(*ans[::-1])

Submission status: Accepted

Solution code:
# AOJ 0155 Spider Jin
# Python3 2018.6.23 bal4u
# 最短経路問題

MAXLEN = 50
MAX = 100
INF = 0x7ffffffffffffff
x = [0]*(MAX+2)
y = [0]*(MAX+2)
dist = [INF]*(MAX+2)
pre = [0]*(MAX+2)
	
import heapq
def dijkstra(V, to, start, goal):
	global dist
	dist = [INF]*V
	Q = []
	dist[start] = 0
	heapq.heappush(Q, (0, start))
	while Q:
		t, s = heapq.heappop(Q)
		if s == goal: break
		if dist[s] < t: continue
		for e, cost in to[s]:
			nt = t + cost
			if dist[e] > nt:
				dist[e] = nt
				heapq.heappush(Q, (nt, e))
				pre[e] = s
while 1:
	n = int(input())
	if n == 0: break
	for i in range(n):
		j, x_, y_ = map(int, input().split())
		x[j], y[j] = x_, y_
	n += 1
	to = [[] for i in range(n)]
	for i in range(1, n):
		for j in range(i+1, n):
			dx, dy = x[i]-x[j], y[i]-y[j]
			cost = (dx**2 + dy**2)**0.5
			if cost <= MAXLEN:
				to[i].append((j, cost))
				to[j].append((i, cost))
	m = int(input())
	for i in range(m):
		s, g = map(int, input().split())
		if s == g: print(s)
		else:
			dijkstra(n, to, s, g)
			if dist[g] == INF: print("NA")
			else:
				ans = []
				ans.append(g)
				while g != s:
					g = pre[g]
					ans.append(g)
				print(*ans[::-1])

Submission status: Accepted

Solution code:
import math

def dijkstra(s):
    d=[inf]*n
    pi=[inf]*n
    visited=[False]*n
    d[s]=0
    pi[s]=-1
    while True:
        mincost=inf
        for i in range(n):
            if not visited[i] and d[i]<mincost:
                mincost=d[i]
                u=i
        if mincost==inf:
            break
        visited[u]=True
        for v in range(n):
            if visited[v] or L[u][v]==inf:
                continue
            if d[u]+L[u][v]<d[v]:
                d[v]=d[u]+L[u][v]
                pi[v]=u
    return pi

def routing(pi,g):
    ans=[]
    i=g
    while i!=-1:
        ans.append(i+1)
        i=pi[i]
    return ans[::-1]

inf=100000
while True:
    n=input()
    if n==0:break
    L=[[inf]*n for i in range(n)]
    for i in range(n):
        L[i][i]=0
    B=[map(int,raw_input().split()) for i in range(n)]
    for i in range(n-1):
        for j in range(i+1,n):
            ba,xa,ya=B[i]
            bb,xb,yb=B[j]
            ba,bb=ba-1,bb-1
            d=math.sqrt(pow(xa-xb,2)+pow(ya-yb,2))
            if d<=50:
                L[ba][bb]=L[bb][ba]=d
    m=input()
    for i in range(m):
        s,g=map(int,raw_input().split())
        s,g=s-1,g-1
        pi=dijkstra(s)
        if pi[g]==inf:
            print "NA"
        else:
            ans=routing(pi,g)
            print " ".join(map(str,ans))

Submission status: Accepted

Solution code:
import heapq

def dijkstra(G, s, g, n):
    cost = [10**9] * n
    cost[s] = 0
    pre = [-1] * n
    q = []
    heapq.heappush(q, (0, s))
    while q:
        d, to = heapq.heappop(q)
        if to == g:
            break
        if cost[to] < d:
            continue
        for n, dist in G[to]:
            nd = d + dist
            if cost[n] > nd:
                cost[n] = nd
                heapq.heappush(q, (nd, n))
                pre[n] = to

    if pre[g] == -1:
        return []
    ans = [g]
    cur = g
    while True:
        ans.append(pre[cur])
        cur = pre[cur]
        if cur == s or cur == -1:
            break

    return ans[::-1]

while True:
    n = int(input())
    if n == 0:
        break
    bldgs = [-1] * n
    G = [[] for _ in range(n)]
    for _ in range(n):
        b, x, y = map(int, input().strip().split(" "))
        bldgs[b-1] = (x, y)

    for i in range(n):
        x_i, y_i = bldgs[i]
        for j in range(i+1, n):
            x_j, y_j = bldgs[j]
            d = ((x_i-x_j)**2 + (y_i-y_j)**2) ** 0.5
            if d <= 50:
                G[i].append((j, d))
                G[j].append((i, d))
    m = int(input())
    for _ in range(m):
        s, g = map(int, input().strip().split(" "))
        ans = dijkstra(G, s-1, g-1, n)
        if ans == []:
            print("NA")
        else:
            print(" ".join([str(i+1) for i in ans]))

Submission status: Accepted

Solution code:
from math import sqrt
from heapq import heappush, heappop

while 1:
    N = int(input())
    if N == 0:
        break
    G = [[] for i in range(N)]
    P = [None]*N
    for i in range(N):
        b, x, y = map(int, input().split())
        P[b-1] = (x, y)

    for i in range(N):
        xi, yi = P[i]
        for j in range(i):
            xj, yj = P[j]
            dd = (xi - xj)**2 + (yi - yj)**2
            if dd <= 2500:
                d = sqrt(dd)
                G[i].append((j, d))
                G[j].append((i, d))

    INF = 10**18
    M = int(input())
    for i in range(M):
        s, t = map(int, input().split()); s -= 1; t -= 1

        que = [(0, s)]
        dist = [INF]*N
        prv = [0]*N
        dist[s] = 0
        while que:
            cost, v = heappop(que)
            if dist[v] < cost:
                continue
            for w, d in G[v]:
                if dist[v] + d < dist[w]:
                    dist[w] = r = dist[v] + d
                    heappush(que, (r, w))
                    prv[w] = v
        if dist[t] == INF:
            print("NA")
        else:
            ans = []
            v = t
            while v != s:
                ans.append(v+1)
                v = prv[v]
            ans.append(s+1)
            ans.reverse()
            print(*ans)

Submission status: Accepted

Solution code:
def solve():
    from sys import stdin
    from itertools import combinations
    from heapq import heappush, heappop
    f_i = stdin
    
    while True:
        n = int(f_i.readline())
        if n == 0:
            break
        
        bldgs = (tuple(map(int, f_i.readline().split())) for i in range(n))
        adj = [[] for i in range(n + 1)]
        for b1, b2 in combinations(bldgs, 2):
            n1, x1, y1 = b1
            n2, x2, y2 = b2
            d = (x1 - x2) ** 2 + (y1 - y2) ** 2
            if d <= 2500:
                d **= 0.5
                adj[n1].append((n2, d))
                adj[n2].append((n1, d))
        
        m = int(f_i.readline())
        for i in range(m):
            s, g = map(int, f_i.readline().split())
            pq = [[0, s]]
            isCalculated = [False] * (n + 1)
            distance = [250000] * (n + 1)
            distance[s] = 0
            
            while pq:
                path1 = heappop(pq)
                n1 = path1[-1]
                
                if n1 == g:
                    print(*path1[1:])
                    break
                
                if path1[0] > distance[n1]:
                    continue
                
                isCalculated[n1] = True
                
                for n2, d2 in adj[n1]:
                    if isCalculated[n2]:
                        continue
                    t_d = distance[n1] + d2
                    if t_d < distance[n2]:
                        distance[n2] = t_d
                        path2 = path1[:]
                        path2[0] = t_d
                        path2.append(n2)
                        heappush(pq, path2)
            else:
                print('NA')

solve()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0155

"""
import sys
from sys import stdin
from math import sqrt
from heapq import heappush, heappop
input = stdin.readline


def dijkstra(s, G):
    BLACK, GRAY, WHITE = 0, 1, 2
    node = len(G)
    d = [float('inf')] * node
    color = [WHITE] * node
    p = [-1] * node

    d[s] = 0
    while True:
        mincost = float('inf')
        # ??\??????????????§??????????????¨?????????????????????u???????????????
        for i in range(node):
            if color[i] != BLACK and d[i] < mincost:  # S????±???????????????????S??¨??\?¶?????????????????????????????????????????°???????????????????
                mincost = d[i]
                u = i  # u??????????????????ID
        if mincost == float('inf'):
            break
        color[u] = BLACK  # ?????????u???S????±???????????????´???
        for v in range(node):
            if color[v] != BLACK and G[u][v] != float('inf'):
                # v????????????????????????????????°??????S???????????£???u????????????????????????????????????????????°??????????????±??§??´??°??????
                if d[u] + G[u][v] < d[v]:
                    d[v] = d[u] + G[u][v]
                    p[v] = u
                    color[v] = GRAY
    return d, p


def main(args):
    while True:
        n = int(input())
        if n == 0:
            break

        G = [[float('inf')] * (n+1) for _ in range(n+1)]
        data = []
        for _ in range(n):
            b, x, y = map(int, input().split(' '))
            data.append([b, x, y])

        for i in range(n):
            b1, x1, y1 = data[i]
            for j in range(n):
                b2, x2, y2 = data[j]
                if b1 == b2:
                    continue
                d = sqrt((x1 - x2)**2 + (y1 - y2)**2)
                if d <= 50.0:
                    G[b1][b2] = d
                    G[b2][b1] = d

        m = int(input())
        for _ in range(m):
            s, g = map(int, input().split(' '))
            d, p = dijkstra(s, G)
            if d[g] == float('inf'):
                print('NA')
            else:
                path = [g]
                while p[g] != s:
                    path.append(p[g])
                    g = p[g]
                path.append(s)
                rev = path[::-1]
                print(' '.join(map(str, rev)))


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0155

"""
import sys
from sys import stdin
from math import sqrt
input = stdin.readline


def dijkstra(s, G):
    BLACK, GRAY, WHITE = 0, 1, 2
    node = len(G)
    d = [float('inf')] * 101
    color = [WHITE] * 101
    p = [-1] * 101

    d[s] = 0
    while True:
        mincost = float('inf')
        # ??\??????????????§??????????????¨?????????????????????u???????????????
        for i in range(node):
            if color[i] != BLACK and d[i] < mincost:  # S????±???????????????????S??¨??\?¶?????????????????????????????????????????°???????????????????
                mincost = d[i]
                u = i  # u??????????????????ID
        if mincost == float('inf'):
            break
        color[u] = BLACK  # ?????????u???S????±???????????????´???
        for v in range(node):
            if color[v] != BLACK and G[u][v] != float('inf'):
                # v????????????????????????????????°??????S???????????£???u????????????????????????????????????????????°??????????????±??§??´??°??????
                if d[u] + G[u][v] < d[v]:
                    d[v] = d[u] + G[u][v]
                    p[v] = u
                    color[v] = GRAY
    return d, p


def main(args):
    while True:
        n = int(input())
        if n == 0:
            break

        G = [[float('inf')] * (100+1) for _ in range(100+1)]
        data = []
        for _ in range(n):
            b, x, y = map(int, input().split(' '))
            data.append([b, x, y])

        for i in range(n):
            b1, x1, y1 = data[i]
            for j in range(n):
                b2, x2, y2 = data[j]
                if b1 == b2:
                    continue
                d = sqrt((x1 - x2)**2 + (y1 - y2)**2)
                if d <= 50.0:
                    G[b1][b2] = d
                    G[b2][b1] = d

        m = int(input())
        for _ in range(m):
            s, g = map(int, input().split(' '))
            d, p = dijkstra(s, G)
            if d[g] == float('inf'):
                print('NA')
            else:
                path = [g]
                while p[g] != s:
                    path.append(p[g])
                    g = p[g]
                path.append(s)
                rev = path[::-1]
                print(' '.join(map(str, rev)))


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0155

"""
import sys
from sys import stdin
from math import sqrt
input = stdin.readline


def dijkstra(s, G):
    BLACK, GRAY, WHITE = 0, 1, 2
    d = [float('inf')] * 101
    color = [WHITE] * 101
    p = [-1] * 101

    d[s] = 0
    while True:
        mincost = float('inf')
        # ??\??????????????§??????????????¨?????????????????????u???????????????
        for i in range(101):
            if color[i] != BLACK and d[i] < mincost:  # S????±???????????????????S??¨??\?¶?????????????????????????????????????????°???????????????????
                mincost = d[i]
                u = i  # u??????????????????ID
        if mincost == float('inf'):
            break
        color[u] = BLACK  # ?????????u???S????±???????????????´???
        for v in range(101):
            if color[v] != BLACK and G[u][v] != float('inf'):
                # v????????????????????????????????°??????S???????????£???u????????????????????????????????????????????°??????????????±??§??´??°??????
                if d[u] + G[u][v] < d[v]:
                    d[v] = d[u] + G[u][v]
                    p[v] = u
                    color[v] = GRAY
    return d, p


def main(args):
    while True:
        n = int(input())
        if n == 0:
            break

        G = [[float('inf')] * (100+1) for _ in range(100+1)]
        data = []
        for _ in range(n):
            b, x, y = map(int, input().split(' '))
            data.append([b, x, y])

        for i in range(n):
            b1, x1, y1 = data[i]
            for j in range(n):
                b2, x2, y2 = data[j]
                if b1 == b2:
                    continue
                d = sqrt((x1 - x2)**2 + (y1 - y2)**2)
                if d <= 50.0:
                    G[b1][b2] = d
                    G[b2][b1] = d

        m = int(input())
        for _ in range(m):
            s, g = map(int, input().split(' '))
            if s == g:
                print(s)
                continue
            d, p = dijkstra(s, G)
            if d[g] == float('inf'):
                print('NA')
            else:
                path = [g]
                while p[g] != s:
                    path.append(p[g])
                    g = p[g]
                path.append(s)
                rev = path[::-1]
                print(' '.join(map(str, rev)))


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0155

"""
import sys
from sys import stdin
from math import sqrt
input = stdin.readline


def dijkstra(s, G):
    BLACK, GRAY, WHITE = 0, 1, 2
    d = [float('inf')] * 101
    color = [WHITE] * 101
    p = [-1] * 101

    d[s] = 0
    while True:
        mincost = float('inf')
        # ??\??????????????§??????????????¨?????????????????????u???????????????
        for i in range(101):
            if color[i] != BLACK and d[i] < mincost:  # S????±???????????????????S??¨??\?¶?????????????????????????????????????????°???????????????????
                mincost = d[i]
                u = i  # u??????????????????ID
        if mincost == float('inf'):
            break
        color[u] = BLACK  # ?????????u???S????±???????????????´???
        for v in range(101):
            if color[v] != BLACK and G[u][v] != float('inf'):
                # v????????????????????????????????°??????S???????????£???u????????????????????????????????????????????°??????????????±??§??´??°??????
                if d[u] + G[u][v] < d[v]:
                    d[v] = d[u] + G[u][v]
                    p[v] = u
                    color[v] = GRAY
    return d, p


def main(args):
    while True:
        n = int(input())
        if n == 0:
            break

        G = [[float('inf')] * (100+1) for _ in range(100+1)]
        data = []
        for _ in range(n):
            b, x, y = map(int, input().split(' '))
            data.append([b, x, y])

        for i in range(n):
            b1, x1, y1 = data[i]
            for j in range(n):
                b2, x2, y2 = data[j]
                if b1 == b2:
                    continue
                d = sqrt((x1 - x2)**2 + (y1 - y2)**2)
                if d <= 50.0:
                    G[b1][b2] = d
                    G[b2][b1] = d

        m = int(input())
        for _ in range(m):
            s, g = map(int, input().split(' '))
            if s == g:
                print(s)
                continue
            if (not 1<= s <= 100) or (not 1<= g <= 100):
                print('NA')
                continue
            d, p = dijkstra(s, G)
            if d[g] == float('inf'):
                print('NA')
            else:
                path = [g]
                while p[g] != s:
                    path.append(p[g])
                    g = p[g]
                path.append(s)
                rev = path[::-1]
                print(' '.join(map(str, rev)))


if __name__ == '__main__':
    main(sys.argv[1:])