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

Solution (Python):



import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect




import sys
input = sys.stdin.readline

INF = float("INF")

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

cost = [[INF] * N for _ in range(N)]
for i in range(N):
    cost[i][i] = 0

for _ in range(M):
    v1, v2, w = map(int, input().split())
    v1, v2 = v1-1, v2-1
    cost[v1][v2] = w
    cost[v2][v1] = w

for k in range(N):
    for i in range(N):
        for j in range(N):
            if cost[i][j] > cost[i][k] + cost[k][j]:
                cost[i][j] = cost[i][k] + cost[k][j]

for i in range(N):
    for j in range(N):
        if cost[i][j] == 0:
            continue
        elif cost[i][j] <= L:
            cost[i][j] = 1
        else:
            cost[i][j] = INF

for k in range(N):
    for i in range(N):
        for j in range(N):
            if cost[i][j] > cost[i][k] + cost[k][j]:
                cost[i][j] = cost[i][k] + cost[k][j]

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

Solution (Python):
import sys
input = sys.stdin.readline
 
n, m, l = map(int, input().split())
 
INF = float("INF")
 
d = [[INF] * n for _ in range(n)]
for i in range(n):
    d[i][i] = 0
 
for i in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    d[a][b] = c
    d[b][a] = c
 
for k in range(n):
    for i in range(n):
        for j in range(n):
            if d[i][j] > d[i][k] + d[k][j]:
                d[i][j] = d[i][k] + d[k][j]
 


 
for i in range(n):
    for j in range(n):
        if i == j:
            continue
 
        if d[i][j] <= l:
            d[i][j] = 1
        else:
            d[i][j] = INF
 
for k in range(n):
    for i in range(n):
        for j in range(n):
            if d[i][j] > d[i][k] + d[k][j]:
                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
 
    if d[s][t] == INF:
        print(-1)
    else:
        print(d[s][t]-1)

Solution (Python):
import sys
input = sys.stdin.readline

def solve():
    INF = float('inf')

    def WarshallFloyd(adjList, INF):
        numV = len(adjList)
        D = [[INF]*numV for _ in range(numV)]
        for u, adj in enumerate(adjList):
            for v, wt in adj:
                D[u][v] = wt
            D[u][u] = 0

        for k in range(numV):
            Dk = D[k]
            for i in range(numV):
                Di = D[i]
                Dik = Di[k]
                for j in range(numV):
                    D2 = Dik + Dk[j]
                    if D2 < Di[j]:
                        D[i][j] = D2

        return D

    N, M, L = map(int, input().split())
    adjL = [[] for _ in range(N)]
    for _ in range(M):
        a, b, c = map(int, input().split())
        a, b = a-1, b-1
        adjL[a].append((b, c))
        adjL[b].append((a, c))
    Q = int(input())
    querys = [tuple(map(int, input().split())) for _ in range(Q)]

    cost1ss = WarshallFloyd(adjL, INF)

    adjL2 = [[] for _ in range(N)]
    for a in range(N):
        for b in range(a+1, N):
            if cost1ss[a][b] <= L:
                adjL2[a].append((b, 1))
                adjL2[b].append((a, 1))

    cost2ss = WarshallFloyd(adjL2, INF)

    anss = []
    for s, t in querys:
        s, t = s-1, t-1
        cost = cost2ss[s][t]
        if cost == INF:
            anss.append(-1)
        else:
            anss.append(cost-1)

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


solve()

Solution (Python):
def resolve():
    from scipy.sparse.csgraph import floyd_warshall
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, m, l = map(int, input().split())
    inf = 10 ** 20
    ar = [[0] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        ar[a - 1][b - 1] = c
    x = floyd_warshall(ar, directed=False)
    br = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(i + 1, n):
            if x[i, j] <= l:
                br[i][j] = 1
    y = floyd_warshall(br, directed=False)
    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        print(int(y[s - 1, t - 1]) - 1 if y[s - 1, t - 1] < inf else -1)


if __name__ == "__main__":
    resolve()

Solution (Python):
def resolve():
    from scipy.sparse.csgraph import floyd_warshall
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, m, l = map(int, input().split())
    inf = float("inf")
    ar = [[0] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        ar[a - 1][b - 1] = c
    x = floyd_warshall(ar, directed=False)
    br = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(i + 1, n):
            if x[i, j] <= l:
                br[i][j] = 1
    y = floyd_warshall(br, directed=False)
    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        print(int(y[s - 1, t - 1]) - 1 if y[s - 1, t - 1] < inf else -1)


if __name__ == "__main__":
    resolve()

Solution (Python):
def resolve():
    from scipy.sparse.csgraph import floyd_warshall
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, m, l = map(int, input().split())
    inf = 10 ** 20
    ar = [[0] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        ar[a - 1][b - 1] = c
    x = floyd_warshall(ar, directed=False)
    br = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(i + 1, n):
            if x[i, j] <= l:
                br[i][j] = 1
    y = floyd_warshall(br, directed=False)
    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        p = y[s - 1, t - 1]
        print(int(y[s - 1, t - 1]) - 1 if y[s - 1, t - 1] < inf else -1)


if __name__ == "__main__":
    resolve()

Solution (Python):
def resolve():
    from scipy.sparse.csgraph import floyd_warshall
    import numpy as np
    import sys
    input = sys.stdin.readline
    n, m, l = map(int, input().split())
    inf = 10 ** 20
    ar = [[0] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        ar[a - 1][b - 1] = c
    x = floyd_warshall(ar, directed=False)
    br = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(i + 1, n):
            if x[i, j] <= l:
                br[i][j] = 1
    y = floyd_warshall(br, directed=False)
    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        p = y[s - 1, t - 1]
        print(int(p) - 1 if p < inf else -1)


if __name__ == "__main__":
    resolve()

Solution (Python):
def resolve():
    from scipy.sparse.csgraph import floyd_warshall
    import numpy as np
    import sys
    input = sys.stdin.readline
    n, m, l = map(int, input().split())
    inf = 10 ** 20
    ar = [[0] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        ar[a - 1][b - 1] = c
        ar[b - 1][a - 1] = c
    x = floyd_warshall(ar)
    br = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(i + 1, n):
            if x[i, j] <= l:
                br[i][j] = 1
                br[j][i] = 1
    y = floyd_warshall(br)
    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        p = y[s - 1, t - 1]
        print(int(p) - 1 if p < inf else -1)


if __name__ == "__main__":
    resolve()

Solution (Python):
def resolve():
    from scipy.sparse.csgraph import floyd_warshall
    import numpy as np
    import sys
    input = sys.stdin.readline
    n, m, l = map(int, input().split())
    inf = 10 ** 20
    ar = np.zeros((n, n))
    for _ in range(m):
        a, b, c = map(int, input().split())
        ar[a - 1, b - 1] = c
        ar[b - 1, a - 1] = c
    x = floyd_warshall(ar)
    br = np.zeros((n, n))
    for i in range(n):
        for j in range(i + 1, n):
            if x[i, j] <= l:
                br[i, j] = 1
                br[j, i] = 1
    y = floyd_warshall(br)
    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        p = y[s - 1, t - 1]
        print(int(p) - 1 if p < inf else -1)


if __name__ == "__main__":
    resolve()

Solution (Python):
import numpy as np
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix
inf = float("inf")
N, M, L = map(int, input().split())
graph = np.zeros((N, N))
for _ in range(M):
    A, B, C = map(int, input().split())
    graph[A-1][B-1] = C
    graph[B-1][A-1] = C
shortest_paths = dijkstra(csr_matrix(graph), directed=False)
graph = np.full((N, N), inf)
for i in range(N):
    for j in range(N):
        if shortest_paths[i][j] <= L:
            graph[i][j] = 1

Q = int(input())
def int_(num_str):
    return int(num_str) - 1

costs = dijkstra(csr_matrix(graph), directed=False)
for _ in range(Q):
    s, t = map(int_, input().split())
    print(int(costs[s][t])-1 if costs[s][t] != inf else -1)

Solution (Python):
from heapq import *
INF = 1e10

def floyd(n):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][j] > d[i][k] + d[k][j]:
                    d[i][j] = d[i][k] + d[k][j]

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

d = [[INF]*N for i in range(N)]

for x in range(M):
    a,b,w = map(int,input().split())
    d[a-1][b-1] = w
    d[b-1][a-1] = w

floyd(N)

grafo2 = [[INF] * N for i in range(N)]

for x in range(N):
    for y in range(N) :
        if d[x][y] <= L:
            grafo2[x][y] = 1

for k in range(N):
    for i in range(N):
        for j in range(N):
            if grafo2[i][j] > grafo2[i][k] + grafo2[k][j]:
                grafo2[i][j] = grafo2[i][k] + grafo2[k][j]

query = int(input())

for x in range(query):
    s,t = map(int,input().split())
    if grafo2[s-1][t-1] == INF:
        print(-1)
    else:
        print(grafo2[s-1][t-1] - 1)

Solution (Python):
from heapq import *
INF = 1e10

def floyd(n):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][j] > d[i][k] + d[k][j]:
                    d[i][j] = d[i][k] + d[k][j]

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

d = [[INF]*N for i in range(N)]

for x in range(M):
    a,b,w = map(int,input().split())
    d[a-1][b-1] = w
    d[b-1][a-1] = w

floyd(N)

grafo2 = [[INF] * N for i in range(N)]

for x in range(N):
    for y in range(N) :
        if d[x][y] <= L:
            grafo2[x][y] = 1

for k in range(N):
    for i in range(N):
        for j in range(N):
            if grafo2[i][j] > grafo2[i][k] + grafo2[k][j]:
                grafo2[i][j] = grafo2[i][k] + grafo2[k][j]

query = int(input())

for x in range(query):
    s,t = map(int,input().split())
    if grafo2[s-1][t-1] == INF:
        print(-1)
    else:
        print(grafo2[s-1][t-1] - 1)

Solution (Python):
import numpy as np
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix
inf = float("inf")
N, M, L = map(int, input().split())
graph = np.zeros((N, N))
for _ in range(M):
    A, B, C = map(int, input().split())
    graph[A-1][B-1] = C
    graph[B-1][A-1] = C
shortest_paths = dijkstra(csr_matrix(graph), directed=False)
graph = np.full((N, N), inf)
for i in range(N):
    for j in range(N):
        if shortest_paths[i][j] <= L:
            graph[i][j] = 1

Q = int(input())
def int_(num_str):
    return int(num_str) - 1

costs = dijkstra(csr_matrix(graph), directed=False)
for _ in range(Q):
    s, t = map(int_, input().split())
    print(int(costs[s][t])-1 if costs[s][t] != inf else -1)

Solution (Python):
import numpy as np
from scipy.sparse.csgraph import shortest_path
from scipy.sparse import csr_matrix
inf = float("inf")
N, M, L = map(int, input().split())
graph = np.zeros((N, N))
for _ in range(M):
    A, B, C = map(int, input().split())
    graph[A-1][B-1] = C
    graph[B-1][A-1] = C
shortest_paths = shortest_path(csr_matrix(graph), directed=False, method="FW")
graph = np.full((N, N), inf)
for i in range(N):
    for j in range(N):
        if shortest_paths[i][j] <= L:
            graph[i][j] = 1

Q = int(input())
def int_(num_str):
    return int(num_str) - 1

costs = shortest_path(csr_matrix(graph), directed=False, method="FW")
for _ in range(Q):
    s, t = map(int_, input().split())
    print(int(costs[s][t])-1 if costs[s][t] != inf else -1)

Solution (Python):
import numpy as np
from scipy.sparse.csgraph import shortest_path
from scipy.sparse import csr_matrix
inf = float("inf")
N, M, L = map(int, input().split())
graph = np.zeros((N, N))
for _ in range(M):
    A, B, C = map(int, input().split())
    graph[A-1][B-1] = C
    graph[B-1][A-1] = C
shortest_paths = shortest_path(csr_matrix(graph), directed=False, method="D")
graph = np.full((N, N), inf)
for i in range(N):
    for j in range(N):
        if shortest_paths[i][j] <= L:
            graph[i][j] = 1

Q = int(input())
def int_(num_str):
    return int(num_str) - 1

costs = shortest_path(csr_matrix(graph), directed=False, method="D")
for _ in range(Q):
    s, t = map(int_, input().split())
    print(int(costs[s][t])-1 if costs[s][t] != inf else -1)

Solution (Python):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from itertools import accumulate, combinations, permutations, product

def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

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,w = n,m 

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

for i in range(w):
    x,y,z = abc[i]
    x,y=x-1,y-1
    d[x][y] = z
    d[y][x] = z
for i in range(n):
    d[i][i] = 0 
tmp=warshall_floyd(d)

newd = [[float("inf") for i in range(n)] for i in range(n)]

for i in range(n):
    for j in range(i+1,n):
        if d[i][j]<=l:
            newd[i][j]=1
            newd[j][i]=1
for i in range(n):
    d[i][i] = 0 

tmp=warshall_floyd(newd)

for i in range(q):
    s,t=st[i]
    s,t=s-1,t-1

    print(newd[s][t]-1 if newd[s][t]!=float('inf') else -1)

Solution (Python):
n,m,L=map(int,input().split(' '))

inf=10**10

d=[[inf]*n for _ in range(n)]
for _ 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):
            if d[i][j] > d[i][k] + d[k][j]:
                d[i][j] = d[i][k] + d[k][j]
                
dl=[[inf]*n for _ in range(n)]
for i in range(n):
    for j in range(n):
        if d[i][j] <= L:
            dl[i][j]=1

for k in range(n):
    for i in range(n):
        for j in range(n):
            if dl[i][j] > dl[i][k] + dl[k][j]:
                dl[i][j] = dl[i][k] + dl[k][j]

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

Solution (Python):
import sys

def main():
  input = sys.stdin.readline
  n, m, l = map(int, input().split())
  
  inf = pow(10, 10)
  towns = [[inf]*n for _ in range(n)]
  
  for _ in range(m):
    a, b, c = map(int, input().split())
    if c > l:
      continue
    towns[a-1][b-1] = c
    towns[b-1][a-1] = c
  
  for k in range(n):
    for i in range(n):
      for j in range(n):
        if towns[i][j] > towns[i][k]+towns[k][j] and l >= towns[i][k]+towns[k][j]:
          towns[i][j] = towns[i][k]+towns[k][j]
  
  for i in range(n):
    for j in range(n):
      if towns[i][j] == inf:
        continue
      towns[i][j] = 0
  
  for k in range(n):
    for i in range(n):
      for j in range(n):
        if towns[i][j] > towns[i][k]+towns[k][j]+1:
          towns[i][j] = towns[i][k]+towns[k][j]+1

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

if __name__ == "__main__":
  main()

Solution (Python):
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

sys.setrecursionlimit(10 ** 7)

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

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


n, m, l = na()
abc = nan(m)
q = ni()
st = nan(q)

g = [[] for _ in range(n)]
dist = [[inf] * n for _ in range(n)]
for i in range(m):
    a, b, c = abc[i]
    a -= 1
    b -= 1
    dist[a][b] = c
    dist[b][a] = c

floyd_warshall(n, dist)

ans = [[inf] * n for _ in range(n)]
for i in range(n):
    for j in range(n):
        if dist[i][j] <= l:
            ans[i][j] = 1
            ans[j][i] = 1

floyd_warshall(n, ans)

for i in range(q):
    s, t = st[i]
    s -= 1
    t -= 1
    if ans[s][t] >= inf:
        print(-1)
    else:
        print(ans[s][t] - 1)

Solution (Python):
INF = 10**10

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())
dist = [[INF for _ in range(N)] for _ in range(N)]
for i in range(N):
    dist[i][i] = 0
for _ in range(M):
    a, b, c = map(int, input().split())
    a -= 1; b -= 1
    dist[a][b] = c; dist[b][a] = c
dist = warshall_floyd(dist)

dist2 = [[INF for _ in range(N)] for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            dist2[i][i] = 0
        else:
            if dist[i][j] <= L:
                dist2[i][j] = 1
dist2 = warshall_floyd(dist2)

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)

Solution (Python):
import sys
input = sys.stdin.readline


def dijkstra(start, matrix):
    
    INF = 10 ** 18
    n = len(matrix)
    used = [False] * n
    
    
    
    
    dist = [INF] * n
    dist[start] = 0
    while True:
        v = -1
        for u in range(n):
            if not used[u] and (v == -1 or dist[u] < dist[v]):
                v = u
        if v == -1:
            break
        used[v] = True
        for nxt_v in range(n):
            tmp = merge(dist[v], matrix[v][nxt_v])
            dist[nxt_v] = min(dist[nxt_v], tmp)
    return dist

def merge(d, cost):
    div_d, mod_d = divmod(d + cost, 10 ** 15)
    if mod_d > l:
        new_d = (div_d + 1) * 10 ** 15 + cost
    else:
        new_d = d + cost
    return new_d


n, m, l = map(int, input().split())
info = [list(map(int, input().split())) for i in range(m)]
q = int(input())
query = [list(map(int, input().split())) for i in range(q)]
INF = 10 ** 18


matrix = [[INF] * n for i in range(n)]
for i in range(n):
    matrix[i][i] = 0
for a, b, cost in info:
    a -= 1
    b -= 1
    if cost > l:
        continue
    matrix[a][b] = cost
    matrix[b][a] = cost

ans = [[0] * n for i in range(n)] 
for i in range(n):
    tmp = dijkstra(i, matrix)
    for j in range(n):
        ans[i][j] = tmp[j] // (10 ** 15)

for a, b in query:
    a -= 1
    b -= 1
    if ans[a][b] == 1000:
        print(-1)
    else:
        print(ans[a][b])

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    

    inf = 10**17
    

    N,M,L = map(int, input().split())
    dp = [[inf]*N for _ in range(N)]
    for i in range(N):
        dp[i][i] = 0
    for _ in range(M):
        a,b,c = map(int, input().split())
        dp[a-1][b-1] = c
        dp[b-1][a-1] = c

    
    
    for k in range(N):
        for i in range(N):
            for j in range(N):
                if dp[i][j] > dp[i][k] + dp[k][j]:
                    dp[i][j] = dp[j][i] = dp[i][k]+dp[k][j]

    dp2 =  [[inf]*N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if dp[i][j] <= L:
                dp2[i][j] = dp2[j][i] = 1

    for k in range(N):
        for i in range(N):
            for j in range(N):
                if dp2[i][j] > dp2[i][k] + dp2[k][j]:
                    dp2[i][j] = dp2[j][i] = dp2[i][k]+dp2[k][j]

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

if __name__ == '__main__':
    main()

Solution (Python):
INF = 10 ** 14
N, M, L = map(int,input().split())

MAP1 = [[INF for i in range(N)] for j in range(N)]

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

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

MAP2 = [[INF for i in range(N)] for j in range(N)]

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

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

Q = int(input())

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

Solution (Python):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,M,L=MI()
    inf=10**15
    d=[[inf]*N for _ in range(N)]
    for i in range(M):
        a,b,c=MI()
        a-=1
        b-=1
        if c<=L:
            d[a][b]=c
            d[b][a]=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])
                
    d2=[[inf]*N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if d[i][j]<=L:
                d2[i][j]=1
                
    for k in range(N):
        for i in range(N):
            for j in range(N):
                d2[i][j]=min(d2[i][j],d2[i][k]+d2[k][j])
                
    Q=I()
    for i in range(Q):
        s,t=MI()
        s-=1
        t-=1
        ans=d2[s][t]
        if ans>=inf:
            print(-1)
        else:
            print(ans-1)
                
    

main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall

n, m, l = map(int, readline().split())
graph = [[0] * n for _ in range(n)]
inf = float('inf')
for i in range(m):
    a, b, c = map(int, readline().split())
    graph[a - 1][b - 1] = c
    graph[b - 1][a - 1] = c
cost = floyd_warshall(csr_matrix(graph))
cnt = [[0] * n for _ in range(n)]
for i, cost_i in enumerate(cost):
    for j, v in enumerate(cost_i):
        if l >= v:
            cnt[i][j] = 1
cnt = floyd_warshall(csr_matrix(cnt))
q = int(readline())
for _ in range(q):
    s, t = map(int, readline().split())
    if cnt[s - 1][t - 1] == inf:
        print(-1)
    else:
        print(int(cnt[s - 1][t - 1] - 1))

Solution (Python):
N,M,L = map(int,input().split())
INF = 10**18
d = [[INF for j in range(N)] for i in range(N)]


for _ in range(M):
    A,B,C = map(int,input().split())
    if L < C:
        continue
    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
        else:
            d[i][j] = INF


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])

Q = int(input())

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

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

INF = 10 ** 18
lst1 = [[INF] * (N + 1) for _ in range(N+1)] 

for i in range(N+1):
    lst1[i][i] = 0
for _ in range(M):
    a, b, c = map(int, input().split())
    lst1[a][b] = c
    lst1[b][a] = c


def WarshallFloyd(lst):
    for k in range(N+1):
        for i in range(N+1):
            for j in range(N+1):
                lst[i][j] = min(lst[i][j], lst[i][k] + lst[k][j])
    return


WarshallFloyd(lst1)

lst2 = [[INF] * (N + 1) for _ in range(N + 1)]
for i in range(N):
    lst2[i][i] = 0
    for j in range(i + 1, N + 1):
        if lst1[i][j] <= L:
            lst2[i][j] = 1
            lst2[j][i] = 1

WarshallFloyd(lst2)

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

Solution (Python):
def warshall_floid(d):
    for k in range(1,n+1):
        for i in range(1,n+1):
            for j in range(1,n+1):
                d[i][j] = min(d[i][j],d[i][k]+d[k][j])
    return d

n,m,l = map(int,input().split())
d = [[10**12]*(n+1) for _ in range(n+1)]
for i in range(m):
    a,b,c = map(int,input().split())
    d[a][b] = c
    d[b][a] = c
d = warshall_floid(d)
for i in range(1,n+1):
    for j in range(1,n+1):
        if d[i][j] <= l:
            d[i][j] = 1
        else:
            d[i][j] = 10**12
d = warshall_floid(d)
q = int(input())
for i in range(q):
    s,t = map(int,input().split())
    if d[s][t] >= 10**12:
        print(-1)
    else:
        print(d[s][t]-1)

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

INF = 10**20
g = [[INF]*N for _ in range(N)]
for a,b,c in ABC:
    a,b = a-1,b-1
    g[a][b] = g[b][a] = c
for i in range(N):
    g[i][i] = 0
for k in range(N):
    for i in range(N):
        for j in range(N):
            if g[i][j] > g[i][k] + g[k][j]:
                g[i][j] = g[i][k] + g[k][j]

h = [[N]*N for _ in range(N)]
for i in range(N-1):
    for j in range(i+1,N):
        if g[i][j] <= L:
            h[i][j] = h[j][i] = 1
for i in range(N):
    h[i][i] = 0
for k in range(N):
    for i in range(N):
        for j in range(N):
            if h[i][j] > h[i][k] + h[k][j]:
                h[i][j] = h[i][k] + h[k][j]

ans = []
for s,t in ST:
    s,t = s-1,t-1
    if h[s][t] == N:
        ans.append(-1)
    else:
        ans.append(h[s][t] - 1)
print(*ans, sep='\n')

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline

    inf = 1 << 60

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

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

    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]
                )

    g = [[inf] * N for _ in range(N)]  
    for A in range(N):
        for B in range(N):
            if dist[A][B] <= L:
                g[A][B] = 1

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

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


if __name__ == '__main__':
    main()