Problem p03634 (python)

Fixed submission:
#!usr/bim/python
#! -*- coding: utf-8 -*-

import numpy as np

n = raw_input()
n = int(n)

tree = []
for i in range(n):
    tree.append([])

for i in range(n-1):
    abc = raw_input()
    abc = abc.split()
    abc = map(int, abc)
    tree[abc[0]-1].append([abc[1]-1,abc[2]])
    tree[abc[1]-1].append([abc[0]-1,abc[2]])


qk = raw_input()
qk = qk.split()
q,k = map(int,qk)

question = []
for i in range(q):
    xy = raw_input()
    xy = xy.split()
    xy = map(int,xy)
    question.append(xy)

distance = []
for i in range(n):
    distance.append(-1)

distance[k-1] = 0

next_point = [k-1]

while 1:
    search_point = next_point
    next_point = []
    if len(search_point) == 0:
        break
    else:
        for i in search_point:
            for j in tree[i]:
                if distance[j[0]] == -1:
                    distance[j[0]] = distance[i] + j[1]
                    next_point.append(j[0])

for line in question:
    print distance[line[0]-1] + distance[line[1]-1]

Buggy submission (verdict: Runtime Error):
#!usr/bim/python
#! -*- coding: utf-8 -*-

import numpy as np

n = raw_input()
n = int(n)

tree = np.zeros([n,n],dtype=np.int64)
for i in range(n-1):
    abc = raw_input()
    abc = abc.split()
    abc = map(int, abc)
    tree[abc[0]-1,abc[1]-1] = abc[2]
    tree[abc[1]-1,abc[0]-1] = abc[2]

qk = raw_input()
qk = qk.split()
q,k = map(int,qk)

question = []
for i in range(q):
    xy = raw_input()
    xy = xy.split()
    xy = map(int,xy)
    question.append(xy)

distance = []
for i in range(n):
    distance.append(-1)

distance[k-1] = 0

while 1:
    status = 0
    for i in range(len(distance)):
        if distance[i] == -1:
            status = 1
        else:
            for j in range(n):
                if not tree[i][j] == 0 and distance[j] == -1:
                    distance[j] = distance[i] + tree[i][j]
    if status == 0:
        break

for line in question:
    ans = distance[line[0]-1] + distance[line[1]-1]
    print ans

Buggy submission (verdict: Runtime Error):
#!usr/bim/python
#! -*- coding: utf-8 -*-

import numpy as np

n = raw_input()
n = int(n)

tree = np.zeros([n,n],dtype=np.int64)
for i in range(n-1):
    abc = raw_input()
    abc = abc.split()
    abc = map(int, abc)
    tree[abc[0]-1,abc[1]-1] = abc[2]
    tree[abc[1]-1,abc[0]-1] = abc[2]

qk = raw_input()
qk = qk.split()
q,k = map(int,qk)

question = []
for i in range(q):
    xy = raw_input()
    xy = xy.split()
    xy = map(int,xy)
    question.append(xy)

distance = []
for i in range(n):
    distance.append(-1)

distance[k-1] = 0

while 1:
    status = 0
    for i in range(len(distance)):
        if distance[i] == -1:
            status = 1
        else:
            for j in range(n):
                if tree[i][j] == 0:
                    pass
                elif distance[j] == -1:
                    distance[j] = distance[i] + tree[i][j]
    if status == 0:
        break

for line in question:
    ans = distance[line[0]-1] + distance[line[1]-1]
    print ans

Buggy submission (verdict: Runtime Error):
#!usr/bim/python
#! -*- coding: utf-8 -*-

import numpy as np

n = raw_input()
n = int(n)

tree = np.zeros([n,n],dtype=np.int64)
for i in range(n-1):
    abc = raw_input()
    abc = abc.split()
    abc = map(int, abc)
    tree[abc[0]-1,abc[1]-1] = abc[2]
    tree[abc[1]-1,abc[0]-1] = abc[2]

qk = raw_input()
qk = qk.split()
q,k = map(int,qk)


distance = []
for i in range(n):
    distance.append(-1)

distance[k-1] = 0

while 1:
    for i in range(len(distance)):
        if distance[i] == -1:
            pass
        else:
            status = 0
            for j in range(n):
                if tree[i][j] == 0:
                    pass
                elif distance[j] == -1:
                    distance[j] = distance[i] + tree[i][j]
                if distance[j] == -1:
                    status = 1
    if status == 0:
        break

answers = []
for i in range(q):
    xy = raw_input()
    xy = xy.split()
    xy = map(int,xy)
    ans = distance[xy[0]-1] + distance[xy[1]-1]
    answers.append(ans)

for ans in answers:
    print ans