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
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n,m,L=map(int,input().split())
    E=[[] for _ in range(n)]
    for _ in range(m):
        u,v,w=map(int,input().split())
        if(w>L): continue
        u-=1; v-=1
        E[u].append((v,w))
        E[v].append((u,w))

    # 各始点からの Dijkstra
    # dist は (補給回数、補給してからの使用料)
    A=[None]*n
    for s in range(n):
        dist=[(INF,INF)]*n
        dist[s]=(0,0)
        used=[0]*n
        for _ in range(n-1):
            v=min((dist[v],v) for v in range(n) if(not used[v]))[1]
            used[v]=1
            c,l=dist[v]
            for nv,w in E[v]:
                if(dist[nv]!=(INF,INF)): continue
                if(l+w>L):
                    nd=(c+1,w)
                else:
                    nd=(c,l+w)
                dist[nv]=nd
        A[s]=dist

    for _ in range(int(input())):
        s,t=map(int,input().split())
        s-=1; t-=1
        res=A[s][t][0]
        print(res if(res!=INF) else -1)
resolve()

Submission status: Wrong Answer

Solution code:
from scipy.sparse.csgraph import floyd_warshall
import numpy as np

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

D = np.ones([N, N]) * np.inf
D[np.eye(N, dtype=bool)] = 0

for _ in range(M):
  A, B, C = map(int, input().split())
  D[A-1, B-1] = C
  D[B-1, A-1] = C

D = floyd_warshall(D)
D2 = (D <= L) + (D > L)*np.inf

D2 = floyd_warshall(D2) - 1

Q = int(input())
for _ in range(Q):
  s, t = map(int, input().split())
  r = D2[s-1, t-1]
  if r < np.inf:
    print(int(r))
  else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import heapq
import collections

def dijkstra():
    pq = [(0, L, start)]
    dist = {}
    while pq:
        cost, fuel, node = heapq.heappop(pq)
        if node == end:
            return cost
        if node in dist:
            continue
        dist[node] = cost
        for nei in graph[node]:
            ncost = graph[node][nei]
            if fuel >= ncost:
                heapq.heappush(pq, (cost, fuel - ncost, nei))
            else:
                heapq.heappush(pq, (cost+1, L-ncost, nei))
    return -1

def floyd_warshall(dist, graph):
    for i in range(N):
        dist[i][i] = 0
    
    for i in range(N):
        for j in range(i):
            dist[i][j] = dist[j][i] = graph[i][j]
        
    for i in range(N):
        for j in range(N):
            for k in range(N):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
    
    
N, M, L = map(int, input().split())
graph = [[float('inf')] * N for _ in range(N)]

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

dist = [[float('inf')] * N for _ in range(N)]
floyd_warshall(dist, graph)

graph1 = [[float('inf')] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if dist[i][j] <= L:
            graph1[i][j] = 1
            
dist1 = [[float('inf')] * N for _ in range(N)]
floyd_warshall(dist1, graph1)

Q = int(input())
for _ in range(Q):
    start, end= map(int, input().split())
    if dist1[start-1][end-1] != float('inf'):
    	print(dist1[start-1][end-1] - 1)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
import heapq
import collections

def dijkstra():
    pq = [(0, L, start)]
    dist = {}
    while pq:
        #print(pq)
        cost, fuel, node = heapq.heappop(pq)
        if node == end:
            return cost
        if node in dist:
            continue
        dist[node] = cost
        for nei in graph[node]:
            ncost = graph[node][nei]
            if fuel >= ncost:
                heapq.heappush(pq, (cost, fuel - ncost, nei))
            else:
                heapq.heappush(pq, (cost+1, L-ncost, nei))
    return -1

graph = collections.defaultdict(dict)
N, M, L = map(int, input().split())
for _ in range(M):
    a, b, c = map(int, input().split())
    graph[a][b] = c
    graph[b][a] = c

Q = int(input())
for _ in range(Q):
    start, end = map(int, input().split())
    print(dijkstra())

Submission status: Wrong Answer

Solution code:
import sys
from copy import deepcopy

def main():
    INF = 10**18
    input = sys.stdin.readline
    N, M, L = [int(x) for x in input().split()]
    d = [set() for _ in range(N+1)]
    ds = [[INF] * (N+1) for _ in range(N+1)]
    bs = [[INF] * (N+1) for _ in range(N+1)]
    for _ in range(M):
        A, B, C = [int(x) for x in input().split()]
        A, B = sorted([A, B])
        d[A].add(B)
        if L >= C:
            ds[A][B] = C

    nes = set()
    for k in range(1, N+1):
        for i in range(1, N+1):
            for j in range(i+1, N+1):
                ds[i][j] = min(ds[i][j], ds[i][k] + ds[k][j])
                if ds[i][j] <= L:
                    bs[i][j] = 1
    #            es.remove((i,j))

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

    Q, = [int(x) for x in input().split()]
    for _ in range(Q):
        s, t = sorted(int(x) for x in input().split())
        print(bs[s][t]-1 if bs[s][t] < INF else -1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from copy import deepcopy

def main():
    INF = 10**18
    input = sys.stdin.readline
    N, M, L = [int(x) for x in input().split()]
    d = [set() for _ in range(N+1)]
    cs = [[INF] * (N+1) for _ in range(N+1)]
    for _ in range(M):
        A, B, C = [int(x) for x in input().split()]
        d[A].add(B)
        d[B].add(A)
        cs[A][B] = C
        cs[B][A] = C

    #ds = [[INF] * (N+1) for in range(N+1)]:
    ds = deepcopy(cs)
    bs = [[INF] * (N+1) for _ in range(N+1)]
    for k in range(1, N+1):
        for i in range(1, N+1):
            for j in range(1, N+1):
                ds[i][j] = min(ds[i][j], ds[i][k] + ds[k][j])
                if ds[i][j] <= L:
                    bs[i][j] = 1

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

    Q, = [int(x) for x in input().split()]
    for _ in range(Q):
        s, t = [int(x) for x in input().split()]
        print(bs[s][t]-1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush
import sys
input = lambda: sys.stdin.readline().rstrip()
inpl = lambda: list(map(int,input().split()))

N, M, L = inpl()
road = [[] for _ in range(N)]
for i in range(M):
    A, B, C = inpl()
    if C <= L:
        road[A-1].append((B-1,C))
        road[B-1].append((A-1,C))
ans = [[None]*N for _ in range(N)]

Q = int(input())
ST = []
goals = [set() for _ in range(N)]
for _ in range(Q):
    s, t = inpl()
    s -= 1
    t -= 1
    ST.append((s, t))
    goals[s].add(t)

for s in range(N):
    if not goals[s]:
        continue
    q = [((0,0), s)]
    done = set()
    goal_set = set(goals[s])
    while q:
        D, x = heappop(q)
        refuel, oil = D
        if x in done:
            continue
        elif x in goal_set:
            ans[s][x] = refuel
            goal_set.remove(x)
            if len(goal_set) == 0:
                break
        done.add(x)
        for y, d in road[x]:
            if y in done:
                continue
            if oil + d > L:
                heappush(q, ((refuel+1, d), y))
            else:
                heappush(q, ((refuel, oil+d), y))

for s, t in ST:
    print(ans[s][t])

Submission status: Wrong Answer

Solution code:
n,m,l=map(int,input().split())
d=[[10**18]*n for i in range(n)]
for _ in range(m):
    a,b,c=map(int,input().split())
    if c<=l:
        d[a-1][b-1]=c
        d[b-1][a-1]=c
for i in range(n):
    d[i][i]=0
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])

for i in range(n):
    for j in range(n):
        if d[i][j]<=l:
            d[i][j]=1
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])
q=int(input())
for i in range(q):
    s,t=map(int,input().split())
    s-=1
    t-=1
    print(d[s][t]-1 if d[s][t]-1<10**18 else -1)

Submission status: Wrong Answer

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

INF = 10 ** 9
dist1 = [[INF] * N for _ in range(N)]
for i in range(N):
    dist1[i][i] = 0

for _ 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):
            dist1[i][j] = min(dist1[i][j], dist1[i][k] + dist1[k][j])

dist2 = [[INF] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if dist1[i][j] <= L:
            dist2[i][j] = 1

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

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

Submission status: Wrong Answer

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

INF = 10 ** 9
dist1 = [[INF] * N for _ in range(N)]
for i in range(N):
    dist1[i][i] = 0

for _ 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):
            dist1[i][j] = min(dist1[i][j], dist1[i][k] + dist1[k][j])

dist2 = [[INF] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if dist1[i][j] <= L:
            dist2[i][j] = 1

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

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

st = [list(map(int, input().split())) for _ in range(q)]

graph = [[float('inf') for _ in range(n)] for _ in range(n)]


for a, b, c in abc:
    graph[a-1][b-1] = c 
    graph[b-1][a-1] = c
for i in range(n):
    graph[i][i] = 0

# print(graph)

def warshall_floyd(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

graph = warshall_floyd(graph)

graph2 = [[float('inf')]*n for _ in range(n)]
for i in range(n):
    for j in range(n):
        if graph[i][j] <= l:
            graph2[i][j] = 1
    graph2[i][i] = 0

graph2 = warshall_floyd(graph2)

for s, t in st:
    print(graph2[s-1][t-1]-1 if graph2[s-1][t-1] != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

st = [list(map(int, input().split())) for _ in range(q)]

graph = [[float('inf') for _ in range(n)] for _ in range(n)]


for a, b, c in abc:
    graph[a-1][b-1] = c 
    graph[b-1][a-1] = c
for i in range(n):
    graph[i][i] = 0

# print(graph)

def warshall_floyd(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

graph = warshall_floyd(graph)

graph2 = [[float('inf')]*n for _ in range(n)]
for i in range(n):
    for j in range(n):
        if graph[i][j] <= l:
            graph2[i][j] = 1
    graph2[i][i] = 0

graph2 = warshall_floyd(graph2)
print(graph2)
for s, t in st:
    print(graph2[s-1][t-1]-1 if graph2[s-1][t-1] != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
# import sys
# input = sys.stdin.readline

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

st = [list(map(int, input().split())) for _ in range(q)]

graph = [[float('inf') for _ in range(n)] for _ in range(n)]


for a, b, c in abc:
    graph[a-1][b-1] = c 
    graph[b-1][a-1] = c
for i in range(n):
    graph[i][i] = 0

# print(graph)

def warshall_floyd(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

graph = warshall_floyd(graph)

for i in range(n):
    for j in range(n):
        if graph[i][j] <= l:
            graph[i][j] = 1
        if graph[i][j] > l:
            graph[i][j] = float('inf')

graph = warshall_floyd(graph)

for s, t in st:
    print(graph[s-1][t-1]-1 if graph[s-1][t-1] != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
# import sys
# input = sys.stdin.readline

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

st = [list(map(int, input().split())) for _ in range(q)]

graph = [[float('inf') for _ in range(n)] for _ in range(n)]


for a, b, c in abc:
    graph[a-1][b-1] = c 
    graph[b-1][a-1] = c
for i in range(n):
    graph[i][i] = 0

# print(graph)

def warshall_floyd(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

graph = warshall_floyd(graph)

for i in range(n):
    for j in range(n):
        if i != j and graph[i][j] <= l:
            graph[i][j] = 1
        if graph[i][j] > l:
            graph[i][j] = float('inf')

graph = warshall_floyd(graph)

for s, t in st:
    print(graph[s-1][t-1]-1 if graph[s-1][t-1] != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

st = [list(map(int, input().split())) for _ in range(q)]

graph = [[float('inf') for _ in range(n)] for _ in range(n)]


for a, b, c in abc:
    graph[a-1][b-1] = c 
    graph[b-1][a-1] = c
for i in range(n):
    graph[i][i] = 0

# print(graph)

def warshall_floyd(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

graph = warshall_floyd(graph)

for i in range(n):
    for j in range(n):
        if 0 < graph[i][j] <= l:
            graph[i][j] = 1
        if graph[i][j] > l:
            graph[i][j] = float('inf')

graph = warshall_floyd(graph)

for s, t in st:
    print(graph[s-1][t-1]-1 if graph[s-1][t-1] != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

st = [list(map(int, input().split())) for _ in range(q)]

graph = [[float('inf') for _ in range(n)] for _ in range(n)]


for a, b, c in abc:
    graph[a-1][b-1] = c 
    graph[b-1][a-1] = c
for i in range(n):
    graph[i][i] = 0

# print(graph)

def warshall_floyd(graph):
    d = graph.copy()
    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

graph = warshall_floyd(graph)

for i in range(n):
    for j in range(n):
        if 0 < graph[i][j] <= l:
            graph[i][j] = 1
        if graph[i][j] > l:
            graph[i][j] = float('inf')

graph = warshall_floyd(graph)

for s, t in st:
    print(graph[s-1][t-1]-1 if graph[s-1][t-1] != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
import sys
read=sys.stdin.buffer.read
readline=sys.stdin.buffer.readline
readlines=sys.stdin.buffer.readlines

N,M,L=map(int,readline().split())
#print("N M L:",N,M,L)

root=[[10**10 for i in range(N+1)] for j in range(N+1)]
for i in range(M):
  A,B,C=map(int,readline().split())
  root[A][B],root[B][A]=C,C
  
Q=int(readline())
S,T=[0]*Q,[0]*Q
for i in range(Q):
  S[i],T[i]=map(int,readline().split())
print("root:",root)
#print("S:",S)
#print("T:",T)

for k in range(1,N+1):
  for i in range(1,N+1):
    for j in range(1,N+1):
      root[i][j]=min(root[i][j],root[i][k]+root[k][j])
#print("root:",root)
for i in range(1,N+1):
  for j in range(1,N+1):
    if root[i][j]<=L:
      root[i][j]=1
    else:
      root[i][j]=10**10
print("root(1):",root)

for k in range(1,N+1):
  for i in range(1,N+1):
    for j in range(1,N+1):
      if root[i][k]!=-1 and root[k][j]!=-1:
        root[i][j]=min(root[i][j],root[i][k]+root[k][j])
print("root(2):",root)

for i in range(Q):
  print(root[S[i]][T[i]]-1 if root[S[i]][T[i]]<10**10 else -1)

Submission status: Wrong Answer

Solution code:
import sys
read=sys.stdin.buffer.read
readline=sys.stdin.buffer.readline
readlines=sys.stdin.buffer.readlines

N,M,L=map(int,readline().split())
#print("N M L:",N,M,L)

A,B,C=[0]*M,[0]*M,[0]*M,
root=[[10**10 for i in range(N+1)] for j in range(N+1)]
for i in range(M):
  A[i],B[i],C[i]=map(int,readline().split())
  root[A[i]][B[i]],root[B[i]][A[i]]=C[i],C[i]
  
Q=int(readline())
S,T=[0]*Q,[0]*Q
for i in range(Q):
  S[i],T[i]=map(int,readline().split())
#print("root:",root)
#print("S:",S)
#print("T:",T)

for i in range(1,N+1):
  for k in range(1,N+1):
    for j in range(1,N+1):
      root[i][j]=min(root[i][j],root[i][k]+root[k][j])
#print("root:",root)
for i in range(1,N+1):
  for j in range(1,N+1):
    if root[i][j]<=L:
      root[i][j]=1
    else:
      root[i][j]=10**10
#print("root:",root)

for i in range(1,N+1):
  for k in range(1,N+1):
    for j in range(1,N+1):
      if root[i][k]!=-1 and root[k][j]!=-1:
        root[i][j]=min(root[i][j],root[i][k]+root[k][j])
#print("root:",root)

for i in range(Q):
  print(root[S[i]][T[i]]-1 if root[S[i]][T[i]]<10**10 else -1)

Submission status: Wrong Answer

Solution code:
import sys
read=sys.stdin.buffer.read
readline=sys.stdin.buffer.readline
readlines=sys.stdin.buffer.readlines

N,M,L=map(int,readline().split())
#print("N M L:",N,M,L)

A,B,C=[0]*M,[0]*M,[0]*M,
root=[[10**10 for i in range(N+1)] for j in range(N+1)]
for i in range(M):
  A[i],B[i],C[i]=map(int,readline().split())
  root[A[i]][B[i]],root[B[i]][A[i]]=C[i],C[i]
  
Q=int(readline())
S,T=[0]*Q,[0]*Q
for i in range(Q):
  S[i],T[i]=map(int,readline().split())
#print("root:",root)
#print("S:",S)
#print("T:",T)

for i in range(1,N+1):
  for k in range(1,N+1):
    for j in range(1,N+1):
      if root[i][k]!=-1 and root[k][j]!=-1:
        root[i][j]=min(root[i][j],root[i][k]+root[k][j])
#print("root:",root)
for i in range(1,N+1):
  for j in range(1,N+1):
    if root[i][j]<=L:
      root[i][j]=1
    else:
      root[i][j]=10**10
#print("root:",root)

for i in range(1,N+1):
  for k in range(1,N+1):
    for j in range(1,N+1):
      if root[i][k]!=-1 and root[k][j]!=-1:
        root[i][j]=min(root[i][j],root[i][k]+root[k][j])
#print("root:",root)

for i in range(Q):
  print(root[S[i]][T[i]]-1 if root[S[i]][T[i]]!=10**10 else -1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from scipy.sparse.csgraph import floyd_warshall

"""
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 = map(int, input().split())

d = [[float("inf") for i in range(N)] for i in range(N)]

for i in range(M):
    A, B, C = map(int, input().split())
    if C <= L:
        d[A-1][B-1] = C
        d[B-1][A-1] = C

S = [[0] * N for _ in range(N)]
floyd_warshall(d, directed = False)

for i in range(N):
    for j in range(N):
        if d[i][j] <= L:
            S[i][j] = 1
            S[j][i] = 1
            
floyd_warshall(S, directed = False)

Q = int(input())

for i in range(Q):
    s, t = map(int, input().split())
    if S[s-1][t-1] == float("inf"):
        print(-1)
    else: 
        print(S[s-1][t-1]-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from scipy.sparse.csgraph import floyd_warshall

"""
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 = map(int, input().split())

d = [[float("inf") for i in range(N)] for i in range(N)]

for i in range(M):
    A, B, C = map(int, input().split())
    if C <= L:
        d[A-1][B-1] = C
        d[B-1][A-1] = C

S = [[float("inf")] * N for _ in range(N)]
floyd_warshall(d)

for i in range(N):
    for j in range(N):
        if d[i][j] <= L:
            S[i][j] = 1
            S[j][i] = 1
            
floyd_warshall(S)

Q = int(input())

for i in range(Q):
    s, t = map(int, input().split())
    if S[s-1][t-1] == float("inf"):
        print(-1)
    else: 
        print(S[s-1][t-1]-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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 = map(int, input().split())

d = [[float("inf") for i in range(N)] for i in range(N)]

for i in range(M):
    A, B, C = map(int, input().split())
    if C <= L:
        d[A-1][B-1] = C
        d[B-1][A-1] = C
for i in range(N):
    d[i][i] = 0

S = [[float("inf")] * N for _ in range(N)]
d2 = warshall_floyd(d)

for i in range(N):
    for j in range(N):
        if d2[i][j] <= L:
            S[i][j] = 1
            S[j][i] = 1


warshall_floyd(S)



Q = int(input())

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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 = map(int, input().split())

d = [[float("inf") for i in range(N)] for i in range(N)]

for i in range(M):
    A, B, C = map(int, input().split())
    if C <= L:
        d[A-1][B-1] = C
        d[B-1][A-1] = C
for i in range(N):
    d[i][i] = 0

d2 = warshall_floyd(d)

Q = int(input())
a = []
for i in range(Q):
    s, t = map(int, input().split())
    ans = -1
    q = d2[s-1][t-1]
    if q == float("inf"):
        a.append(ans)
    else:
        ans = q // L
        a.append(ans)
for i in a:
    print(i)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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 = map(int, input().split())

d = [[float("inf") for i in range(N)] for i in range(N)]

for i in range(M):
    A, B, C = map(int, input().split())
    if C <= L:
        d[A-1][B-1] = C
        d[B-1][A-1] = C
for i in range(N):
    d[i][i] = 0

d2 = warshall_floyd(d)

Q = int(input())

for i in range(Q):
    s, t = map(int, input().split())
    ans = -1
    q = d2[s-1][t-1]
    if q == float("inf"):
        print(ans)
    else:
        while q > 0:
           ans += 1
           q = q - L
        print(ans)

Submission status: Wrong Answer

Solution code:
n, m, l = map(int, input().split())
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    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
inf = 10**12
d = [[inf for i in range(n)] for i in range(n)] 
#d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(m):
    x,y,z = map(int,input().split())
    d[x-1][y-1] = z
    d[y-1][x-1] = z
for i in range(n):
    d[i][i] = 0 #自身のところに行くコストは０
dd = warshall_floyd(d)
ddd = [[inf for i in range(n)] for i in range(n)]
for i in range(n):
  for j in range(n):
    if dd[i][j] <= l:
      ddd[i][j] = 1
dddd = warshall_floyd(ddd)
q = int(input())
for i in range(q):
  s, t = map(int, input().split())
  print(dddd[s-1][t-1]-1)

Submission status: Wrong Answer

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

class WarshallFloyd:
    def __init__(self,n):
        self.n = n
        self.inf = 1000000000
        self.d = [[self.inf for _ in range(n)] for _ in range(n)]
        for i in range(self.n):
            self.d[i][i] = 0
    def register(self,v1,v2,l,multiple=True):
        self.d[v1][v2] = l
        if multiple:
            self.d[v2][v1] = l
    
    def solve(self):
        for i in range(self.n):
            for j in range(self.n):
                for k in range(self.n):
                    self.d[j][k] = min(self.d[j][k],self.d[j][i]+self.d[i][k])
        return self.d

W = WarshallFloyd(N)
for a,b,c in ABC:
    W.register(a-1,b-1,c)
D = W.solve()

WL = WarshallFloyd(N)
for i in range(N):
    for j in range(N):
        if D[i][j] <= L and i != j:
            WL.register(i,j,1)
DL = WL.solve()
ST = [list(map(int,input().split())) for _ in range(Q)]
for i in range(Q):
    s,t = ST[i]
    transition = DL[s-1][t-1]
    if transition == 1000000000:
        print(-1)
    else:
        print(transition-1)

Submission status: Wrong Answer

Solution code:
INF=10**15
N,M,L=map(int,input().split())
d=[[INF for i in range(N)] for j in range(N)]
for i in range(N):
    d[i][i]=0
for m in range(M):
    A,B,C=map(int,input().split())
    d[A-1][B-1]=C
    d[B-1][A-1]=C

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])
for i in range(N):
        for j in range(N):
            if i!=j and d[i][j]<=L:
                d[i][j]=1
                d[j][i]=1
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])

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

Submission status: Wrong Answer

Solution code:
INF=10**15
N,M,L=map(int,input().split())
d=[[INF for i in range(N)] for j in range(N)]
for i in range(N):
    d[i][i]=0
for m in range(M):
    A,B,C=map(int,input().split())
    d[A-1][B-1]=C
    d[B-1][A-1]=C

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])
for i in range(N):
        for j in range(N):
            if i!=j and d[i][j]<=L:
                d[i][j]=1
                d[j][i]=1
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])
ans=[]
Q=int(input())
for i in range(Q):
    s,t=map(int,input().split())
    dis=d[s-1][t-1]
    if dis==INF:
        ans.append(-1)
    else:
        ans.append(dis-1)

for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
INF=10**15
N,M,L=map(int,input().split())
d=[[INF for i in range(N)] for j in range(N)]
for i in range(N):
    d[i][i]=0
for m in range(M):
    A,B,C=map(int,input().split())
    d[A-1][B-1]=C
    d[B-1][A-1]=C

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])
for i in range(N):
        for j in range(N):
            if i!=j and d[i][j]<=L:
                d[i][j]=1
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])
ans=[]
Q=int(input())
for i in range(Q):
    s,t=map(int,input().split())
    dis=d[s-1][t-1]
    if dis==INF:
        ans.append(-1)
    else:
        ans.append(dis-1)

for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
INF=10**18
N,M,L=map(int,input().split())
d=[[INF for i in range(N)] for j in range(N)]
for i in range(N):
    d[i][i]=0
for m in range(M):
    A,B,C=map(int,input().split())
    d[A-1][B-1]=C
    d[B-1][A-1]=C

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])
for i in range(N):
        for j in range(N):
            if d[i][j]<=L:
                d[i][j]=1
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])
ans=[]
Q=int(input())
for i in range(Q):
    s,t=map(int,input().split())
    dis=d[s-1][t-1]
    if dis==INF:
        ans.append(-1)
    else:
        ans.append(dis-1)

for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
INF=10**15
N,M,L=map(int,input().split())
d=[[INF for i in range(N)] for j in range(N)]
for i in range(N):
    d[i][i]=0
for m in range(M):
    A,B,C=map(int,input().split())
    d[A-1][B-1]=C
    d[B-1][A-1]=C

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])
for i in range(N):
        for j in range(N):
            if d[i][j]<=L:
                d[i][j]=1
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])
ans=[]
Q=int(input())
for i in range(Q):
    s,t=map(int,input().split())
    dis=d[s-1][t-1]
    if dis==INF:
        ans.append(-1)
    else:
        ans.append(dis-1)

for a in ans:
    print(int(a))