Problem:


Score : 500 points

Problem Statement

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.

Constraints

All values in input are integers.

2 \leq N \leq 300

0 \leq M \leq \frac{N(N-1)}{2}

1 \leq L \leq 10^9

1 \leq A_i, B_i \leq N

A_i \neq B_i

\left(A_i, B_i\right) \neq \left(A_j, B_j\right) (if i \neq j)

\left(A_i, B_i\right) \neq \left(B_j, A_j\right) (if i \neq j)

1 \leq C_i \leq 10^9

1 \leq Q \leq N\left(N-1\right)

1 \leq s_i, t_i \leq N

s_i \neq t_i

\left(s_i, t_i\right) \neq \left(s_j, t_j\right) (if i \neq j)

Input

Input is given from Standard Input in the following format:

N M L
A_1 B_1 C_1
:
A_M B_M C_M
Q
s_1 t_1
:
s_Q t_Q

Output

Print Q lines.

The i-th line should contain the minimum number of times the tank needs to be fulled while traveling from Town s_i to Town t_i. If Town t_i is unreachable, the line should contain -1 instead.

Sample Input 1

3 2 5
1 2 3
2 3 3
2
3 2
1 3

Sample Output 1

0
1

To travel from Town 3 to Town 2, we can use the second road to reach Town 2 without fueling the tank on the way.

To travel from Town 1 to Town 3, we can first use the first road to get to Town 2, full the tank, and use the second road to reach Town 3.

Sample Input 2

4 0 1
1
2 1

Sample Output 2

-1

There may be no road at all.

Sample Input 3

5 4 4
1 2 2
2 3 2
3 4 3
4 5 2
20
2 1
3 1
4 1
5 1
1 2
3 2
4 2
5 2
1 3
2 3
4 3
5 3
1 4
2 4
3 4
5 4
1 5
2 5
3 5
4 5

Sample Output 3

0
0
1
2
0
0
1
2
0
0
0
1
1
1
0
0
2
2
1
0



Sample input:
3 2 5
1 2 3
2 3 3
2
3 2
1 3


Sample output:
0
1

Submission status: Wrong Answer

Solution code:
import sys
from copy import deepcopy


INF = 10 ** 3
S = sys.stdin.readlines()
N, M, L = map(int, S[0].split())

dp = [[INF] * N for _ in range(N)]

for i in range(1, M + 1):
    A, B, C = map(int, S[i].split())
    dp[A - 1][B - 1] = C
    dp[B - 1][A - 1] = C
for i in range(N):
    dp[i][i] = 0
dp2 = deepcopy(dp)

for k in range(N):
    for i in range(N):
        for j in range(N):
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])


for i in range(N):
    for j in range(N):
        here = dp[i][j]
        dp2[i][j] = 1 if here <= L else here


for k in range(N):
    for i in range(N):
        for j in range(N):
            dp2[i][j] = min(dp2[i][j], dp2[i][k] + dp2[k][j])


Q = int(S[M + 1])
for i in range(M + 2, M + 2 + Q):
    s, t = map(int, S[i].split())
    refuel = dp2[s - 1][t - 1] - 1
    if refuel > N:
        print(-1)
    else:
        print(refuel)

Submission status: Wrong Answer

Solution code:
import sys
from copy import deepcopy


INF = 10 ** 3
S = sys.stdin.readlines()
N, M, L = map(int, S[0].split())

dp = [[INF] * N for _ in range(N)]

for i in range(1, M + 1):
    A, B, C = map(int, S[i].split())
    dp[A - 1][B - 1] = C
    dp[B - 1][A - 1] = C
for i in range(N):
    dp[i][i] = 0
dp2 = deepcopy(dp)

for k in range(N):
    for i in range(N):
        for j in range(N):
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])


for i in range(N):
    for j in range(N):
        here = dp[i][j]
        dp2[i][j] = 1 if here <= L else here


for k in range(N):
    for i in range(N):
        for j in range(N):
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])


Q = int(S[M + 1])
for i in range(M + 2, M + 2 + Q):
    s, t = map(int, S[i].split())
    refuel = dp[s - 1][t - 1] - 1
    if refuel > N:
        print(-1)
    else:
        print(refuel)

Submission status: Wrong Answer

Solution code:
import sys


INF = 10 ** 3
S = sys.stdin.readlines()
N, M, L = map(int, S[0].split())

dp = [[INF] * N for _ in range(N)]
for i in range(1, M + 1):
    A, B, C = map(int, S[i].split())
    dp[A - 1][B - 1] = C
    dp[B - 1][A - 1] = C
for i in range(N):
    dp[i][i] = 0


for k in range(N):
    for i in range(N):
        for j in range(N):
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])


for i in range(N):
    for j in range(N):
        here = dp[i][j]
        dp[i][j] = 1 if dp[i][j] <= L else here


for k in range(N):
    for i in range(N):
        for j in range(N):
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])


Q = int(S[M + 1])
for i in range(M + 2, M + 2 + Q):
    s, t = map(int, S[i].split())
    refuel = dp[s - 1][t - 1] - 1
    if refuel > N:
        print(-1)
    else:
        print(refuel)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)


S = sys.stdin.readlines()
N, M, L = map(int, S[0].split())
path = [[] for _ in range(N)]
for i in range(1, M + 1):
    A, B, C = map(int, S[i].split())
    path[A - 1].append((B - 1, C))
    path[B - 1].append((A - 1, C))
Q = int(S[M + 1])
query = [None] * Q
for i in range(M + 2, M + 2 + Q):
    query[i - (M + 2)] = tuple(map(int, S[i].split()))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)


S = sys.stdin.readlines()
N, M, L = map(int, S[0].split())
path = [[] for _ in range(N)]
for i in range(1, M + 1):
    A, B, C = map(int, S[i].split())
    path[A - 1].append((B - 1, C))
    path[B - 1].append((A - 1, C))
Q = int(S[M + 1])
query = [None] * Q
for i in range(M + 2, M + 2 + Q):
    query[i - (M + 2)] = tuple(map(int, S[i].split()))


def dfs(s, t, fuel):
    global L
    isGoal = False
    refuel = 301
    if s == t:
        isGoal |= True
        refuel = 0
    else:
        for ns, c in [x for x in path[s] if track[x[0]] == 0]:
            track[ns] = 1
            if fuel >= c:
                x, y = dfs(ns, t, fuel - c)
                isGoal |= x
                if x:
                    refuel = min(refuel, y)
            elif L >= c:
                x, y = dfs(ns, t, L - c)
                isGoal |= x
                if x:
                    refuel = min(refuel, y + 1)
    return isGoal, refuel


for s, t in query:
    s -= 1
    t -= 1
    track = [0] * N
    track[s] = 1
    isGoal, refuel = dfs(s, t, L)
    if isGoal:
        print(refuel)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)


N, M, L = map(int, input().split())
path = [[] for _ in range(N)]
for i in range(M):
    A, B, C = map(int, input().split())
    path[A - 1].append((B - 1, C))
    path[B - 1].append((A - 1, C))
Q = int(input())
query = [tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(Q)]


def dfs(s, t, fuel):
    global L
    isGoal = False
    refuel = 301
    if s == t:
        isGoal |= True
        refuel = 0
    else:
        for ns, c in [x for x in path[s] if track[x[0]] == 0]:
            track[ns] = 1
            if fuel >= c:
                x, y = dfs(ns, t, fuel - c)
                isGoal |= x
                if x:
                    refuel = min(refuel, y)
            elif L >= c:
                x, y = dfs(ns, t, L - c)
                isGoal |= x
                if x:
                    refuel = min(refuel, y + 1)
    return isGoal, refuel


for s, t in query:
    track = [0] * N
    track[s] = 1
    isGoal, refuel = dfs(s, t, L)
    if isGoal:
        print(refuel)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

N, M, L = map(int, input().split())
path = [[] for _ in range(N)]
for i in range(M):
    A, B, C = map(int, input().split())
    path[A - 1].append((B - 1, C))
    path[B - 1].append((A - 1, C))
Q = int(input())
query = [tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(Q)]


def dfs(s, t, fuel):
    global L
    if s == t:
        return 0
    else:
        can = []
        for ns, c in [x for x in path[s] if track[x[0]] == 0]:
            track[ns] = 1
            if L < c:
                can.append(-1)
            elif fuel >= c:
                can.append(dfs(ns, t, fuel - c))
            else:
                can.append(1 + dfs(ns, t, L - c))
        can.sort()
        m = bisect_left(can, 0)
        if m == len(can):
            return -1
        else:
            return can[m]


for s, t in query:
    track = [0] * N
    track[s] = 1
    print(dfs(s, t, L))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, m, l = map(int, input().split())
graph = [[] for i in range(N+1)]

INF = 2 ** 31 - 1
d = [[INF for i in range(N+1)] for j in range(N+1)]
for i in range(N+1):
    d[i][i] = 0
for i in range(m):
    a, b, c = map(int, input().split())
    d[a][b] = c
    d[b][a] = c


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

d2 = [[INF for i in range(N+1)] for j in range(N+1)]
for i in range(N+1):
    d[i][i] = 0

for i in range(N+1):
    for j in range(N+1):
        if d[i][j] <= l:
            d2[i][j] = 1

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

"""
q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    if d2[s][t] == INF:
        print(-1)
    else:
        print(d2[s][t] - 1)
"""

Submission status: Wrong Answer

Solution code:
import sys
import math
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9323372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res

N,M,L=LI()

DIST=[[(float('inf'), 0, 0) for _ in range(N+1)] for _ in range(N+1)]
for i in range(M):
    a,b,c = LI()
    if c <= L:
        DIST[a][b] = (.0, c, c)
        DIST[b][a] = (.0, c, c)

for k in range(1, N+1):
    for i in range(1, N+1):
        for j in range(1, N+1):
            cnt1, dist_bef1, dist_aft1 = DIST[i][j]
            cnt2, dist_bef2, dist_aft2 = DIST[i][k]
            cnt3, dist_bef3, dist_aft3 = DIST[k][j]
            new_cnt = cnt2 + cnt3 + (dist_aft2 + dist_bef3 > L)
            if cnt1 > new_cnt:
                DIST[i][j] = (new_cnt, dist_bef2, dist_aft3)

for i in range(II()):
    st, en = LI()
    if DIST[st][en][0] != float('inf'):
        print(int(DIST[st][en][0]))
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    def input():
        return sys.stdin.readline()

    N, M, L = map(int, input().split())
    D = [[float('inf')]*(N+1) for _ in range(N+1)]
    Ans = [[[float('inf'),0] for i in range(N+1)] for _ in range(N+1)]
    from collections import defaultdict
    G = defaultdict(list)

    import heapq
#heapq.heappush(heap, item)
#heapq.heappop(heap)
 
    for i in range(N+1):
        D[i][i] = 0
        Ans[i][i] = [0,L]

    for _ in range(M):
        A,B,C = map(int, input().split())
        if C <= L:
            D[A][B]=C
            D[B][A]=C
            G[A].append(B)
            G[B].append(A)

    for i in range(1,N+1):
        heap = []
        heapq.heappush(heap, [0,L,i])
        visited = [False]*(N+1)
        while heap != []:
            s = heapq.heappop(heap)
            if not visited[s[2]]:
                visited[s[2]] = True
                for j in G[s[2]]:
                    if Ans[i][s[2]][1] >= D[s[2]][j]:
                        tmp = [Ans[i][s[2]][0], Ans[i][s[2]][1] - D[s[2]][j]]
                    else:
                        tmp = [Ans[i][s[2]][0]+1, L - D[s[2]][j]]
                    if (Ans[i][j][0] > tmp[0]) or (Ans[i][j][0] == tmp[0] and Ans[i][j][1] < tmp[1]):
                        Ans[i][j] = tmp
                        heapq.heappush(heap,[tmp[0], tmp[1], j])
                
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        if Ans[s][t][0] != float('inf'):
            print(Ans[s][t][0])
        else:
            print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    def input():
        return sys.stdin.readline()

    N, M, L = map(int, input().split())
    D = [[float('inf')]*(N+1) for _ in range(N+1)]
    Ans = [[[float('inf'),0] for i in range(N+1)] for _ in range(N+1)]
    from collections import defaultdict
    G = defaultdict(list)

    import heapq
#heapq.heappush(heap, item)
#heapq.heappop(heap)

    for i in range(N+1):
        D[i][i] = 0
        Ans[i][i] = [0,L]

    for _ in range(M):
        A,B,C = map(int, input().split())
        if C <= L:
            D[A][B]=C
            D[B][A]=C
            G[A].append(B)
            G[B].append(A)

    for i in range(1,N+1):
        heap = []
        heapq.heappush(heap, [0,L,i])
        visited = [False]*(N+1)
        while heap != []:
            s = heapq.heappop(heap)
            if not visited[s[2]]:
                visited[s[2]] = True
                for j in G[s[2]]:
                    if not visited[j]:
                        if Ans[i][s[2]][1] >= D[s[2]][j]:
                            tmp = [Ans[i][s[2]][0], Ans[i][s[2]][1] - D[s[2]][j]]
                        else:
                            tmp = [Ans[i][s[2]][0]+1, L - D[s[2]][j]]
                        if (Ans[i][j][0] > tmp[0]) or (Ans[i][j][0] == tmp[0] and Ans[i][j][1] < tmp[1]):
                            Ans[i][j] = tmp
                            heapq.heappush(heap,[tmp[0], tmp[1], j])
                
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        if Ans[s][t][0] != float('inf'):
            print(Ans[s][t][0])
        else:
            print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    def input():
        return sys.stdin.readline()

    N, M, L = map(int, input().split())
    D = [[float('inf')]*(N+1) for _ in range(N+1)]
    Ans = [[[float('inf'),0] for i in range(N+1)] for _ in range(N+1)]
    from collections import defaultdict
    G = defaultdict(list)

    import heapq
#heapq.heappush(heap, item)
#heapq.heappop(heap)

    for i in range(N+1):
        D[i][i] = 0
        Ans[i][i] = [0,L]

    for _ in range(M):
        A,B,C = map(int, input().split())
        if C <= L:
            D[A][B]=C
            D[B][A]=C
            G[A].append(B)
            G[B].append(A)

    for i in range(1,N+1):
        heap = []
        heapq.heappush(heap, [0,L,i])
        visited = [False]*(N+1)
        while heap != []:
            s = heapq.heappop(heap)
            visited[s[2]] = True
            for j in G[s[2]]:
                if not visited[j]:
                    if Ans[i][s[2]][1] >= D[s[2]][j]:
                        tmp = [Ans[i][s[2]][0], Ans[i][s[2]][1] - D[s[2]][j]]
                    else:
                        tmp = [Ans[i][s[2]][0]+1, L - D[s[2]][j]]
                    if (Ans[i][j][0] > tmp[0]) or (Ans[i][j][0] == tmp[0] and Ans[i][j][1] < tmp[1]):
                        Ans[i][j] = tmp
                        heapq.heappush(heap,[tmp[0], tmp[1], j])
                
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        if Ans[s][t][0] != float('inf'):
            print(Ans[s][t][0])
        else:
            print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from heapq import heappop, heappush


class Graph(object):
    """
    隣接リストによる有向グラフ
    """
    # コンストラクタ
    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, src, dst, weight=1):  # デフォルトの距離は1
        self.graph[src].append((dst, weight))

    def get_nodes(self):
        return self.graph.keys()


class Dijkstra(object):
    """
    ダイクストラ法 (二分ヒープ)による最短経路探索
    計算量: O((E+V)logV)
    """

    # コンストラクタ, これで生成されるのはDijkstraクラスのオブジェクト
    # graph: Graphクラスのオブジェクト, start: 始点のindex
    def __init__(self, graph, start):
        self.g = graph.graph

        # startノードからの最短距離を格納するインスタンス変数dist
        # startノードは0, それ以外は無限大で初期化
        self.dist = defaultdict(lambda: float('inf'))
        self.dist[start] = 0

        # そのノードまでの最短経路を定義する際、どのノードから来たのかを記録する
        self.prev = defaultdict(lambda: None)

        # startノードをキューに入れる
        self.Q = []
        # このヒープの要素はタプルにできる
        # 追跡したい主レコード(ノードのIndex)とは別にタスクの優先度のような比較値を指定するときに便利
        heappush(self.Q, (self.dist[start], start))  # そのノードまでの仮の最短距離, そのノードのIndex
        # 本当の最短距離はself.distに格納する
        while self.Q:
            # 距離が最小であるものをキューから取り出す
            dist_u, u = heappop(self.Q)
            if self.dist[u] < dist_u:  # 仮の距離より短い距離が正式に求まっているならスルー
                # 待ち行列に追加はされたものの、出番が来る前に自分より良い経路が求まっている場合がある
                continue
            for v, weight in self.g[u]:  # 対象のノードに隣接しているノードに対して操作を行う
                alt = dist_u + weight  # dist_uはstartのノードからの距離
                if self.dist[v] > alt:
                    self.dist[v] = alt
                    self.prev[v] = u
                    heappush(self.Q, (alt, v))  # 更新された仮の最短経路, ノードのIndex

    def shortest_distance(self, goal):
        """
        startノードからgoalノードまでの最短距離
        """
        return self.dist[goal]

    def shortest_path(self, goal):
        """
        startノードからgoalノードまでの最短経路
        """
        path = []
        node = goal
        while node is not None:  # startノードはprevがNoneであるからこの条件で良い
            path.append(node)
            node = self.prev[node]
        return path[::-1]  # 逆順にして返す


N, M, L = map(int, input().split())
graph = Graph()
dist = {}
for _ in range(M):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    if c > L:
        continue
    graph.add_edge(a, b, c)
    graph.add_edge(b, a, c)
    dist[(a, b)] = c
    dist[(b, a)] = c

Q = int(input())
for _ in range(Q):
    s, t = map(lambda x: int(x)-1, input().split())
    instance = Dijkstra(graph, s)
    # print(instance.shortest_distance(t)//len(instance.shortest_path(t))-1)
    count = 0
    energy = L
    path = instance.shortest_path(t)
    now = s
    if len(path) == 1:
        print(-1)
        continue
    for i in range(1, len(path)):
        if energy >= dist[(now, path[i])]:
            energy -= dist[(now, path[i])]
        else:
            energy = L
            count += 1
            energy -= dist[(now, path[i])]
        now = path[i]

    print(count)

Submission status: Wrong Answer

Solution code:
def warshall_floyd(v_count: int, matrix: list) -> list:
    """ ワーシャルフロイド
    v_count: 頂点数
    matrix: 隣接行列(到達不能はfloat("inf"))
    """
    for i in range(v_count):
        for j, c2 in enumerate(row[i] for row in matrix):
            for k, (c1, c3) in enumerate(zip(matrix[j], matrix[i])):
                if c1 > c2+c3:
                    matrix[j][k] = c2+c3
    return matrix


INF = 10 ** 16
n, m, l = map(int, input().split())
mat = [[INF] * n for _ in range(n)]
for i in range(n):
    mat[i][i] = 0
for _ in range(m):
    a, b, c = map(int, input().split())
    mat[a - 1][b - 1] = c
    mat[b - 1][a - 1] = c
mat = warshall_floyd(n, mat)
fuel_mat = [[INF] * n for _ in range(n)]
for i in range(n):
    for j in range(n):
        if i == j:
            fuel_mat[i][j] = 0
        elif mat[i][j] <= l:
            fuel_mat[i][j] = 1
fuel_mat = warshall_floyd(n, fuel_mat)
q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    d = fuel_mat[s - 1][t - 1] - 1
    if d == INF:
        print(-1)
    else:
        print(d)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,l = map(int,input().split())
E = [[] for i in range(n+1)]
for i in range(m):
    a,b,c = map(int,input().split())
    E[a].append((b,c))
    E[b].append((a,c))

def bfs(s,g):
    d = [-1]*(n+1)
    d[s] = 0
    t = 0
    li = l
    q = deque([])
    q.append((s,t,li))
    while q:
        i,j,k = q.popleft()
        if i == g:
            continue
        for a,c in E[i]:
            
            if d[a] == -1:
                if k >= c:
                    k1 = k-c
                    d[a] = d[i]
                    q.append((a,t,k1))
                
                elif l >= c > k:
                 
                    
                    k1 = l-c
                    d[a] = d[i]+1
                    q.append((a,t+1,k1))
               
    return d[g]

x = int(input())
ans = []
for i in range(x):
    o,p = map(int,input().split())
    ans.append(bfs(o,p))
for i in ans:
	print(i)

Submission status: Wrong Answer

Solution code:
def main():
    [N, M, L] = [int(x) for x in input().split()]
    edge_list = [[] for _ in range(N+1)]
    for _ in range(M):
        [A, B, C] = [int(x) for x in input().split()]
        edge_list[A].append((B, C))
        edge_list[B].append((A, C))

    Q = int(input())
    st_list = []
    for _ in range(Q):
        [s, t] = [int(x) for x in input().split()]
        st_list.append((s, t))

    # ダイクストラ法により最短経路を求める
    for st in st_list:
        start = st[0]
        goal = st[1]
        unsearched_nodes = list(range(N+1))
        distance = [10**10 for _ in range(N+1)]
        distance[start] = 0
        previous_nodes = [-10**10] * (N+1)  # 最短経路でそのノードのひとつ前に到達するノードのリスト
        previous_nodes[start] = -1
        target_min_index = start

        while len(unsearched_nodes) > 1:
            # ターゲットになるノードからのびるエッジのリスト
            for edge in edge_list[target_min_index]:
                if edge[1] > L:
                    continue
                if distance[target_min_index] + edge[1] < distance[edge[0]]:
                    distance[edge[0]] = distance[target_min_index] + edge[1]
                    previous_nodes[edge[0]] = target_min_index
            # 最少経路が確定したノードを探して取り除く
            target_min_index = -1
            posible_min_distance = 10**10
            for node_index in unsearched_nodes:
                if posible_min_distance > distance[node_index]:
                    posible_min_distance = distance[node_index]
                    target_min_index = node_index
            if target_min_index == -1:
                print(-1)
                break
            unsearched_nodes.remove(target_min_index)
            if target_min_index == goal:
                # print('res', distance[goal], previous_nodes)
                break

        if target_min_index == -1:
            continue

        pass_node_list = []
        previous_node = goal
        while previous_node != -1:
            pass_node_list.append(previous_node)
            previous_node = previous_nodes[previous_node]
        pass_node_list = list(reversed(pass_node_list))
        # print(pass_node_list)

        fuel = L
        charge_num = 0
        for idx in range(len(pass_node_list)-1):
            for edge in edge_list[pass_node_list[idx]]:
                if edge[0] == pass_node_list[idx+1]:
                    if fuel >= edge[1]:
                        fuel -= edge[1]
                    else:
                        fuel = L - edge[1]
                        charge_num += 1
        print(charge_num)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

"""
"""

def warshall_floyd(d):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

n,m,l = LI()

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

for i in range(n):
    dist[i][i] = 0
    dist2[i][i] = 0
for _ in range(m):
    a,b,c = LI()
    dist[a-1][b-1] = c
    dist[b-1][a-1] = c

dist = warshall_floyd(dist)

for i in range(n):
    for j in range(n):
        if dist[i][j] > l:
            dist2[i][j] = inf
        else:
            dist2[i][j] = 1

dist2 = warshall_floyd(dist2)
q = I()
ans = []
for _ in range(q):
    s,t = LI()
    if dist2[s-1][t-1] == inf:
        ans.append(-1)
    else:
        ans.append(dist2[s-1][t-1]-1)
print(*ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

"""
"""

def warshall_floyd(d):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

n,m,l = LI()

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

for i in range(n):
    dist[i][i] = 0
    dist2[i][i] = 0
for _ in range(m):
    a,b,c = LI()
    dist[a-1][b-1] = c
    dist[b-1][a-1] = c

dist = warshall_floyd(dist)
print(dist)
for i in range(n):
    for j in range(n):
        if dist[i][j] > l:
            dist2[i][j] = inf
        else:
            dist2[i][j] = 1

dist2 = warshall_floyd(dist2)
q = I()
ans = []
for _ in range(q):
    s,t = LI()
    if dist2[s-1][t-1] == inf:
        ans.append(-1)
    else:
        ans.append(dist2[s-1][t-1]-1)
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
#道がない場合、infで初期化したい
inf = 10 ** 13 + 1
#input
n, m, l = list(map(int, input().split()))
way = [[inf for i in range(n)] for j in range(n)]
oil = [[inf for i in range(n)] for j in range(n)]

for i in range(m):
    a, b, c, = list(map(int, input().split()))
    way[a - 1][b - 1] = c
    way[b - 1][a - 1] = c

q = int(input())

query = []

for i in range(q):
    query.append(list(map(int, input().split())))

#ワーシャルフロイド法を使い、各道への最短経路を調べる。計算量はO(N**3)
for i in range(n):
    for j in range(n):
        for k in range(n):
            if i == k:
                way[i][k] = 0
                continue
            way[i][k] = min(way[i][k],way[i][j] + way[j][k])
            
#oilの補給できる回数もワーシャルフロイドで求める。まず、一補給でいけるところに辺を張る
for i in range(n):
    for j in range(n):
        if i == j:
            oil[i][j] = 0
        if way[i][j] <= l:
            oil[i][j] = oil[j][i] = 1
#の後でワーシャルフロイド
for i in range(n):
    for j in range(n):
        for k in range(n):
            if i == k:
                oil[i][k] = 0
                continue
            oil[i][k] = min(oil[i][k],oil[i][j] + oil[j][k])

for i in range(q):
    ans = oil[query[i][0] - 1][query[i][1] - 1] - 1
    if way[query[i][0] - 1][query[i][1] - 1] == inf or oil[query[i][0] - 1][query[i][1] - 1] == inf:
        ans = -1
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
#道がない場合、infで初期化したい
inf = 10 ** 13 + 1
#input
n, m, l = list(map(int, input().split()))
way = [[inf for i in range(n)] for j in range(n)]
oil = [[inf for i in range(n)] for j in range(n)]

for i in range(m):
    a, b, c, = list(map(int, input().split()))
    way[a - 1][b - 1] = c
    way[b - 1][a - 1] = c

q = int(input())

query = []

for i in range(q):
    query.append(list(map(int, input().split())))

#ワーシャルフロイド法を使い、各道への最短経路を調べる。計算量はO(N**3)
for i in range(n):
    for j in range(n):
        for k in range(n):
            if i == k:
                way[i][k] = 0
                continue
            way[i][k] = min(way[i][k],way[i][j] + way[j][k])
            
#oilの補給できる回数もワーシャルフロイドで求める。まず、一補給でいけるところに辺を張る
for i in range(n):
    for j in range(n):
        if way[i][j] <= l:
            oil[i][j] = oil[j][i] = 1
#の後でワーシャルフロイド
for i in range(n):
    for j in range(n):
        for k in range(n):
            if i == k:
                oil[i][k] = 0
                continue
            oil[i][k] = min(oil[i][k],oil[i][j] + oil[j][k])

for i in range(q):
    ans = oil[query[i][0] - 1][query[i][1] - 1] - 1
    if way[query[i][0] - 1][query[i][1] - 1] == inf:
        ans = -1
    print(ans)

Submission status: Wrong Answer

Solution code:

#道がない場合、infで初期化したい
inf = 10 ** 9 + 1
#input
n, m, l =list(map(int, input().split()))
way = [[inf for i in range(n)] for j in range(n)]
oil = [[inf for i in range(n)] for j in range(n)]

for i in range(m):
    a, b, c, = list(map(int, input().split()))
    way[a - 1][b - 1] = c
    way[b - 1][a - 1] = c

q = int(input())

query = []

for i in range(q):
    query.append(list(map(int, input().split())))

#ワーシャルフロイド法を使い、各道への最短経路を調べる。計算量はO(N**3)
for i in range(n):
    for j in range(n):
        for k in range(n):
            way[i][k] = min(way[i][k],way[i][j] + way[j][k])
            if i == k:
                way[i][k] = 0
#oilの補給できる回数もワーシャルフロイドで求める。まず、一補給でいけるところに辺を張る
for i in range(n):
    for j in range(n):
        if way[i][j] <= l:
            oil[i][j] = oil[j][i] = 1
#の後でワーシャルフロイド
for i in range(n):
    for j in range(n):
        for k in range(n):
            oil[i][k] = min(oil[i][k],oil[i][j] + oil[j][k])
            if i == k:
                oil[i][k] = 0

for i in range(q):
    ans = oil[query[i][0] - 1][query[i][1] - 1] - 1
    if way[query[i][0] - 1][query[i][1] - 1] == inf:
        ans = -1
    print(ans)

Submission status: Wrong Answer

Solution code:
import math
from scipy.sparse.csgraph import shortest_path
#道がない場合、infで初期化したい
inf = 10 ** 9 + 1
#input
n, m, l =list(map(int, input().split()))
way = [[inf for i in range(n)] for j in range(n)]
oil = [[inf for i in range(n)] for j in range(n)]

for i in range(m):
    a, b, c, = list(map(int, input().split()))
    way[a - 1][b - 1] = c
    way[b - 1][a - 1] = c

q = int(input())

query = []

for i in range(q):
    query.append(list(map(int, input().split())))

#ワーシャルフロイド法を使い、各道への最短経路を調べる。計算量はO(N**3)
for i in range(n):
    for j in range(n):
        for k in range(n):
            way[i][k] = min(way[i][k],way[i][j] + way[j][k])
            if i == k:
                way[i][k] = 0
#oilの補給できる回数もワーシャルフロイドで求める。まず、一補給でいけるところに辺を張る
for i in range(n):
    for j in range(n):
        if way[i][j] <= l:
            oil[i][j] = oil[j][i] = 1
#の後でワーシャルフロイド
for i in range(n):
    for j in range(n):
        for k in range(n):
            oil[i][k] = min(oil[i][k],oil[i][j] + oil[j][k])
            if i == k:
                oil[i][k] = 0
                
for i in range(q):
    ans = oil[query[i][0] - 1][query[i][1] - 1] - 1
    if way[query[i][0] - 1][query[i][1] - 1] == inf:
        ans = -1
    print(ans)