Problem p03634 (python)

Fixed submission:
from collections import *
from heapq import*
import sys
input=lambda:sys.stdin.readline()

d=defaultdict(list)
n=int(input())

for s in range(n-1):
    a,b,c=map(int,input().split())
    d[a].append([b,c])
    d[b].append([a,c])
#defaultdict(<class 'list'>, {1: [[2, 1], [3, 1]], 2: [[1, 1], [3, 3]], 3: [[1, 1], [2, 3]]})

def DIJKSTRA(point,d):
    cost      = {node:float("inf") for node in d.keys()}
    Q         = []  #頂点のリスト
    cost[point]=0  #開始地点のコストをゼロとする（当たり前
    heappush(Q,(point,cost[point]))
    while Q:
        prefnd,tmpC=heappop(Q)
        if cost[prefnd]<tmpC:
            continue
        for node,c in d[prefnd]:
            altC=c+tmpC
            if cost[node]>altC:
                cost[node]=altC
                heappush(Q,(node,altC))
    return cost

q,k=map(int,input().split())
y=DIJKSTRA(k,d)

for i in range(q):
    a,b=map(int,input().split())
    print(y[a]+y[b])

Buggy submission (verdict: Runtime Error):
#from numpy import*
from scipy.sparse.csgraph import shortest_path
from scipy.sparse import csr_matrix

from collections import* #defaultdict Counter deque appendleft
from fractions import gcd
from functools import* #reduce
from itertools import* #permutations("AB",repeat=2) combinations("AB",2) product("AB",2) groupby accumulate
from operator import mul
from bisect import* #bisect_left bisect_right
from heapq import* #heapify heappop heappushpop
from math import factorial,pi
from copy import deepcopy
import sys
#input=sys.stdin.readline  #危険！基本オフにしろ！
sys.setrecursionlimit(10**6)

def main():
    n=int(input())
    graph=[[0]*n for i in range(n)]
    for i in range(n-1):
        a,b,c=map(int,input().split())
        graph[a-1][b-1]=c
        graph[b-1][a-1]=c
    graph=csr_matrix(graph)
    d = shortest_path(csgraph=graph)
    q,k=map(int,input().split())
    for i in range(q):
        a,b=map(int,input().split())
        print(int(d[a-1][k-1]+d[k-1][b-1]))
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import collections as c
 
d=c.defaultdict(list)
n=int(input())
for s in range(n-1):
    a,b,c=map(int,input().split())
    d[a].append([b,c])
    d[b].append([a,c])
 
def DFS(point,d,cost,TFlist): #入力と同じindex系
    for s in d[point]:
        try:
            p=s[0]
            c=s[1]+cost[point]
            if TFlist[p-1]:
                continue
            cost[p]+=c
            TFlist[p-1]=1
            DFS(p,d,cost,TFlist)
        except RecursionError:
            continue
 
cost=[None]+[0 for i in range(n)]
q,k=map(int,input().split())
TFlist=[0 for i in range(n)]
TFlist[k-1]=1
DFS(k,d,cost,TFlist)
 
for t in range(q):
    a,b=map(int,input().split())
    print(cost[a]+cost[b])

Buggy submission (verdict: Runtime Error):
import collections as c
 
d=c.defaultdict(list)
n=int(input())
for s in range(n-1):
    a,b,c=map(int,input().split())
    d[a].append([b,c])
    d[b].append([a,c])
 
def DFS(point,d,cost,TFlist): #入力と同じindex系
    for s in d[point]:
        try:
            p=s[0]
            c=s[1]+cost[point]
            if TFlist[p-1]:
                continue
            cost[p]+=c
            TFlist[p-1]=1
            DFS(p,d,cost,TFlist)
        except KeyError:
            continue
 
cost=[None]+[0 for i in range(n)]
q,k=map(int,input().split())
TFlist=[0 for i in range(n)]
TFlist[k-1]=1
DFS(k,d,cost,TFlist)
 
for t in range(q):
    a,b=map(int,input().split())
    print(cost[a]+cost[b])

Buggy submission (verdict: Runtime Error):
import collections as c
 
d=c.defaultdict(list)
n=int(input())
for s in range(n-1):
    a,b,c=map(int,input().split())
    d[a].append([b,c])
    d[b].append([a,c])
 
def DFS(point,d,cost,TFlist): #入力と同じindex系
    for s in d[point]:
        try:
            p=s[0]
            c=s[1]+cost[point]
            if TFlist[p-1]:
                continue
            cost[p]+=c
            TFlist[p-1]=1
            DFS(p,d,cost,TFlist)
        except IndexError:
            continue
 
cost=[None]+[0 for i in range(n)]
q,k=map(int,input().split())
TFlist=[0 for i in range(n)]
TFlist[k-1]=1
DFS(k,d,cost,TFlist)
 
for t in range(q):
    a,b=map(int,input().split())
    print(cost[a]+cost[b])

Buggy submission (verdict: Runtime Error):
import collections as c
 
d=c.defaultdict(list)
n=int(input())
for s in range(n-1):
    a,b,c=map(int,input().split())
    d[a].append([b,c])
    d[b].append([a,c])
 
def DFS(point,d,cost,TFlist): #入力と同じindex系
    for s in d[point]:
        p=s[0]
        c=s[1]+cost[point]
        if TFlist[p-1]:
            continue
        cost[p]+=c
        TFlist[p-1]=1
        DFS(p,d,cost,TFlist)
 
cost=[None]+[0 for i in range(n)]
q,k=map(int,input().split())
TFlist=[0 for i in range(n)]
TFlist[k-1]=1
DFS(k,d,cost,TFlist)
 
for t in range(q):
    a,b=map(int,input().split())
    print(cost[a]+cost[b])

Buggy submission (verdict: Runtime Error):
import collections as c
 
d=c.defaultdict(list)
n=int(input())
for s in range(n-1):
    a,b,c=map(int,input().split())
    d[a].append([b,c])
    d[b].append([a,c])

def DFS(point,d,cost,TFlist): #入力と同じindex系
    for s in d[point]:
        p=s[0]
        c=s[1]+cost[point]
        if TFlist[p-1]:
            continue
        cost[p]+=c
        TFlist[p-1]=1
        DFS(p,d,cost,TFlist)
    return cost[point]

cost=[None]+[0 for i in range(n)]
q,k=map(int,input().split())
TFlist=[0 for i in range(n)]
TFlist[k-1]=1
DFS(k,d,cost,TFlist)

for t in range(q):
    a,b=map(int,input().split())
    print(cost[a]+cost[b])

Buggy submission (verdict: Runtime Error):
import collections as c

d=c.defaultdict(list)
n=int(input())
for s in range(n-1):
    a,b,c=map(int,input().split())
    d[a].append([b,c])
    d[b].append([a,c])
#print(d)

    
def DFS(point,d,cost,TFlist): #入力と同じindex系
    if all(TFlist):
        return cost
    for s in d[point]:
        p=s[0]
        c=s[1]+cost[point]
        if TFlist[p-1]:
            continue
        cost[p]+=c
        TFlist[p-1]=1
      #  print(cost,TFlist)
        DFS(p,d,cost,TFlist)
    return cost[point]

cost=[None]+[0 for i in range(n)]
q,k=map(int,input().split())
TFlist=[0 for i in range(n)]
TFlist[k-1]=1
DFS(k,d,cost,TFlist)

for t in range(q):
    a,b=map(int,input().split())
    print(cost[a]+cost[b])