Problem: There are N towns numbered 1 to N and M roads. The i-th road connects Town A_i and Town B_i bidirectionally and has a length of C_i.  Takahashi will travel between these towns by car, passing through these roads. The fuel tank of his car can contain at most L liters of fuel, and one liter of fuel is consumed for each unit distance traveled. When visiting a town while traveling, he can full the tank (or choose not to do so). Travel that results in the tank becoming empty halfway on the road cannot be done.  Process the following Q queries:    * The tank is now full. Find the minimum number of times he needs to full his tank while traveling from Town s_i to Town t_i. If Town t_i is unreachable, print -1.

Submission attempts (in order):
from collections import defaultdict
from scipy.sparse.csgraph import floyd_warshall

n, m, l = map(int, input().split())

graph = defaultdict(list)

for _ in range(m):
    a, b, c = map(int, input().split())
    graph[a].append((b, c))
    graph[b].append((a, c))


def dfs(u):
    dist = [-1] * (n + 1)
    stack = [u]
    dist[u] = 0
    while stack:
        u = stack.pop()
        for v, d in graph[u]:
            nxt = dist[u] + d
            if dist[v] == -1 or dist[v] > nxt:
                dist[v] = nxt
                stack.append(v)
    return dist


dist = [[0] * n for _ in range(n)]
for i in range(1, n + 1):
    dist[i - 1] = dfs(i)[1:]

dist = np.array(dist)

graph2 = np.full((n, n), np.inf)
graph2[dist <= l] = 1
dist = floyd_warshall(graph2, directed=False)
dist = dist.astype(int)

q = int(input())

ans = []
for _ in range(q):
    s, t = map(int, input().split())
    ans.append(dist[s - 1, t - 1] - 1)

print(*ans, sep="\n")

from collections import defaultdict
import sys
sys.setrecursionlimit(10**7)

n, m, l = map(int, input().split())

graph = defaultdict(list)

for _ in range(m):
    a, b, c = map(int, input().split())
    graph[a].append((b, c))
    graph[b].append((a, c))


def dfs(u, l, rest):
    dist = [-1] * (n + 1)
    stack = [u]
    dist[u] = 0
    while stack:
        u = stack.pop()
        tmp_rest = rest
        for v, d in graph[u]:
            rest = tmp_rest
            rest -= d
            if rest < 0:
                nxt = dist[u] + 1
                rest = l
            else:
                nxt = dist[u]
            if dist[v] == -1 or dist[v] > nxt:
                dist[v] = nxt
                stack.append(v)
    return dist


q = int(input())

ans = []
for _ in range(q):
    s, t = map(int, input().split())
    ans.append(dfs(s, l, l)[t])

print(ans, sep="\n")

from collections import defaultdict
sys.setrecursionlimit(10**7)

n, m, l = map(int, input().split())

graph = defaultdict(list)

for _ in range(m):
    a, b, c = map(int, input().split())
    graph[a].append((b, c))
    graph[b].append((a, c))


def dfs(u, l, rest):
    dist = [-1] * (n + 1)
    stack = [u]
    dist[u] = 0
    while stack:
        u = stack.pop()
        tmp_rest = rest
        for v, d in graph[u]:
            rest = tmp_rest
            rest -= d
            if rest < 0:
                nxt = dist[u] + 1
                rest = l
            else:
                nxt = dist[u]
            if dist[v] == -1 or dist[v] > nxt:
                dist[v] = nxt
                stack.append(v)
    return dist


q = int(input())

ans = []
for _ in range(q):
    s, t = map(int, input().split())
    ans.append(dfs(s, l, l)[t])

print(ans, sep="\n")

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

n, m, l = map(int, input().split())
INF = 1 << 60


dist = [[INF] * n for _ in range(n)]


for i in range(n):
    dist[i][i] = 0


for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    if c > l:
        continue
    dist[a][b] = c
    dist[b][a] = c


dist = floyd_warshall(csr_matrix(dist))


dist2 = np.full_like(dist, 1)
dist2[np.where(dist > l)] = INF


dist2 = floyd_warshall(dist2, directed=False)
dist2 = dist2.astype(int)


dist2[dist2 == INF] = 0

q = int(input())


ans = []
for _ in range(q):
    s, t = map(int, input().split())
    ans.append(dist2[s - 1, t - 1] - 1)

print(*ans, sep="\n")

Verdicts: Runtime Error

Wrong Answer

Runtime Error

Accepted