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

INF = float('inf')
d1 = [[INF]*N for _ in range(N)]


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

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

for i in range(N):
    for j in range(N):
        if i == j:
            d2[i][j] = 0
        if d1[i][j] <= L and i != j:
            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][k] + d2[k][j], d2[i][j])
Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    if d2[s][t] == INF:
        print(-1)
    else:
        print(d2[s][t]-1)

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

"""
g=[[] for _ in range(n)]
for a,b,c in abc:
  if c>l:continue
  a,b=a-1,b-1
  g[a].append([b,c])
  g[b].append([a,c])
"""
q=int(input())
st=[list(map(int,input().split())) for _ 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
inf=float('inf')
d=[[inf]*n for _ in range(n)]

for a,b,c in abc:
  a,b=a-1,b-1
  d[a][b]=c
  d[b][a]=c
for i in range(n):
  d[i][i]=0
d=warshall_floyd(d)
nabc=[]
dd=[[inf]*n for _ in range(n)]

for i in range(n):
  for j in range(n):
    if d[i][j]<=l:
      dd[i][j]=1
for i in range(n):
  dd[i][i]=0
dd=warshall_floyd(dd)
for s,t in st:
  s,t=s-1,t-1
  tmp=dd[s][t]
  print(max(0,tmp-1) if tmp!=inf else -1)

Solution (Python):
n,m,l=map(int,input().split())
inf=100000000000
wf=[[inf]*n for i in range(n)]
for i in range(m):
    a,b,c=map(int,input().split())
    wf[a-1][b-1]=c
    wf[b-1][a-1]=c
for i in range(n):
    wf[i][i]=0
for k in range(n):
    for i in range(n):
        for j in range(n):
            wf[i][j]=min(wf[i][j],wf[i][k]+wf[k][j])
for i in range(n):
    for j in range(n):
        if i!=j:
            wf[i][j]=1 if wf[i][j]<=l else inf
for k in range(n):
    for i in range(n):
        for j in range(n):
            wf[i][j]=min(wf[i][j],wf[i][k]+wf[k][j])
q=int(input())
for i in range(q):
    s,t=map(int,input().split())
    x=wf[s-1][t-1]
    print(-1 if x==inf else wf[s-1][t-1]-1)

Solution (Python):
import sys
input = sys.stdin.readline
N, M, L = map(int, input().split())
d = [[10 ** 16 * (i != j) for j in range(N + 1)] for i in range(N + 1)]
for _ in range(M):
  x, y, c = map(int, input().split())
  d[x][y] = c
  d[y][x] = c
for k in range(N + 1):
  for i in range(N + 1):
    for j in range(N + 1): d[i][j] = min(d[i][j], d[i][k] + d[k][j])

dd = [[(d[i][j] <= L) + (d[i][j] > L) * 10 ** 16 for j in range(N + 1)] for i in range(N + 1)]
for k in range(N + 1):
  for i in range(N + 1):
    for j in range(N + 1): dd[i][j] = min(dd[i][j], dd[i][k] + dd[k][j])

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

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


def main():
    import heapq
    import sys
    input = sys.stdin.buffer.readline
    N, M, L = map(int, input().split())

    G = [[-1]*N for i in range(N)]

    for i in range(M):
        A, B, C = map(int, input().split())
        A -= 1
        B -= 1
        G[A][B] = C
        G[B][A] = C
    g = csgraph_from_dense(G, null_value=-1)
    D = shortest_path(g)
    G2 = [[-1]*N for i in range(N)]
    for i in range(N):
        for j in range(N):
            if D[i][j] >= 0 and D[i][j] <= L:
                G2[i][j] = 1

    g2 = csgraph_from_dense(G2, null_value=-1)
    D = shortest_path(g2)

    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        if D[s][t] <= N**2:
            print(int(D[s][t])-1)
        else:
            print(-1)


main()

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


def main():
    import heapq
    import sys
    input = sys.stdin.buffer.readline
    N, M, L = map(int, input().split())

    G = [[-1]*N for i in range(N)]

    for i in range(M):
        A, B, C = map(int, input().split())
        A -= 1
        B -= 1
        G[A][B] = C
        G[B][A] = C
    g = csgraph_from_dense(G, null_value=-1)
    D = dijkstra(g)
    G2 = [[-1]*N for i in range(N)]
    for i in range(N):
        for j in range(N):
            if D[i][j] >= 0 and D[i][j] <= L:
                G2[i][j] = 1

    g2 = csgraph_from_dense(G2, null_value=-1)
    D = dijkstra(g2)


    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        if D[s][t] <= N**2:
            print(int(D[s][t])-1)
        else:
            print(-1)


main()

Solution (Python):

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

N,M,L = map(int, input().split())
V = [[ 10**12 for _ in range(N) ] for _ in range(N)]
for _ in range(M):
  a,b,c = map(int, input().split())
  a,b = a-1, b-1
  V[a][b] = c
  V[b][a] = c
  
G = csgraph_from_dense(V, null_value=10**12)
d = floyd_warshall(G)

V2 = [[ 10**9 for _ in range(N) ] for _ in range(N)]
for i in range(N):
  for j in range(N):
    if 0 < d[i][j] <= L:
      V2[i][j] = 1
      
G2 = csgraph_from_dense(V2, null_value=10**9)
d2 = floyd_warshall(G2)

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

Solution (Python):
import sys

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  

N,M,L = LI()

class WarshallFloyd():
    def __init__(self,N):  
        self.N = N
        self.d = [[float('inf')]*N for _ in range(N)]  
    def add(self,u,v,c,directed = False):
        if directed is False:  
            self.d[u][v] = c
            self.d[v][u] = c
        else:  
            self.d[u][v] = c
    def WarshallFloyd_search(self):
        for k in range(self.N):
            for i in range(self.N):
                for j in range(self.N):
                    self.d[i][j] = min(self.d[i][j],self.d[i][k] + self.d[k][j])
        for i in range(self.N):
            self.d[i][i] = 0
        return self.d

G1 = WarshallFloyd(N)

for i in range(M):
    a,b,c = LI()
    G1.add(a-1,b-1,c,directed=False)

G1.WarshallFloyd_search()

G2 = WarshallFloyd(N)

for i in range(N-1):
    for j in range(i+1,N):
        if G1.d[i][j] <= L:
            G2.add(i,j,1,directed=False)

G2.WarshallFloyd_search()

Q = I()
for i in range(Q):
    s,t = LI()
    if G2.d[s-1][t-1] == float('inf'):
        print(-1)
    else:
        print(G2.d[s-1][t-1]-1)

Solution (Python):
INF = 10**12

def warshall_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 = [ [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

    warshall_floyd(d, n)

    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

    warshall_floyd(d2,n)
    ans = []
    q = int(input())
    for _ in range(q):
        s,t = map(int, input().split())
        dist = d2[s-1][t-1]
        if dist == INF: dist = 0
        ans.append(dist-1)

    for a in ans:
        print(a)

if __name__ == "__main__":
    main()

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import defaultdict
    n, m, l = map(int, readline().split())
    dist = [[INF] * n for _ in range(n)]

    for _ in range(m):
        a, b, c = map(int, readline().split())
        a, b = a - 1, b - 1
        dist[a][b] = c
        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])

    dist2 = [[INF] * n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            if dist[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(readline())

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


if __name__ == '__main__':
    main()

Solution (Python):


import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)
inf = float("inf")

import numpy as np
import scipy.sparse.csgraph as csg

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

    
    A = np.array([[inf]*N]*N)
    for a,b,c in ABC:
        A[a-1][b-1] = c
        A[b-1][a-1] = c
    A = csg.floyd_warshall(A)
    
    
    B = np.array([[inf]*N]*N)
    for i in range(N):
        for j in range(N):
            if A[i][j] <= L:
                B[i][j] = 1
    
    B = csg.floyd_warshall(B)
    
    for s,t in ST:
        if B[s-1][t-1] == inf:
            print(-1)
        else:
            print(int(B[s-1][t-1] - 1))

if __name__ == "__main__":
    main()

Solution (Python):
import sys
I=sys.stdin.readlines()
N,M,L=map(int,I[0].split())
a,b,c=0,0,0
D=[[L+1]*N for i in range(N)]
for i in range(M):
  a,b,c=map(int,I[i+1].split())
  a,b=a-1,b-1
  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=[[N*N+2]*N for i in range(N)]
for i in range(N):
  D2[i][i]=0
  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=int(I[M+1])
for i in range(Q):
  a,b=map(int,I[i+2+M].split())
  if N<D2[a-1][b-1]:
    print(-1)
  else:
    print(D2[a-1][b-1]-1)

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

inf = 10 ** 9 + 7
graph = [[inf] * N for _ in range(N)]
count = [[inf] * N for _ in range(N)]

for i in range(N):
    graph[i][i] = 0
    count[i][i] = 0
    
for a, b, c in X:
    graph[a - 1][b - 1] = c
    graph[b - 1][a - 1] = c

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

for i in range(N):
    for j in range(N):
        if i != j and graph[i][j] <= L:
            count[i][j] = 1

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

for s, t in Y:
    if count[s - 1][t - 1] == inf:
        print(-1)
    else:
        print(count[s - 1][t - 1] - 1)

Solution (Python):
N,M,L=map(int,input().split())
A=[[] for i in range(N)]
for i in range(M):
    a,b,c=map(int,input().split())
    A[a-1].append((b-1,c))
    A[b-1].append((a-1,c))
d=[[100000000001]*N for i in range(N)]
for i in range(N):
    d[i][i]=0
for i in range(N):
    for to,cost in A[i]:
        d[i][to] = cost
for k in range(N):
    for i in range(N):
        for j in range(N):
            if d[i][k] != 100000000001 and d[k][j] != 100000000001:
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
B=[[] for i in range(N)]
for i in range(N):
    for j in range(N):
        if d[i][j]<=L:
            B[i].append((j,1))
times=[[100000000001]*N for i in range(N)]
for i in range(N):
    times[i][i]=0
for i in range(N):
    for to,cost in B[i]:
        times[i][to] = cost
for k in range(N):
    for i in range(N):
        for j in range(N):
            if times[i][k] != 100000000001 and times[k][j] != 100000000001:
                times[i][j] = min(times[i][j], times[i][k] + times[k][j])
Q=int(input())
for i in range(Q):
    s,t=map(int,input().split())
    if times[s-1][t-1]!=100000000001:
    	print(times[s-1][t-1]-1)
    else:
      	print(-1)

Solution (Python):
def main():
    INF = 10 ** 12
    N, M, L = list(map(int, input().split(' ')))
    D = [[INF for _ in range(N)] for _ in range(N)]
    for _ in range(M):
        A, B, C = list(map(int, input().split(' ')))
        A -= 1
        B -= 1
        D[A][B] = C
        D[B][A] = C
    Q = int(input())
    query_list = [list(map(lambda x: int(x) - 1, input().split(' '))) for _ in range(Q)]
    
    for via in range(N):
        for s in range(N):
            for t in range(N):
                d = D[s][via] + D[via][t]
                if D[s][t] > d:
                    D[s][t] = d
                    D[t][s] = d
    
    counts = [[INF for _ in range(N)] for _ in range(N)]
    for s in range(N):
        for t in range(N):
            if D[s][t] <= L:
                counts[s][t] = 1
                counts[t][s] = 1
    for via in range(N):
        for s in range(N):
            for t in range(N):
                d = counts[s][via] + counts[via][t]
                if counts[s][t] > d:
                    counts[s][t] = d
                    counts[t][s] = d
    
    for query in query_list:
        s, t = query
        if counts[s][t] >= INF:
            print(-1)
        else:
            print(counts[s][t] - 1)


if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
from scipy.sparse.csgraph import shortest_path
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse.csgraph import csgraph_from_dense

INF = float('inf')

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

    
    G = [[INF] * (N+1) for n in range(N+1)]

    for i in range(M):
        a, b, c = map(int, input().split())
        G[a][b] = c
        G[b][a] = c
    G = csgraph_from_dense(G, null_value=INF)

    Q = int(input())
    Queries = []
    for i in range(Q):
        s, t = map(int, input().split())
        Queries.append((s, t))

    
    cost = floyd_warshall(G)
    
    
    
    
    no_refuel = [[1 if x <= L else INF for x in c] for c in cost]
    no_refuel = csgraph_from_dense(no_refuel, null_value=INF)
    refuel_count = floyd_warshall(no_refuel)

    ans = []
    for s, t in Queries:
        d = refuel_count[s][t]
        ans.append(int(d-1) if d != INF else -1)
    
    print(*ans, sep='\n')

if __name__ == '__main__':
    main()

Solution (Python):
INF = float('inf')

def warshall_floyd(G):
    
    
    
    cost = [g[:] for g in G]
    N = len(G)
    
    for k in range(N):
        for i in range(N):
            for j in range(N):
                if cost[i][k] != INF and cost[k][j] != INF:
                    cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])
    return cost

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

    
    G = [[INF] * (N+1) for n in range(N+1)]

    for i in range(M):
        a, b, c = map(int, input().split())
        G[a][b] = c
        G[b][a] = c

    Q = int(input())
    Queries = []
    for i in range(Q):
        s, t = map(int, input().split())
        Queries.append((s, t))

    
    cost = warshall_floyd(G)

    
    
    
    
    no_refuel = [[1 if x <= L else INF for x in c] for c in cost]

    refuel_count = warshall_floyd(no_refuel)

    ans = []
    for s, t in Queries:
        d = refuel_count[s][t]
        ans.append(d-1 if d != INF else -1)
    
    print(*ans, sep='\n')

if __name__ == '__main__':
    main()

Solution (Python):
from scipy.sparse import*
f=csgraph.johnson
n,m,l,*t=map(int,open(0).read().split())
m*=3
[*map(print,f(f(csr_matrix((t[2:m:3],(t[:m:3],t[1:m:3])),[n+1]*2),0)<=l)[t[m+1::2],t[m+2::2]].clip(0,n)%n-1)]

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

import math




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

import numpy as np
from scipy.sparse.csgraph import shortest_path
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse.csgraph import csgraph_from_dense
INF = 10 ** 9 + 1
G = [[INF for i in range(N)] for j in range(N)]
for i in range(M):
  a,b,c = map(int,readline().split())
  G[a - 1][b - 1] = c
  G[b - 1][a - 1] = c
G = csgraph_from_dense(G, null_value=INF)
d = floyd_warshall(G)

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

EG = csgraph_from_dense(EG, null_value=INF)
d = floyd_warshall(EG)

Q = int(readline())
for i in range(Q):
  s,t = map(int,readline().split())
  if d[s - 1][t - 1] != math.inf:
    print(int(d[s - 1][t - 1] - 1))
  else:
    print(-1)

Solution (Python):
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string



def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')



input=lambda: sys.stdin.readline().rstrip()

show_flg=False
show_flg=True

def wf(d):
    n=len(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=MI()
d=[[inf]*n for _ in range(n)]
for i in range(m):
    a,b,c=LI_()
    d[a][b]=c+1
    d[b][a]=c+1

d=wf(d)
g=[[inf]*n for i in range(n)]

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

q=I()
for _ in range(q):
    s,t=LI_()
    if g[s][t]==inf:
        ans=-1
    else:
        ans=g[s][t]-1
    print(ans)

Solution (Python):


N, M, L = [int(s) for s in input().split()]
edge = [[int(s) for s in input().split()] for _ in range(M)]
dist = [[10 ** 10] * N for _ in range(N)]
graph = [[] for _ in range(N)]
Q = int(input())

for i, j, w in edge:
    dist[i - 1][j - 1] = w
    dist[j - 1][i - 1] = w

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

for i in range(N):
    for j in range(N):
        dist[i][j] = 1 if dist[i][j] <= L else 10 ** 10

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


for _ in range(Q):
    s, t = [int(a) - 1 for a in input().split()]
    print(dist[s][t] - 1 if dist[s][t] < 10**10 else -1)

Solution (Python):
inf = 10 ** 13

n, m, l = map(int, input().split())

d = [[inf] * n for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    d[a - 1][b - 1] = 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])

e = [[1 if d[i][j] <= l else inf for j in range(n)] for i in range(n)]
for k in range(n):
    for i in range(n):
        for j in range(n):
            e[i][j] = min(e[i][j], e[i][k] + e[k][j])

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

Solution (Python):
import sys
from heapq import heappop, heappush
from scipy.sparse.csgraph import floyd_warshall
input = sys.stdin.readline
 
def main():
    n,w,l = map(int,input().split())
    d = [[10**15 for _ in range(n)] for _ in range(n)]
    for _ in range(w):
        x,y,z = map(int,input().split())
        x -= 1
        y -= 1
        if z > l:
            continue
        d[x][y] = z
        d[y][x] = z
    for i in range(n):
        d[i][i] = 0
    d = floyd_warshall(d)
    G = [[10**15 for _ in range(n)] for _ in range(n)]
    for i in range(n-1):
        for j in range(i+1,n):
            if d[i][j] <= l:
                G[i][j] = 1
                G[j][i] = 1
    for i in range(n):
        G[i][i] = 0
    G = floyd_warshall(G)
    q = int(input())
    for _ in range(q):
        s,t = map(int,input().split())
        s -= 1
        t -= 1
        ans = G[s][t]
        if ans == 10**15:
            print(-1)
            continue
        print(int(ans)-1)
    
if __name__ == "__main__":
    main()

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

N, M, L = map(int, input().split())
INF = 10**18
G = [[INF]*N for i in range(N)]
for i in range(M):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    if c <= L:
        G[a][b] = c
        G[b][a] = 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])
G2 = [[INF]*N for i in range(N)]
for i in range(N):
    for j in range(N):
        if G[i][j] <= L:
            G2[i][j] = 1
for k in range(N):
    for i in range(N):
        for j in range(N):
            G2[i][j] = min(G2[i][j], G2[i][k]+G2[k][j])
Q = int(input())
ans = []
for i in range(Q):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    if G2[s][t] == INF:
        ans.append(-1)
    else:
        ans.append(G2[s][t]-1)
print(*ans, sep="\n")

Solution (Python):
N, M, L = map(int, input().split())
INF = 10**18
G = [[INF]*N for i in range(N)]
for i in range(M):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    if c <= L:
        G[a][b] = c
        G[b][a] = 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])
G2 = [[INF]*N for i in range(N)]
for i in range(N):
    for j in range(N):
        if G[i][j] <= L:
            G2[i][j] = 1
for k in range(N):
    for i in range(N):
        for j in range(N):
            G2[i][j] = min(G2[i][j], G2[i][k]+G2[k][j])
Q = int(input())
ans = []
for i in range(Q):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    if G2[s][t] == INF:
        ans.append(-1)
    else:
        ans.append(G2[s][t]-1)
print(*ans, sep="\n")

Solution (Python):

"""
E - Travel by Car
https://atcoder.jp/contests/abc143/tasks/abc143_e

"""
import sys


from scipy.sparse.csgraph import floyd_warshall

def main(args):
    N, M, L = map(int, input().split())
    g1 = [[float('inf')] * (N+1) for _ in range(N+1)]
    for i in range(N+1):
        g1[i][i] = 0
    for _ in range(M):
        A, B, C = map(int, input().split())
        g1[A][B] = g1[B][A] = C
    d1 = floyd_warshall(g1)
    g2 = [[float('inf')] * (N+1) for _ in range(N+1)]
    for i in range(N+1):
        for j in range(N+1):
            if i == j:
                g2[i][j] = 0
            elif d1[i][j] <= L:
                g2[i][j] = g2[j][i] = 1
    d2 = floyd_warshall(g2)
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        print(-1 if d2[s][t] == float('inf') else int(d2[s][t]) - 1)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
import sys
from collections import defaultdict
from queue import deque
readline = sys.stdin.buffer.readline



def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    N, M, L = geta(int)
    INF = 10**10

    d = [[INF] * (N + 1) for _ in range(N + 1)]

    for i in range(1, N + 1):
        d[i][i] = 0

    for _ in range(M):
        a, b, c = geta(int)
        d[a][b] = c
        d[b][a] = c

    def warshall_froyd(_d):
        for k in range(1, N + 1):
            for i in range(1, N + 1):
                for j in range(1, N + 1):
                    l = _d[i][k] + _d[k][j]
                    if _d[i][j] > l:
                        _d[i][j] = l

    warshall_froyd(d)

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

    warshall_froyd(f)

    for _ in range(gete(int)):
        s, t = geta(int)
        ret = f[s][t]
        print(ret - 1 if ret != INF else -1)


if __name__ == "__main__":
    main()

Solution (Python):
n, m, l = map(int, input().split())
def warshall_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])

inf = 10 ** 15
d = [[inf] * n for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    d[a-1][b-1] = min(d[a-1][b-1], c)
    d[b-1][a-1] = min(d[b-1][a-1], c)

warshall_floyd(d, n)

e = [[inf] * n for _ in range(n)]
for i in range(n):
    for j in range(i+1, n):
        if d[i][j] <= l:
            e[i][j] = min(e[i][j], 1)
            e[j][i] = min(e[j][i], 1)

warshall_floyd(e, n)


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

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)

def wff(n,l,abc,st):
    nl = list(range(n))
    d = [[inf] * n for _ in nl]
    for i in nl:
        d[i][i] = 0
    for a,b,c in abc:
        a -= 1
        b -= 1
        d[a][b] = c
        d[b][a] = c

    for i in nl:
        di = d[i]
        for j in nl:
            if i == j:
                continue
            dj = d[j]
            for k in nl:
                if i != k and j != k and dj[k] > dj[i] + di[k]:
                    dj[k] = dj[i] + di[k]

    dd = [[inf] * n for _ in nl]
    for i in nl:
        dd[i][i] = 0
    for i in nl:
        for j in nl:
            if j != i and d[i][j] <= l:
                dd[i][j] = 1

    for i in nl:
        di = dd[i]
        for j in nl:
            if i == j:
                continue
            dj = dd[j]
            for k in nl:
                if i != k and j != k and dj[k] > dj[i] + di[k]:
                    dj[k] = dj[i] + di[k]

    rr = []
    for s,t in st:
        if dd[s][t] == inf:
            rr.append(-1)
        else:
            rr.append(dd[s][t] - 1)

    return JA(rr, "\n")


def main():
    n,m,l = LI()
    abc = [LI() for _ in range(m)]
    q = I()
    st = [LI_() for _ in range(q)]

    if m > 300 * 100:
        return wff(n,l,abc,st)

    e = collections.defaultdict(list)
    for a,b,c in abc:
        a -= 1
        b -= 1
        e[a].append((b, c))
        e[b].append((a, c))

    def search(s):
        d = collections.defaultdict(lambda: inf)
        d[s] = 0
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True

            for uv, ud in e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv] > vd and vd <= l:
                    d[uv] = vd
                    heapq.heappush(q, (vd, uv))

        return set([k for k,v in d.items() if v <= l])

    d = {}
    for i in range(n):
        d[i] = search(i)

    dd = [[inf] * n for _ in range(n)]
    for i in range(n):
        q = [(0, i)]
        qi = 0
        v = set([i])
        while len(q) > qi:
            k, t = q[qi]
            qi += 1
            u = d[t] - v
            v |= u
            dd[i][t] = k
            for w in u:
                q.append((k+1, w))

    rr = []
    for s,t in st:
        if dd[s][t] == inf:
            rr.append(-1)
        else:
            rr.append(dd[s][t] - 1)


    return JA(rr, "\n")

print(main())