Problem p02889 (python)

Fixed submission:
import os
import sys

import numpy as np
from scipy.sparse.csgraph import floyd_warshall

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M, L = list(map(int, sys.stdin.readline().split()))
ABC = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]
Q = int(sys.stdin.readline())
S, T = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(Q)]))

D = np.full((N, N), 0)
for a, b, c in ABC:
    D[a - 1][b - 1] = c
    D[b - 1][a - 1] = c

floyd_warshall(D, directed=False, overwrite=True)
counts = floyd_warshall(D <= L, directed=False, unweighted=True)
S = np.array(S, dtype=int) - 1
T = np.array(T, dtype=int) - 1

ans = counts[S, T] - 1
ans[ans == np.inf] = -1
print(*ans.astype(int), sep='\n')

Buggy submission (verdict: Runtime Error):
import os
import sys

import numpy as np
from scipy.sparse.csgraph import dijkstra
from scipy.sparse.csgraph import floyd_warshall

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M, L = list(map(int, sys.stdin.readline().split()))
ABC = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]
Q = int(sys.stdin.readline())
S, T = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(Q)]))

D = np.full((N, N), 0)
for a, b, c in ABC:
    D[a - 1][b - 1] = c
    D[b - 1][a - 1] = c

floyd_warshall(D, directed=False, overwrite=True)
counts = dijkstra(D <= L, directed=False, unweighted=True)
S = np.array(S, dtype=int) - 1
T = np.array(T, dtype=int) - 1

ans = counts[S, T] - 1
ans[ans == np.inf] = -1
print(*ans.astype(int), sep='\n')

Buggy submission (verdict: Runtime Error):
import os
import sys

import numpy as np
from scipy.sparse.csgraph import dijkstra
from scipy.sparse.csgraph import floyd_warshall

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M, L = list(map(int, sys.stdin.readline().split()))
ABC = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]
Q = int(sys.stdin.readline())
S, T = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(Q)]))

D = np.full((N, N), IINF)
for a, b, c in ABC:
    D[a - 1][b - 1] = c
    D[b - 1][a - 1] = c

floyd_warshall(D, directed=False, overwrite=True)
counts = dijkstra(D <= L, directed=False, unweighted=True)
S = np.array(S, dtype=int) - 1
T = np.array(T, dtype=int) - 1

ans = counts[S, T] - 1
ans[ans == np.inf] = -1
print(*ans.astype(int), sep='\n')

Buggy submission (verdict: Runtime Error):
import os
import sys

import numpy as np
from scipy.sparse.csgraph import dijkstra

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M, L = list(map(int, sys.stdin.readline().split()))
ABC = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]
Q = int(sys.stdin.readline())
S, T = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(Q)]))


D = np.full((N, N), IINF)
for a, b, c in ABC:
    D[a - 1][b - 1] = c
    D[b - 1][a - 1] = c

# warshall floyd
for via in range(N):
    D = np.minimum(D, sum(np.meshgrid(D[via], D[via], sparse=True)))

counts = dijkstra(D <= L, directed=False)
S = np.array(S, dtype=int) - 1
T = np.array(T, dtype=int) - 1

ans = counts[S, T] - 1
ans[ans == np.inf] = -1
print(*ans.astype(int), sep='\n')