Problem p02889 (python)

Fixed submission:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools

import queue
from collections import deque



def main():
    node_num, edge_num, max_tank = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(edge_num)]
    q_num = int(input())
    query = [list(map(int, input().split())) for i in range(q_num)]

    graph_data = defaultdict(set)
    dist_data = defaultdict(int)

    distance = [[10 ** 12 for i in range(node_num + 1)] for j in range(node_num + 1)]
    for i in range(1, node_num + 1):
        distance[i][i] = 0

    for i in range(edge_num):
        a, b, c = data[i]
        graph_data[a].add(b)
        graph_data[b].add(a)
        dist_data[(a, b)] = c
        dist_data[(b, a)] = c
        distance[a][b] = c
        distance[b][a] = c


    for k in range(1, node_num + 1):
        for i in range(1, node_num + 1):
            for j in range(1, node_num + 1):
                distance[i][j] = min(distance[i][k] + distance[k][j], distance[i][j])


    distance2 = [[10 ** 5 for i in range(node_num + 1)] for j in range(node_num + 1)]
    for i in range(1, node_num + 1):
        distance2[i][i] = 0

    for i in range(1, node_num + 1):
        for j in range(1, node_num + 1):
            if distance[i][j] <= max_tank:
                distance2[i][j] = 1


    for k in range(1, node_num + 1):
        for i in range(1, node_num + 1):
            for j in range(1, node_num + 1):
                distance2[i][j] = min(distance2[i][k] + distance2[k][j], distance2[i][j])

    for i in range(q_num):
        s, t = query[i]
        ans = distance2[s][t]
        if ans == 10 ** 5:
            print(-1)
        else:
            print(ans - 1)



if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools

import queue
from collections import deque



def main():
    node_num, edge_num, max_tank = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(edge_num)]
    q_num = int(input())
    query = [list(map(int, input().split())) for i in range(q_num)]

    graph_data = defaultdict(set)
    dist_data = defaultdict(int)

    for i in range(edge_num):
        a, b, c = data[i]
        graph_data[a].add(b)
        graph_data[b].add(a)
        dist_data[(a, b)] = c
        dist_data[(b, a)] = c

    ans_list = [[-1 for i in range(node_num + 1)] for j in range(node_num + 1)]

    for i in range(1, node_num + 1):
        ans_list[i][i] = (0, max_tank)

        count_all = node_num - 1

        check_ele = set()
        check_ele.add(i)
        for j in range(1, i):
            count_all -= 1
            ans_list[i][j] = ans_list[j][i]
            check_ele.add(j)
        aaa = []

        for bbb in check_ele:
            for next_ele in graph_data[bbb]:
                if ans_list[i][next_ele] != -1:
                    continue

                # print(ans_list[i][next_ele])
                count_data, nokori_data = ans_list[i][bbb]
                dist = dist_data[(bbb, next_ele)]
                if dist > max_tank:
                    continue
                if dist <= nokori_data:
                    heapq.heappush(aaa, (count_data, - (nokori_data - dist), next_ele))
                else:
                    heapq.heappush(aaa, (count_data + 1, - (max_tank - dist), next_ele))

        while len(aaa):
            count, nokori, now_ele = heapq.heappop(aaa)

            if ans_list[i][now_ele] != -1:
                continue

            nokori = -1 * nokori
            ans_list[i][now_ele] = (count, nokori)
            count_all -= 1

            if count_all == 0:
                break

            for next_ele in graph_data[now_ele]:
                if ans_list[i][next_ele] != -1:
                    continue
                dist = dist_data[(now_ele, next_ele)]
                if dist > max_tank:
                    continue
                if nokori < dist:
                    heapq.heappush(aaa, (count + 1, - (max_tank - dist), next_ele))
                else:
                    heapq.heappush(aaa, (count, - (nokori - dist), next_ele))

            for lll in range(1, node_num + 1):
                if ans_list[i][lll] == -1:
                    break
            else:
                break


    for i in range(q_num):
        s, t = query[i]
        ans = ans_list[s][t]
        if ans == -1:
            print(-1)
        else:
            print(ans[0])


if __name__ == '__main__':
    main()