Problem p02889 (python)

Fixed submission:
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra

n, m, l = map(int, input().split())
abc = []
for _ in range(m):
    abc.append(map(int, input().split()))
q = int(input())
st = []
for _ in range(q):
    st.append(map(int, input().split()))

adjacency_matrix = np.zeros((n, n))
for a, b, c in abc:
    adjacency_matrix[a-1, b-1] = c
    adjacency_matrix[b-1, a-1] = c

shotest = dijkstra(adjacency_matrix)
fuel_once = np.where(shotest <= l, 1, 0)
fuel_times = dijkstra(fuel_once-np.eye(n))

for s, t in st:
    ans = fuel_times[s-1][t-1]
    if np.isinf(ans):
        print(-1)
    else:
        print(int(ans) - 1)

Buggy submission (verdict: Runtime Error):
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra

n, m, l = map(int, input().split())
abc = []
for _ in range(m):
    abc.append(map(int, input().split()))
q = int(input())
st = []
for _ in range(q):
    st.append(map(int, input().split()))

adjacency_matrix = np.zeros((n, n))
for a, b, c in abc:
    adjacency_matrix[a-1, b-1] = c
    adjacency_matrix[b-1, a-1] = c

shotest = dijkstra(adjacency_matrix)
fuel_once = np.where(shotest <= l, 1, 0)
fuel_times = dijkstra(fuel_once)

for s, t in st:
    ans = fuel_times[s-1][t-1]
    if np.isinf(ans):
        print(-1)
    else:
        print(int(ans) - 1)

Buggy submission (verdict: Runtime Error):
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra

n, m, l = map(int, input().split())
abc = []
for _ in range(m):
    abc.append(map(int, input().split()))
q = int(input())
st = []
for _ in range(q):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    st.append((s,t))

adjacency_matrix = np.zeros((n, n))
for (a, b, c) in abc:
    adjacency_matrix[a-1][b-1] = c
    adjacency_matrix[b-1][a-1] = c

shotest = dijkstra(adjacency_matrix)
fuel_once = np.where(shotest <= l, 1, 0)
fuel_times = dijkstra(fuel_once).astype(int)

for tap in st:
    s, t = tap
    if shotest[s][t] < np.inf:
        print(fuel_times[s][t] - 1)
    else:
        print(-1)

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra

n, m, l = map(int, input().split())
abc = []
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    abc.append([a, b, c])
    abc.append([b, a, c])
q = int(input())
st = []
for _ in range(q):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    st.append((s,t))


if m == 0:
    print(-1)
    sys.exit()

a, b, c = np.array(abc).T
adjacency_matrix = csr_matrix((c, (a,b)), (n, n))

shotest = dijkstra(adjacency_matrix)
fuel_once = np.where(shotest <= l, 1, 0)
fuel_times = dijkstra(fuel_once).astype(int)

for tap in st:
    s, t = tap
    if shotest[s][t] < np.inf:
        print(fuel_times[s][t] - 1)
    else:
        print(-1)

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra

n, m, l = map(int, input().split())
abc = []
for mi in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    abc.append([a, b, c])
    abc.append([b, a, c])
q = int(input())
st = []
for i in range(q):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    st.append((s,t))


if m == 0:
    print(-1)
    sys.exit()

a, b, c = np.array(abc).T
adjacency_matrix = csr_matrix((c, (a,b)), (n, n))

shotest = dijkstra(adjacency_matrix)
fuel_once = np.where(shotest <= l, 1, 0)
once_dijkstra = dijkstra(fuel_once)
fuel_times = np.where(once_dijkstra > 0, once_dijkstra - 1, once_dijkstra).astype(int)

for tap in st:
    s, t = tap
    print(fuel_times[s][t])

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra


def get_path(graph, s, t):
    if s == t:
        return [], [], 0
    else:
        path_cost, town = dijkstra(adjacency_matrix, return_predecessors=True, indices=s)
        path, costs, pre_cost = get_path(graph, s, town[t])
        path.append(town[t])
        costs.append(path_cost[t] - pre_cost)
        return path, costs, path_cost[t]


n, m, l = map(int, input().split())
abc = []
for mi in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    abc.append([a, b, c])
    abc.append([b, a, c])
q = int(input())
st = []
for i in range(q):
    s, t = map(int, input().split())
    st.append([s,t])


if m == 0:
    print(-1)
    sys.exit()

a, b, c = np.array(abc).T
adjacency_matrix = csr_matrix((c, (a,b)), (n, n))

for element in st:
    start, end = element[0]-1, element[1]-1
    _, costs, _ = get_path(adjacency_matrix, start, end)

    count = 0
    tmp_fuel = l
    for cost in costs:
        if tmp_fuel < cost:
            tmp_fuel = l
            count += 1
        tmp_fuel -= cost
    print(count)