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

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N, M, L = map(int, input().split())
        ABC = [tuple(map(int, input().split())) for _ in range(M)]
        Q = int(input())
        st = [tuple(map(int, input().split())) for _ in range(Q)]

        load = [[10 ** 9] * N for _ in range(N)]
        for a, b, c in ABC:
            load[a - 1][b - 1] = c
            load[b - 1][a - 1] = c

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

        load2 = [[10 ** 9] * N for _ in range(N)]
        for i in range(N):
            for j in range(N):
                if load[i][j] <= L:
                    load2[i][j] = 1

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

        for s, t in st:
            if load2[s - 1][t - 1] == 10 ** 9:
                print(-1)
            else:
                print(load2[s - 1][t - 1] - 1)


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N, M, L = map(int, input().split())
        ABC = [tuple(map(int, input().split())) for _ in range(M)]
        Q = int(input())
        st = [tuple(map(int, input().split())) for _ in range(Q)]

        load = [[10 ** 9 + 1] * N for _ in range(N)]
        for a, b, c in ABC:
            load[a - 1][b - 1] = c
            load[b - 1][a - 1] = c

        for k in range(N):
            for i in range(N):
                for j in range(N):
                    if i == j:
                        load[i][j] = 0
                    else:
                        load[i][j] = min(load[i][j], load[i][k] + load[k][j])

        load2 = [[10 ** 9 + 1] * N for _ in range(N)]
        for i in range(N):
            for j in range(N):
                if load[i][j] <= L:
                    load2[i][j] = 1

        for k in range(N):
            for i in range(N):
                for j in range(N):
                    if i == j:
                        load2[i][j] = 0
                    else:
                        load2[i][j] = min(load2[i][j], load2[i][k] + load2[k][j])

        for s, t in st:
            if load2[s - 1][t - 1] == 10 ** 9 + 1:
                print(-1)
            else:
                print(load2[s - 1][t - 1] - 1)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N, M, L = map(int, input().split())
        ABC = [tuple(map(int, input().split())) for _ in range(M)]
        Q = int(input())
        st = [tuple(map(int, input().split())) for _ in range(Q)]

        load = [[10 ** 9] * N for _ in range(N)]
        for a, b, c in ABC:
            load[a - 1][b - 1] = c
            load[b - 1][a - 1] = c

        for k in range(N):
            for i in range(N):
                for j in range(N):
                    if i == j:
                        load[i][j] = 0
                    else:
                        load[i][j] = min(load[i][j], load[i][k] + load[k][j])

        load2 = [[10 ** 9] * N for _ in range(N)]
        for i in range(N):
            for j in range(N):
                if load[i][j] <= L:
                    load2[i][j] = 1

        for k in range(N):
            for i in range(N):
                for j in range(N):
                    if i == j:
                        load2[i][j] = 0
                    else:
                        load2[i][j] = min(load2[i][j], load2[i][k] + load2[k][j])

        for s, t in st:
            if load2[s - 1][t - 1] == 10 ** 9:
                print(-1)
            else:
                print(load2[s - 1][t - 1] - 1)


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Wrong Answer

Solution code:
import math
#道がない場合、infで初期化したい
inf = 10 ** 10
#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
    if c <= l:
        oil[a - 1][b - 1] = 1
        oil[b - 1][a - 1] = 1

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])
#oilの補給できる回数もワーシャルフロイドで求める。これは補給可能な数
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])

for i in range(q):
    ans = math.ceil(way[query[i][0] - 1][query[i][1] - 1] / l) - 1
    if way[query[i][0] - 1][query[i][1] - 1] == inf:
        ans = -1
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
input = sys.stdin.readline

N, M, L = map(int, input().split())
INF = float("inf")
dist1 = [[INF]*N for i in range(N)]
dist2 = [[INF]*N for i in range(N)]

for i in range(M):
  A,B,C = map(int,input().split())
  A -= 1
  B -= 1
  dist1[A][B] = C
  dist1[B][A] = C
  
for k in range(N):
  for i in range(N):
    for j in range(N):
      if i==j:
        dist1[i][j] = 0
      dist2[i][j] = min(dist2[i][j], dist1[i][k]+dist1[k][j])

for i in range(N):
  for j in range(N):
    if dist2[i][j] <= L:
      dist2[i][j] = 1
    else:
      dist2[i][j] = INF
      
for k in range(N):
  for i in range(N):
    for j in range(N):
      if i==j:
        dist2[i][j] = 0
      dist2[i][j] = min(dist2[i][j], dist2[i][k]+dist2[k][j])
Q = int(input())

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

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
input = sys.stdin.readline

N, M, L = map(int, input().split())
INF = 10**9+1
dist1 = [[INF]*N for i in range(N)]
dist2 = [[INF]*N for i in range(N)]

for i in range(M):
  A,B,C = map(int,input().split())
  A -= 1
  B -= 1
  dist1[A][B] = C
  dist1[B][A] = C
  
for k in range(N):
  for i in range(N):
    for j in range(N):
      if i==j:
        dist1[i][j] = 0
      dist2[i][j] = min(dist2[i][j], dist1[i][k]+dist1[k][j])

for i in range(N):
  for j in range(N):
    if dist2[i][j] <= L:
      dist2[i][j] = 1
    else:
      dist2[i][j] = INF
      
for k in range(N):
  for i in range(N):
    for j in range(N):
      if i==j:
        dist2[i][j] = 0
      dist2[i][j] = min(dist2[i][j], dist2[i][k]+dist2[k][j])
Q = int(input())

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

Submission status: Wrong Answer

Solution code:
from collections import deque
N, M, L = map(int, input().split())
INF = 10**9+1
dist1 = [[INF]*N for i in range(N)]
dist2 = [[INF]*N for i in range(N)]

for i in range(M):
  A,B,C = map(int,input().split())
  A -= 1
  B -= 1
  dist1[A][B] = C
  dist1[B][A] = C
  
for k in range(N):
  for i in range(N):
    for j in range(N):
      if i==j:
        dist1[i][j] = 0
      dist2[i][j] = min(dist2[i][j], dist1[i][k]+dist1[k][j])

for i in range(N):
  for j in range(N):
    if dist2[i][j] <= L:
      dist2[i][j] = 1
    else:
      dist2[i][j] = INF
      
for k in range(N):
  for i in range(N):
    for j in range(N):
      if i==j:
        dist2[i][j] = 0
      dist2[i][j] = min(dist2[i][j], dist2[i][k]+dist2[k][j])
Q = int(input())

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

Submission status: Wrong Answer

Solution code:
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
i8 = np.int64
i4 = np.int32


def main():
    stdin = open('/dev/stdin')
    pin = np.fromstring(stdin.read(), i8, sep= ' ')
    N = pin[0]
    M = pin[1]
    L = pin[2]
    ABC = pin[3: M * 3 + 3].reshape((-1, 3))
    A = ABC[:, 0] - 1
    B = ABC[:, 1] - 1
    C = ABC[:, 2]
    Q = pin[M * 3 + 3]
    st = (pin[M * 3 + 4: 2 * Q + M * 3 + 4] - 1).reshape((-1, 2))

    graph = csr_matrix((C, (A, B)), shape=(N, N))
    dist = np.array(floyd_warshall(graph))
    dist[dist > L + 0.5] = 0
    dist[dist > 0] = 1
    min_dist = np.array(floyd_warshall(dist))
    min_dist[min_dist == np.inf] = 0
    for i in range(st.shape[0]):
        print(int(min_dist[st[i, 0], st[i, 1]] - 1.0))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,M,L = map(int, input().split())
RL=[list(map(int, input().split())) for _ in range(M)]
Q=int(input())
QL=[list(map(int, input().split())) for _ in range(Q)]
inf=float('inf')
fuel=[[inf]*N for _ in range(N)]
for i in range(N):
    fuel[i][i]=0
for I in RL:
    fuel[I[0]-1][I[1]-1]=fuel[I[1]-1][I[0]-1]=I[2]
for k in range(N):
    for i in range(N):
        for j in range(N):
            fuel[i][j]=min(fuel[i][j],fuel[i][k]+fuel[k][j])
for i in range(N):
    for j in range(N):
        if fuel[i][j] <= L:
            fuel[i][j]=min(fuel[i][j], 1)
for k in range(N):
    for i in range(N):
        for j in range(N):
            fuel[i][j]=min(fuel[i][j], fuel[i][k]+fuel[k][j])
for i in range(Q):
    print(fuel[QL[i][0]-1][QL[i][1]-1]-1 if fuel[QL[i][0]-1][QL[i][1]-1] != inf else -1)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import math

input = sys.stdin.readline


def solve():
    infinity = 10 ** 10
    n, m, le = map(int, input().split())
    paths = np.zeros((n, n))
    for i in range(n):
        for j in range(n):
            if i != j:
                paths[i, j] = infinity

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

    for i in range(n):
        for j in range(n):
            for k in range(n):
                paths[i, j] = min(paths[i, j], paths[i, k] + paths[k, j])

    refuels = np.array(paths, copy=True)
    for i in range(n):
        for j in range(n):
            refuels[i, j] = 1 if paths[i, j] < le else infinity
            if i == j:
                refuels[i, j] = 0

    for i in range(n):
        for j in range(n):
            for k in range(n):
                refuels[i, j] = min(refuels[i, j], refuels[i, k] + refuels[k, j])

    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        print(int(refuels[s-1, t-1] - 1 if refuels[s-1, t-1] < infinity else -1))


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
N,M,L = map(int, input().split())
RL=[list(map(int, input().split())) for _ in range(M)]
Q=int(input())
QL=[list(map(int, input().split())) for _ in range(Q)]
inf=float('inf')
fuel=[[inf]*N for _ in range(N)]
dist=[[inf]*N for _ in range(N)]
for i in range(N):
    fuel[i][i]=dist[i][i]=0
print(dist)
for I in RL:
    fuel[I[0]-1][I[1]-1]=fuel[I[1]-1][I[0]-1]=I[2]
for k in range(N):
    for i in range(N):
        for j in range(N):
            fuel[i][j]=min(fuel[i][j],fuel[i][k]+fuel[k][j])
for i in range(N):
    for j in range(N):
        if fuel[i][j] <= L:
            dist[i][j]=min(dist[i][j],1)
for k in range(N):
    for i in range(N):
        for j in range(N):
            dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j])
print(dist)
for i in range(Q):
    print(dist[QL[i][0]-1][QL[i][1]-1]-1 if dist[QL[i][0]-1][QL[i][1]-1] != inf else -1)

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter


def dijkstra_origin(s, l, links, INF):
    dist = [[INF, INF, False] for _ in [0] * n]
    dist[s] = [0, 0, False]
    for _ in range(n):
        nv = -1
        tmp_min = [INF, INF, False]
        for v in range(n):
            item = dist[v]
            if item[2]:
                continue
            if tmp_min > item:
                nv = v
                tmp_min = item
        if nv == -1:
            break
        tmp_min[2] = True
        refuel, used, _ = tmp_min
        for u, c in links[nv]:
            if dist[u][2]:
                continue
            if l - used >= c:
                new_item = [refuel, used + c, False]
            else:
                new_item = [refuel + 1, c, False]
            dist[u] = min(dist[u], new_item)
    return list(map(itemgetter(0), dist))


n, m, l = list(map(int, input().split()))
lines = sys.stdin.readlines()

links = [set() for _ in range(n)]

for line in lines[:m]:
    a, b, c = map(int, line.split())
    a -= 1
    b -= 1
    if l < c:
        continue
    links[a].add((b, c))
    links[b].add((a, c))

ans = [dijkstra_origin(v, l, links, 10 ** 18) for v in range(n)]
buf = []

for line in lines[m + 1:]:
    s, t = map(int, line.split())
    buf.append(ans[s - 1][t - 1])

print('\n'.join(map(str, buf)))

Submission status: Wrong Answer

Solution code:
N, M, L = map(int, input().split(" "))

edges = [tuple(map(int, input().split(" "))) for i in range (M)]
edges = [(x-1, y-1, w) for (x, y, w) in edges if w <= L]
edgeDict = {}
for (x, y, w) in edges:
    if x not in edgeDict:
        edgeDict[x] = {}
    edgeDict[x][y] = w

    if y not in edgeDict:
        edgeDict[y] = {}
    edgeDict[y][x] = w

def wf(V, E):
    dist = [[int(1e25)] * N for i in range(N)]
    next_ = [[None] * N for i in range(N)]
    for x, y, w in E:
        dist[x][y] = w
        dist[y][x] = w
        next_[x][y] = y
        next_[y][x] = x

    for v in V:
        dist[v][v] = 0
        next_[v][v] = v

    for k in range(0, len(V)):
        for i in range(0, len(V)):
            for j in range(0, len(V)):
                if dist[i][j] > dist[i][k] + dist[k][j]:
                    dist[i][j] = dist[i][k]+ dist[k][j]
                    next_[i][j] = next_[i][k]

    return next_, dist

def path(next_, u, v):
    if next_[u][v] == None:
        return None
    path = [u]
    while u != v:
        u = next_[u][v]
        path.append(u)
    return path

next_, dist = wf(range(N), edges)

Q = int(input())
for i in range(Q):
    s, t = map(int, input().split(" "))
    s -= 1
    t -= 1


    p = path(next_, s, t)
    if p == None:
        print(-1)
        continue
    tank = L
    count = 0
    u = p[0]
    for v in p[1:]:
        w = edgeDict[u][v]
        if w > tank:
            count += 1
            tank = L - w
        else:
            tank -= w
        u = v
    print(count)

Submission status: Wrong Answer

Solution code:
N, M, L = map(int, input().split(" "))

edges = [tuple(map(int, input().split(" "))) for i in range (M)]
edges = [(x-1, y-1, w) for (x, y, w) in edges if w <= L]
edgeDict = {}
for (x, y, w) in edges:
    if x not in edgeDict:
        edgeDict[x] = {}
    edgeDict[x][y] = w

    if y not in edgeDict:
        edgeDict[y] = {}
    edgeDict[y][x] = w

def wf(V, E):
    dist = [[int(1e18)] * N for i in range(N)]
    next_ = [[None] * N for i in range(N)]
    for x, y, w in E:
        dist[x][y] = w
        dist[y][x] = w
        next_[x][y] = y
        next_[y][x] = x

    for v in V:
        dist[v][v] = 0
        next_[v][v] = v

    for k in range(0, len(V)):
        for i in range(0, len(V)):
            for j in range(0, len(V)):
                if dist[i][j] > dist[i][k] + dist[k][j]:
                    dist[i][j] = dist[i][k]+ dist[k][j]
                    next_[i][j] = next_[i][k]

    return next_, dist

def path(next_, u, v):
    if next_[u][v] == None:
        return None
    path = [u]
    while u != v:
        u = next_[u][v]
        path.append(u)
    return path

next_, dist = wf(range(N), edges)

Q = int(input())
for i in range(Q):
    s, t = map(int, input().split(" "))
    s -= 1
    t -= 1


    p = path(next_, s, t)
    if p == None:
        print(-1)
        continue
    tank = L
    count = 0
    u = p[0]
    for i in range(1, len(p)):
        v = p[i]
        w = edgeDict[u][v]
        if w > tank:
            count += 1
            tank = L - w
        else:
            tank -= w
        u = v
    print(count)

Submission status: Wrong Answer

Solution code:
def warshall_floyd(dist):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                dist[i][j] = min(dist[i][j],
                              dist[i][k] + dist[k][j])
    return dist

n, m, l = map(int, input().split()) #N:頂点数　M:辺の数 L:容量
dist = [[float("inf") for i in range(n)] for j in range(n)]
dist2 = [[float("inf") for i in range(n)] for j in range(n)]

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

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

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

warshall_floyd(dist2)

q = int(input())
for qi in range(q):
    s, t = map(int, input().split())
    ans = dist2[s-1][t-1]-1
    if dist2[s-1][t-1] == float("inf"):
        ans = -1
    print(ans)

Submission status: Wrong Answer

Solution code:
def warshall_floyd(dist):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                dist[i][j] = min(dist[i][j],
                              dist[i][k] + dist[k][j])
    return dist

n, m, l = map(int, input().split()) #N:頂点数　M:辺の数 L:容量
dist = [[float("inf") for i in range(n)] for j in range(n)]
dist2 = [[float("inf") for i in range(n)] for j in range(n)]

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

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

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

warshall_floyd(dist2)

q = int(input())
for qi in range(q):
    s, t = map(int, input().split())
    ans = dist2[s-1][t-1]-1
    if dist2[s-1][t-1] == "inf":
        ans = -1

Submission status: Wrong Answer

Solution code:
import sys
from heapq import heappop, heappush


class UnionFind:
    def __init__(self, n):
        self.table = [-1] * n

    def _root(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self._root(self.table[x])
            return self.table[x]

    def find(self, x, y):
        return self._root(x) == self._root(y)

    def union(self, x, y):
        r1 = self._root(x)
        r2 = self._root(y)
        if r1 == r2:
            return
        d1 = self.table[r1]
        d2 = self.table[r2]
        if d1 <= d2:
            self.table[r2] = r1
            self.table[r1] += d2
        else:
            self.table[r1] = r2
            self.table[r2] += d1

    def united_count(self, x):
        return -self.table[self._root(x)]


def dijkstra(s, links, f9, f30):
    q = [s]
    ret = [-1] * n
    visited = set()
    limit = uft.united_count(s)
    while q:
        item = heappop(q)

        v = item & f9
        if v in visited:
            continue
        visited.add(v)

        if len(visited) >= limit:
            break

        count = item >> 39
        used = (item >> 9) & f30
        ret[v] = count

        for u, c in links[v]:
            if u in visited:
                continue
            if l - used < c:
                new_item = ((count + 1) << 39) + (c << 9) + u
            else:
                new_item = (count << 39) + ((used + c) << 9) + u
            heappush(q, new_item)

    return ret


n, m, l = list(map(int, input().split()))
lines = sys.stdin.readlines()

links = [set() for _ in range(n)]
uft = UnionFind(n)

for line in lines[:m]:
    a, b, c = map(int, line.split())
    a -= 1
    b -= 1
    if l < c:
        continue
    links[a].add((b, c))
    links[b].add((a, c))
    uft.union(a, b)

f9 = (1 << 9) - 1
f30 = (1 << 30) - 1

ans = [dijkstra(v, links, f9, f30) for v in range(n)]
buf = []

for line in lines[m + 1:]:
    s, t = map(int, line.split())
    buf.append(ans[s - 1][t - 1])

print('\n'.join(map(str, buf)))

Submission status: Wrong Answer

Solution code:
def Warshall_Floyd(dist,n,restoration=False):
    next_point = [[j for j in range(n)] for _ in range(n)]
    for i in range(n):
        for j in range(n):
            for k in range(n):
                if dist[j][k] > dist[j][i] + dist[i][k]:
                    next_point[j][k] = next_point[j][i]
                    dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k])
    if restoration:
        return dist,next_point
    else:
        return dist

n,m,l = map(int,input().split())
inf = 10**18
dist = [[inf for i in range(n)] for j in range(n)]

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

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

dist = Warshall_Floyd(dist,n)
route = [[inf for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(n):
        if i == j:
            route[i][j] = 0
        elif dist[i][j] <= l:
            route[i][j] = 1

route = Warshall_Floyd(route,n)

q = int(input())


for i in range(q):
    s,t = map(int,input().split())
    s -= 1
    t -= 1
    print(route[s][t]-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N,M,L=map(int,input().split())
ABC=[map(int,input().split()) for _ in range(M)]
Q=int(input())
ST=[list(map(int,input().split())) for _ in range(Q)]

E=[[-1]*N for _ in range(N)]
for a,b,c in ABC:
    E[min(a,b)-1][max(a,b)-1]=c

def warshall_floyd(V,E):
    for k in range(V):
        for i in range(V):
            for j in range(i+1,V):
                if E[i][j] == -1:
                    if (E[i][k] != -1) and (E[k][j] != -1):
                        E[i][j]=E[i][k]+E[k][j]
                elif E[i][k] == -1 or E[k][j] == -1:
                    pass
                else:
                    E[i][j]=min(E[i][j],E[i][k]+E[k][j])
    return E

E=warshall_floyd(N,E)
for i in range(N):
    E[i][i]=-1
E=[[1 if __<= L and __!= -1 else -1 for __ in _] for _ in E]
E=warshall_floyd(N,E)


for s,t in ST:
    if E[min(s,t)-1][max(s,t)-1]>0:
        print(E[min(s,t)-1][max(s,t)-1]-1)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from scipy.sparse import csr_matrix #pypyだとエラーになるので、pythonを使うこと
from scipy.sparse.csgraph import shortest_path, floyd_warshall
import numpy as np

import sys
def input():
	return sys.stdin.readline().strip()

N, M, L = list(map(int, input().split()))

A = np.zeros(M)
B = np.zeros(M)
C = np.zeros(M)
for i in range(M):
	Ai, Bi, Ci = list(map(int, input().split()))
	A[i] = Ai - 1
	B[i] = Bi - 1
	C[i] = Ci

graph = csr_matrix((C, (A, B)), shape=(N, N))
#print(graph)
dist = floyd_warshall(graph, directed=False)

graph2 = np.full((N, N), np.inf)
graph2[dist <= L] = 1
graph2 = csr_matrix(graph2)
num = floyd_warshall(graph2, directed=False)


Q = int(input())
for q in range(Q):
	s, t = list(map(int, input().split()))

Submission status: Wrong Answer

Solution code:
from scipy.sparse import csr_matrix #pypyだとエラーになるので、pythonを使うこと
from scipy.sparse.csgraph import shortest_path, floyd_warshall
import numpy as np

import sys
def input():
	return sys.stdin.readline().strip()

N, M, L = list(map(int, input().split()))

A = np.zeros(M)
B = np.zeros(M)
C = np.zeros(M)
for i in range(M):
	Ai, Bi, Ci = list(map(int, input().split()))
	A[i] = Ai - 1
	B[i] = Bi - 1
	C[i] = Ci

graph = csr_matrix((C, (A, B)), shape=(N, N))
#print(graph)
dist = floyd_warshall(graph, directed=False)

graph2 = np.full((N, N), np.inf)
graph2[dist <= L] = 1
graph2 = csr_matrix(graph2)
num = floyd_warshall(graph2, directed=False)


Q = int(input())
for q in range(Q):
	s, t = list(map(int, input().split()))
	if num[s - 1][t - 1] < 10000000:
		continue

Submission status: Wrong Answer

Solution code:
from scipy.sparse import csr_matrix #pypyだとエラーになるので、pythonを使うこと
from scipy.sparse.csgraph import shortest_path, floyd_warshall
import numpy as np

import sys
def input():
	return sys.stdin.readline().strip()

N, M, L = list(map(int, input().split()))

A = np.zeros(M)
B = np.zeros(M)
C = np.zeros(M)
for i in range(M):
	Ai, Bi, Ci = list(map(int, input().split()))
	A[i] = Ai - 1
	B[i] = Bi - 1
	C[i] = Ci

graph = csr_matrix((C, (A, B)), shape=(N, N))
#print(graph)
dist = floyd_warshall(graph, directed=False)

graph2 = np.full((N, N), np.inf)
graph2[dist <= L] = 1
graph2 = csr_matrix(graph2)
num = floyd_warshall(graph2, directed=False)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, deque, defaultdict
from math import factorial
import bisect, heapq
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

n, m, l = MAP()
d1 = [[INF]*n for i in range(n)]
for i in range(n):
    d1[i][i] = 0
for i in range(m):
    a,b,t = MAP()
    d1[a-1][b-1] = t
    d1[b-1][a-1] = t
q = INT()

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

d1 = warshallfloyd(d1)

d2 = [[INF]*n for i in range(n)]
for i in range(n):
    for j in range(n):
        if i == j:
            d2[i][j] = 0
        elif d1[i][j] <= l:
            d2[i][j] = 1

d2 = warshallfloyd(d2)

for i in range(q):
    s, t = MAP()
    res = d2[s-1][t-1] 
    if res == INF:
        print(-1)
    else:
        print(res-1)

Submission status: Wrong Answer

Solution code:
# ABC143E - Travel by Car
def floyd_warshall(dist):
    """
    Compute the shortest paths of all pairs and modify dist.
    """
    V = len(dist)
    for i in range(V):  # intermediate point
        for j, c2 in enumerate(dist[j][i] for j in range(V)):  #  source
            for k, (c1, c3) in enumerate(zip(dist[j], dist[i])):  # target
                if c2 + c3 < c1:
                    dist[j][k] = c2 + c3


def main():
    # FW -> once more FW to edges <= L
    N, M, L, *X = map(int, open(0).read().split())
    ABC, ST = X[: 3 * M], X[3 * M + 1 :]
    INF = 1 << 60
    dist = [[INF] * (N + 1) for _ in range(N + 1)]
    for i in range(N + 1):
        dist[i][i] = 0
    for v, u, cost in zip(*[iter(ABC)] * 3):
        dist[v][u] = dist[u][v] = cost
    floyd_warshall(dist)

    # assume distance within L as 1 and compute the number of fueling
    modified_dist = [[1 if x <= L else INF for x in d] for d in dist]
    floyd_warshall(modified_dist)
    ans = []
    for s, t in zip(*[iter(ST)] * 2):
        x = modified_dist[s][t]
        if x < INF:
            ans.append(x)
        else:
            ans.append(-1)
    print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, deque, defaultdict
from math import factorial
import bisect, heapq
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

n, m, l = MAP()
d1 = [[INF]*n for i in range(n)]
for i in range(n):
    d1[i][i] = 0
for i in range(m):
    a,b,t = MAP()
    d1[a-1][b-1] = t
    d1[b-1][a-1] = t
q = INT()
query = []
for i in range(q):
    s, t = MAP()
    query.append((s-1, t-1))

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

d1 = warshallfloyd(d1)

d2 = [[INF]*n for i in range(n)]
for i in range(n):
    for j in range(n):
        if i == j:
            d2[i][j] = 0
        elif d1[i][j] <= l:
            d2[i][j] = 1

d2 = warshallfloyd(d2)

for i in range(q):
    res = d2[query[i][0]][query[i][1]]
    if query[i][0] == query[i][1]:
        print(0)
    elif res == INF:
        print(-1)
    else:
        print(res-1)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, deque, defaultdict
from math import factorial
import bisect
from heapq import heappop, heappush

import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

n, m, l = MAP()
d1 = [[INF]*n for i in range(n)]
for i in range(n):
    d1[i][i] = 0
for i in range(m):
    a,b,t = MAP()
    d1[a-1][b-1] = t
    d1[b-1][a-1] = t
q = INT()
query = []
for i in range(q):
    s, t = MAP()
    query.append((s-1, t-1))

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

d1 = warshallfloyd(d1)

d2 = [[INF]*n for i in range(n)]
for i in range(n):
    for j in range(n):
        if d1[i][j] <= l:
            d2[i][j] = 1

d2 = warshallfloyd(d2)

for i in range(q):
    res = d2[query[i][0]][query[i][1]]
    if res == INF:
        print(-1)
    else:
        print(res-1)