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):
from scipy.sparse import*
f=csgraph.floyd_warshall
def main():
  n,m,l,*t=map(int,open(0).read().split())
  d=[[0]*(n+1)for _ in range(n+1)]
  for a,b,c in zip(*[iter(t[:m*3])]*3):d[a][b]=c
  d=f(f(d,0)<=l)-1
  d[d>n]=-1
  d=d.astype('i').astype('U')
  print('\n'.join(d[i]for i in zip(*[iter(t[m*3+1:])]*2)))
main()

Solution (Python):
from scipy.sparse import*
f=csgraph.floyd_warshall
def main():
  n,m,l,*t=map(int,open(0).read().split())
  d=[[0]*(n+1)for _ in range(n+1)]
  for a,b,c in zip(*[iter(t[:m*3])]*3):d[a][b]=c
  d=f(f(d,0)<=l)-1
  d[d>n]=-1
  d=d.astype('U')
  print('\n'.join(d[i]for i in zip(*[iter(t[m*3+1:])]*2)))
main()

Solution (Python):
from scipy.sparse import*
f=csgraph.floyd_warshall
def main():
  n,m,l,*t=map(int,open(0).read().split())
  d=[[0]*(n+1)for _ in range(n+1)]
  for a,b,c in zip(*[iter(t[:m*3])]*3):d[a][b]=c
  d=f(f(d,0)<=l)-1
  d[d>n]=-1
  d=d.astype('i')
  print('\n'.join(map(str,[d[i]for i in zip(*[iter(t[m*3+1:])]*2)])))
main()

Solution (Python):
from scipy.sparse import*
f=csgraph.floyd_warshall
def main():
  n,m,l,*t=map(int,open(0).read().split())
  d=[[0]*(n+1)for _ in range(n+1)]
  for a,b,c in zip(*[iter(t[:m*3])]*3):d[a][b]=c
  d=f(f(d,0)<=l)-1
  d[d>n]=-1
  d=d.astype('i')
  print('\n'.join(str(d[i])for i in zip(*[iter(t[m*3+1:])]*2)))
main()

Solution (Python):
from scipy.sparse import*
def main():
  f=csgraph.floyd_warshall
  n,m,l,*t=map(int,open(0).read().split())
  d=[[0]*(n+1)for _ in range(n+1)]
  for a,b,c in zip(*[iter(t[:m*3])]*3):d[a][b]=c
  d=f(f(d,0)<=l)-1
  d[d>n]=-1
  print('\n'.join(map(str,map(int,[d[i]for i in zip(*[iter(t[m*3+1:])]*2)]))))
main()

Solution (Python):
from scipy.sparse import*
def main():
  f=csgraph.floyd_warshall
  n,m,l,*t=map(int,open(0).read().split())
  d=[[0]*(n+1)for _ in range(n+1)]
  for a,b,c in zip(*[iter(t[:m*3])]*3):d[a][b]=c
  d=f(f(d,0)<=l)-1
  d[d>n]=-1
  print('\n'.join(map(str,[d[i]for i in zip(*[iter(t[m*3+1:])]*2)])))
main()

Solution (Python):
from scipy.sparse import*
def main():
  f=csgraph.floyd_warshall
  n,m,l,*t=map(int,open(0).read().split())
  d=[[0]*(n+1)for _ in range(n+1)]
  for a,b,c in zip(*[iter(t[:m*3])]*3):d[a][b]=c
  d=f(f(d,0)<=l)-1
  d[d>n]=-1
  print('\n'.join(str(int(d[i]))for i in zip(*[iter(t[m*3+1:])]*2)))
main()

Solution (Python):
from scipy.sparse import*
def main():
  f=csgraph.floyd_warshall
  n,m,l,*t=map(int,open(0).read().split())
  d=[[0]*(n+1)for _ in range(n+1)]
  for a,b,c in zip(*[iter(t[:m*3])]*3):d[a][b]=c
  d=f(f(d,0)<=l)-1
  d[d>n]=-1
  print('\n'.join(str(d[i])for i in zip(*[iter(t[m*3+1:])]*2)))
main()

Solution (Python):
def e_travel_by_car(INF=float('inf')):
    from copy import deepcopy
    from scipy.sparse.csgraph import floyd_warshall
    N, M, L = [int(i) for i in input().split()]
    Roads = [[int(i) for i in input().split()] for j in range(M)]
    Q = int(input())
    Queries = [[int(i) for i in input().split()] for j in range(Q)]

    graph = [[INF] * i + [0] + [INF] * (N - i - 1) for i in range(N)]
    fuel = deepcopy(graph)

    for a, b, c in Roads:
        graph[a - 1][b - 1] = graph[b - 1][a - 1] = c
    dist = floyd_warshall(graph)

    for row in range(N):
        for col in range(row):
            fuel[row][col] = fuel[col][row] = int(dist[row][col] <= L)
    fuel = floyd_warshall(fuel)

    ans = [-1 if fuel[s - 1][t - 1] == INF else int(fuel[s - 1][t - 1]) - 1
           for s, t in Queries]
    return '\n'.join(map(str, ans))

print(e_travel_by_car())

Solution (Python):
N,M,L=map(int,input().split())
c=[[0]*N for i in range(N)]
for i in range(M):
    A,B,C=map(int,input().split())
    c[A-1][B-1]=c[B-1][A-1]=C
Q=int(input())
ST=[list(map(int,input().split())) for i in range(Q)]
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
g=csr_matrix(c)
d=floyd_warshall(g,directed=False)
import numpy as np
c=np.array([[1 if d[i][j]<=L else 0 for j in range(N)] for i in range(N)])
g=csr_matrix(c)
d=floyd_warshall(g,directed=False)
for s,t in ST:
    x=d[s-1][t-1]
    print(int(x)-1 if x<10**9 else -1)

Solution (Python):
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 = [[10**12 for i in range(n)] for i in range(n)]
for i in range(m):
    ai, bi, ci = map(int, input().split())
    d[ai-1][bi-1] = ci
    d[bi-1][ai-1] = ci
for i in range(n):
    d[i][i] = 0 

d = warshall_floyd(d)
for i in range(n):
    for j in range(n):
        if d[i][j] <= l:
            d[i][j] = 1
        else:
            d[i][j] = 10**12

d = warshall_floyd(d)

q = int(input())
for i in range(q):
    si, ti = map(int, input().split())
    if d[si-1][ti-1] >= 10**12:
        print(-1)
    else:
        print(d[si-1][ti-1]-1)

Solution (Python):
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 = [[10**12 for i in range(n)] for i in range(n)]
for i in range(m):
    ai, bi, ci = map(int, input().split())
    d[ai-1][bi-1] = ci
    d[bi-1][ai-1] = ci
for i in range(n):
    d[i][i] = 0 

d = warshall_floyd(d)
for i in range(n):
    for j in range(n):
        if d[i][j] <= l:
            d[i][j] = 1
        else:
            d[i][j] = 10**12

d = warshall_floyd(d)

q = int(input())
for i in range(q):
    si, ti = map(int, input().split())
    if d[si-1][ti-1] >= 10**12:
        print(-1)
    else:
        print(d[si-1][ti-1]-1)

Solution (Python):
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall

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

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

fuel = floyd_warshall(csgraph_from_dense(fuel))





dist = [[INF] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            dist[i][j] = 0
        elif fuel[i][j] <= L:
            dist[i][j] = 1

dist = floyd_warshall(csgraph_from_dense(dist))





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

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



def warchall_floyd(d, v):
    for k in range(v):
        for i in range(v):
            for j in range(v):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])


def main():
    N, M, L = map(int, input().split())
    inf = 10**10

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

    Q = int(input())

    F = [[inf]*N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if D[i][j] <= L:
                F[i][j] = 1
    warchall_floyd(F, N)

    ans = []
    for _ in range(Q):
        a, b = map(int, input().split())
        tmp = F[a-1][b-1]
        if tmp == inf:
            ans.append(-1)
        else:
            ans.append(tmp-1)
    for a in ans:
        print(a)


if __name__ == '__main__':
    main()

Solution (Python):

import sys


def input(): return sys.stdin.readline()[:-1]


sys.setrecursionlimit(10**8)
INF = 1001001001001
n, m, l = map(int, input().split())
D = [[INF]*n for i in range(n)]

for i 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):
        D[i][j] = 1 if D[i][j] <= l else INF

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, g = map(int, input().split())
    s -= 1
    g -= 1
    print(D[s][g]-1 if D[s][g] != INF else -1)

Solution (Python):
INF = 1001001001

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

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

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

dist = [[INF] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            dist[i][j] = 0
        elif fuel[i][j] <= L:
            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])

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

Solution (Python):
from scipy.sparse.csgraph import csgraph_from_dense,dijkstra,floyd_warshall
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)
N,M,L =LI()
s = []
t = []
dp = [[inf] * (N) for _ in range(N)]
for i in range(M):
    a, b, c = map(int, input().split())
    dp[a-1][b-1] = c
    dp[b-1][a-1] = c
Q = I()
for i in range(Q):
    a,b = LI()
    s.append(a-1)
    t.append(b-1)

for i in range(N):
    dp[i][i] = 0




G = csgraph_from_dense(dp)
d1 = floyd_warshall(G)

newdp = [[inf] * (N) for _ in range(N)]
for i in range(N):
    for j in range(i+1,N):
        if d1[i][j]<=L:
            newdp[i][j] = 1
            newdp[j][i] = 1

for i in range(N):
    newdp[i][i] = 0




G2 = csgraph_from_dense(newdp)
d2 = floyd_warshall(G2)

for i in range(Q):
    ans = d2[s[i]][t[i]]-1
    if ans!= inf:
        print(int(ans))
    else:
        print("-1")

Solution (Python):
n,m,l=map(int,input().split())
inf=10000000000
g=[[inf for i in range(n)] for j in range(n)]
for i in range(m):
    a,b,c=map(int,input().split())
    g[a-1][b-1]=c
    g[b-1][a-1]=c
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])
r=[[inf for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(n):
        if g[i][j]<=l:
            r[i][j]=1
        elif g[i][j]>=inf:
            r[i][j]=inf
for k in range(n):
    for i in range(n):
        for j in range(n):
            r[i][j]=min(r[i][j],r[i][k]+r[k][j])
q=int(input())
for i in range(q):
    s,e=map(int,input().split())
    if r[s-1][e-1]>=inf:
        print(-1)
    else:
        print(r[s-1][e-1]-1)

Solution (Python):



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

import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)
N,M,L =LI()
s = []
t = []
dp = [[inf] * (N) for _ in range(N)]
for i in range(M):
    a, b, c = map(int, input().split())
    dp[a-1][b-1] = c
    dp[b-1][a-1] = c
Q = I()
for i in range(Q):
    a,b = LI()
    s.append(a-1)
    t.append(b-1)

for i in range(N):
    dp[i][i] = 0


d1 = [[0] * (N) for _ in range(N)]
d1 = warshall_floyd(N,dp)

newdp = [[inf] * (N) for _ in range(N)]
for i in range(N):
    for j in range(i+1,N):
        if d1[i][j]<=L:
            newdp[i][j] = 1
            newdp[j][i] = 1

for i in range(N):
    newdp[i][i] = 0


d2 = [[0] * (N) for _ in range(N)]
d2 = warshall_floyd(N,newdp)

for i in range(Q):
    ans = d2[s[i]][t[i]]-1
    if ans!= inf:
        print(ans)
    else:
        print("-1")

Solution (Python):

import sys
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall, dijkstra
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)



n,m,l=map(int,input().split())
rinsetsu=[[0]*n for i in range(n)]
for i in range(m):
    a,b,c=map(int,input().split())
    rinsetsu[a-1][b-1]=c
    rinsetsu[b-1][a-1]=c


f = floyd_warshall(rinsetsu,directed=True)

for i in range(n):
    for j in range(n):
        f[i][j]= 1 if 1<=f[i][j]<=l else 0

f2= floyd_warshall(f,directed=True)

q = int(input())
for i in range(q):
    s,g=map(int,input().split())
    s-=1;g-=1
    print(int(f2[s][g])-1 if f2[s][g]!=float("inf") else -1)

Solution (Python):
from scipy.sparse.csgraph import floyd_warshall

n,m,l=map(int,input().split())
g=[[float('inf')]*(n+1) for _ in range(n+1)]
for _ in range(m):
  a,b,c=map(int,input().split())
  g[a][b]=c 
  g[b][a]=c
d=floyd_warshall(g)
g2=[[float('inf')]*(n+1) for _ in range(n+1)]
for i in range(1,n+1):
  for j in range(1,n+1):
    if d[i][j]<=l:
      g2[i][j]=1
d2=floyd_warshall(g2)
q=int(input())
for _ in range(q):
  a,b=map(int,input().split())
  tmp=d2[a][b]
  if tmp==float('inf'):
    print(-1)
  else:
    print(int(tmp-1))

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

def warshal_floyd(d,N):
    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

def main():
    N, M, L = map(int, input().split())
    d = [[float('inf')] * N for _ in range(N)]
    for _ in range(M):
        A, B, C = map(int, input().split())
        A -= 1
        B -= 1
        d[A][B] = C
        d[B][A] = C

    e = warshal_floyd(d,N)
    f = [[float('inf')] * N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if e[i][j] <= L:
                f[i][j] = 1

    g = warshal_floyd(f,N)
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        if g[s][t] == float('inf'):
            print(-1)
        else:
            print(g[s][t]-1)
    



if __name__ == "__main__":
    main()

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

n, m, l = map(int, input().split())
mw =[[float('inf')] * n for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    mw[a-1][b-1] = c
    mw[b-1][a-1] = c

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

d = warshall_floyd(mw)
mw =[[float('inf')] * n for _ in range(n)]
for i in range(n):
    for j in range(n):
        if d[i][j] <= l:
            mw[i][j] = 1
dd = warshall_floyd(mw)

q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    ans = dd[s-1][t-1]-1
    if ans > 10 ** 10:
        print(-1)
    else:
        print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

def dijkstra(s,n,cost,l):
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        for i in range(n):
            if (not used[i]) and (v == -1):
                v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
            break
        used[v] = True
        dist_v = d[v]
        for j,weight in enumerate(cost[v]):
            alt = dist_v + weight
            r = (dist_v - 1) // l
            if (r != (alt - 1) // l) and dist_v != 0:
                alt = (r + 1) * l + weight         
            d[j] = min(d[j],alt)
    return d
 
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)]
path = [[float("inf") for i in range(n)] for i in range(n)]
ans_list = []
for i in abc:
  a,b,cost = i[0]-1, i[1]-1, i[2]
  if cost <= l:
    path[a][b] = cost
    path[b][a] = cost
for s in range(n):
  ans_list.append(dijkstra(s,n,path,l))
for i in st:
  s, t = i[0] - 1, i[1] - 1
  ans = ans_list[s][t]
  if ans == float("inf"):
    ans = -1
  else:
    ans = (ans - 1) // l
  print(ans)

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

def dijkstra(s,n,cost,l):
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        for i in range(n):
            if (not used[i]) and (v == -1):
                v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
            break
        used[v] = True
        dist_v = d[v]
        for j, weight in enumerate(cost[v]):
            alt = dist_v + weight
            r = (dist_v - 1) // l
            if r != ((alt - 1) // l) and dist_v != 0:
                alt = (r + 1) * l + weight         
            d[j] = min(d[j],alt)
    return d

n,m,l = map(int, input().split())
path = [[float("inf") for i in range(n)] for i in range(n)]
ans_list = []
for i in range(m):
  a,b,cost = map(int, input().split())
  a -= 1
  b -= 1
  if cost <= l:
    path[a][b] = cost
    path[b][a] = cost
for s in range(n):
  ans_list.append(dijkstra(s,n,path,l))
q = int(input())

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

Solution (Python):
n,m,l=map(int,input().split())
s=[[float("INF")for i in range(n)]for i in range(n)]
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall as fw
for i in range(m):
    a,b,c=map(int,input().split())
    s[a-1][b-1]=c
    s[b-1][a-1]=c
s=fw(s)
t=[[float("INF")for i in range(n)]for i in range(n)]
for i in range(n):
    for j in range(n):
        if s[i][j]<=l:
            t[i][j]=1
t=fw(t)
for i in range(int(input())):
    a,b=map(int,input().split())
    if t[a-1][b-1]!=float("INF"):
        print(int(t[a-1][b-1])-1)
    else:
        print(-1)

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

INF = 10 ** 9 + 6
graph = [[INF] * N for _ in range(N)]

for i in range(N):
    graph[i][i] = 0

for u, v, l in edges:
    graph[u - 1][v - 1] = l
    graph[v - 1][u - 1] = l

for k in range(N):
    for i in range(N):
        if graph[i][k] == INF:
            continue
        for j in range(N):
            if graph[k][j] == INF:
                continue
            graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])

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

for i in range(N):
    cnt[i][i] = 0

for i in range(N):
    for j in range(N):
        if i == j:
            continue
        if graph[i][j] <= L:
            cnt[i][j] = 1
            cnt[j][i] = 1

for k in range(N):
    for i in range(N):
        if cnt[i][k] == INF:
            continue
        for j in range(N):
            if cnt[k][j] == INF:
                continue
            cnt[i][j] = min(cnt[i][j], cnt[i][k] + cnt[k][j])

for s, t in X:
    if cnt[s - 1][t - 1] == INF:
        print(-1)
    else:
        print(cnt[s - 1][t - 1] - 1)

Solution (Python):
N,M,L = map(int, input().split())
Edges = [map(int, input().split()) for i in range(M)]
Q = int(input())
Queries = [map(int, input().split()) for i in range(Q)]

INF = 10**18

d = [[INF]*N for i in range(N)]
for i in range(N):
    d[i][i] = 0
for a,b,c in Edges:
    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(i+1,N):
            d[i][j] = min(d[i][j], d[i][k] + d[k][j])
            d[j][i] = d[i][j]


d2 = [[INF]*N for i in range(N)]
for i in range(N):
    d2[i][i] = 0
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(i+1,N):
            d2[i][j] = min(d2[i][j], d2[i][k] + d2[k][j])
            d2[j][i] = d2[i][j]
            


for a,b in Queries:
    s = min(a,b)-1
    t = max(a,b)-1
    
    ans = d2[s][t]
    if ans == INF:
        ans = 0
    print(ans-1)

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

d = [[INF]*N for i 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(i+1,N):
            d[i][j] = min(d[i][j], d[i][k] + d[k][j])
            d[j][i] = d[i][j]


d2 = [[INF]*N for i in range(N)]
for i in range(N):
    d2[i][i] = 0
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(i+1,N):
            d2[i][j] = min(d2[i][j], d2[i][k] + d2[k][j])
            d2[j][i] = d2[i][j]
            


Q = int(input())
for i in range(Q):
    l  = list(map(int, input().split()))
    s = min(l)-1
    t = max(l)-1
    
    ans = d2[s][t]
    if ans == INF:
        ans = 0
    print(ans-1)

Solution (Python):
import sys
input=sys.stdin.readline
n,m,l=map(int,input().split())
INF=10**10
graph=[[INF for i in range(n)] for j in range(n)]
fuel=[[INF for i in range(n)] for j in range(n)]
for i in range(n):
    graph[i][i]=0
for i in range(m):
    a,b,c=map(int,input().split())
    a-=1;b-=1
    if c>l:continue
    graph[a][b],graph[b][a]=c,c
for i in range(n):
  for j in range(n):
    for k in range(n):
      graph[j][k]=min(graph[j][k],graph[j][i]+graph[i][k])
for i in range(n):
    for j in range(n):
        if graph[i][j]<=l:
            fuel[i][j]=1
for i in range(n):
  for j in range(n):
    for k in range(n):
      fuel[j][k]=min(fuel[j][k],fuel[j][i]+fuel[i][k])
q=int(input())
for i in range(q):
    s,t=map(int,input().split())
    s-=1;t-=1
    if fuel[s][t]==INF:print(-1)
    else:print(fuel[s][t]-1)

Solution (Python):
def main():

    import sys
    input = sys.stdin.readline

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

    D2 = [[float("inf") for i in range(n)] for i in range(n)]
    for i in range(m):
        D2[Edge[i][0]-1][Edge[i][1]-1] = Edge[i][2]
        D2[Edge[i][1]-1][Edge[i][0]-1] = Edge[i][2]
    for i in range(n):
        D2[i][i] = 0

    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

    D2 = warshall_floyd(D2)

    D = [[float("inf") for i in range(n)] for i in range(n)]
    for i in range(m):
        if Edge[i][2] <= l:
            D[Edge[i][0]-1][Edge[i][1]-1] = 1
            D[Edge[i][1]-1][Edge[i][0]-1] = 1
    for i in range(n):
        D[i][i] = 0
    for i in range(n):
        for j in range(n):
            if D2[i][j] <= l:
                D[i][j] = 1

    D = warshall_floyd(D)

    for query in Query:
        if D[query[0]-1][query[1]-1] != float('inf'):
            print(D[query[0]-1][query[1]-1]-1)
        else:
            print(-1)
if __name__ == '__main__':
    main()

Solution (Python):
from scipy.sparse.csgraph import floyd_warshall

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

A=[[10**12 for i in range(N+1)]for j in range(N+1)]
B=[[10**12 for i in range(N+1)]for j in range(N+1)]
for i in range(M):
    a,b,c=map(int,input().split())
    if c<=L:
        A[a][b]=c
        A[b][a]=c
        B[a][b]=1
        B[b][a]=1




A= floyd_warshall(A,directed = False)

for i in range(N+1):
    for j in range(N+1):
        if A[i][j]<=L:
            B[i][j]=1
"""for c in range(1,N+1):
    for a in range(1,N+1):
        for b in range(1,N+1):
            A[a][b]=min(A[a][b],A[a][c]+A[c][b])
            if A[a][b]<=L:
                B[a][b]=1"""


B= floyd_warshall(B,directed = False)
"""for c in range(1,N+1):
    for a in range(1,N+1):
        for b in range(1,N+1):
            B[a][b]=min(B[a][b],B[a][c]+B[c][b])"""


Q=int(input())
for i in range(Q):
    s,t=map(int,input().split())
    if B[s][t]==10**12:
        print(-1)
    else:
        print(int(B[s][t]-1))

Solution (Python):
import sys

input = sys.stdin.readline


def read_i():
    return list(map(int, input().split()))


class Graph:
    def __init__(self, n_nodes):
        self.n_nodes = n_nodes
        self.edges = [[float('inf')] * n_nodes for _ in range(n_nodes)]

    def add_edge(self, s, t, d):
        self.edges[s][t] = d
        self.edges[t][s] = d


class FloydWarshall:
    def __init__(self, graph):
        self.graph = graph
        self.distances = [[float('inf')] * graph.n_nodes
                          for _ in range(graph.n_nodes)]
        self._init()

    def _init(self):
        for x in range(self.graph.n_nodes):
            self.distances[x][x] = 0
        for source in range(self.graph.n_nodes):
            for destination, distance in enumerate(self.graph.edges[source]):
                self.distances[source][destination] = distance

    def compute_shortest_path_lengths(self):
        for k in range(self.graph.n_nodes):
            for i in range(self.graph.n_nodes):
                for j in range(self.graph.n_nodes):
                    s = self.distances[i][k] + self.distances[k][j]
                    if s < self.distances[i][j]:
                        self.distances[i][j] = s
        return self.distances


n, m, l = read_i()
graph = Graph(n)
for _ in range(m):
    a, b, c = read_i()
    if c > l:
        continue
    graph.add_edge(a - 1, b - 1, c)
fw = FloydWarshall(graph)
ds = fw.compute_shortest_path_lengths()

graph = Graph(n)
for s in range(n):
    for t in range(s + 1, n):
        if ds[s][t] > l:
            continue
        graph.add_edge(s, t, 1)
fw = FloydWarshall(graph)
ds = fw.compute_shortest_path_lengths()

q = int(input())
for _ in range(q):
    s, t = read_i()
    d = ds[s - 1][t - 1] - 1
    if d == float('inf'):
        print(-1)
    else:
        print(d)

Solution (Python):
import sys

input = sys.stdin.readline


def read_i():
    return list(map(int, input().split()))


class Graph:
    def __init__(self, n_nodes):
        self.n_nodes = n_nodes
        self.edges = [[float('inf')] * n_nodes for _ in range(n_nodes)]

    def add_edge(self, s, t, d):
        self.edges[s][t] = d
        self.edges[t][s] = d


class FloydWarshall:
    def __init__(self, graph):
        self.graph = graph
        self.distances = [[float('inf')] * graph.n_nodes
                          for _ in range(graph.n_nodes)]
        self._init()

    def _init(self):
        for x in range(self.graph.n_nodes):
            self.distances[x][x] = 0
        for source in range(self.graph.n_nodes):
            for destination, distance in enumerate(self.graph.edges[source]):
                self.distances[source][destination] = distance

    def compute_shortest_path_lengths(self):
        for k in range(self.graph.n_nodes):
            for i in range(self.graph.n_nodes):
                for j in range(self.graph.n_nodes):
                    s = self.distances[i][k] + self.distances[k][j]
                    if s < self.distances[i][j]:
                        self.distances[i][j] = s
        return self.distances


n, m, l = read_i()
graph = Graph(n)
for _ in range(m):
    a, b, c = read_i()
    if c > l:
        continue
    graph.add_edge(a - 1, b - 1, c)
fw = FloydWarshall(graph)
ds = fw.compute_shortest_path_lengths()

graph = Graph(n)
for s in range(n):
    for t in range(n):
        if s >= t:
            continue
        if ds[s][t] > l:
            continue
        graph.add_edge(s, t, 1)
fw = FloydWarshall(graph)
ds = fw.compute_shortest_path_lengths()

q = int(input())
for _ in range(q):
    s, t = read_i()
    s -= 1
    t -= 1
    d = ds[s][t] - 1
    if d == float('inf'):
        print(-1)
    else:
        print(d)