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
input=sys.stdin.readline
n,m,l=map(int,input().split())
G=[[] for i in range(n)]
for _ in range(m):
    a,b,c=map(int,input().split())
    if c>l:
        pass
    else:
        G[a-1].append((b-1,c))
        G[b-1].append((a-1,c))
INF=10**9
ans=[]
for i in range(n):
    d=[(INF,INF)]*n
    used=[False]*n
    d[i]=(0,l)
    while True:
        v=-1
        for j in range(n):
            if not used[j]:
                if v==-1:
                    v=j
                else:
                    if d[j][0]<d[v][0]:
                        v=j
                    elif d[j][0]==d[v][0]:
                        if d[j][1]>d[v][1]:
                            v=j
        if v==-1:
            break
        used[v]=True
        for j in G[v]:
            if d[v][1]<j[1]:
                x=d[v][0]+1
                y=l-j[1]
            else:
                x=d[v][0]
                y=d[v][1]-j[1]
            if x<d[j[0]][0]:
                d[j[0]]=(x,y)
            elif x==d[j[0]][0]:
                if y>d[j[0]][1]:
                    d[j[0]]=(x,y)
    ans.append(d)
q=int(input())
for i in range(q):
    s,t=map(int,input().split())
    if ans[s-1][t-1]==(INF,INF):
        print(-1)
    else:
        print(ans[s-1][t-1][0])

Solution (Python):
def dijkstra(s,n,w,cost):
    
    
    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
               
        for j in range(n):
               d[j] = min(d[j],d[v]+cost[v][j])
    return d



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

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

for i in range(m):
    x,y,z = map(int,input().split())
    cost[x-1][y-1] = z
    cost[y-1][x-1] = z

kyolist=[]
for i in range(n):
    kyolist.append(dijkstra(i,n,m,cost))
zerolist=[]

for i in range(n):
    nowzero=[]
    for j in range(n):
        if i!=j and kyolist[i][j]<=l:
            nowzero.append(j)
    zerolist.append(nowzero)


llist=[]
for i in range(n):
    nowlist=[1000]*n
    nowlist[i]=0
    next=[i]
    now=0
    while next!=[]:
        newnext=[]
        for nx in next:
            for ima in zerolist[nx]:
                if nowlist[ima]==1000:
                    nowlist[ima]=now
                    newnext.append(ima)
        next=newnext
        now+=1
    llist.append(nowlist)

q=int(input())
for i in range(q):
    s, t = (int(i) for i in input().split())
    kotae=llist[s-1][t-1]
    if kotae==1000:
        print(-1)
    else:
        print(kotae)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m,l = LI()
    v = [[] for i in range(n)]
    for i in range(m):
        a,b,c = LI()
        a -= 1
        b -= 1
        v[a].append((b,c))
        v[b].append((a,c))
    ans = [[[float("inf")]*2 for j in range(n)] for i in range(n)]
    q = []
    for s in range(n):
        ans[s][s] = [0,0]
        heappush(q,(0,0,s))
        while q:
            da,dx,x = heappop(q)
            if [da,dx] > ans[s][x]:
                continue
            for y,w in v[x]:
                if w > l:
                    continue
                nd = dx+w
                if nd <= l:
                    na = da
                    nd_ = nd
                else:
                    na = da+1
                    nd_ = w
                if [na,nd_] < ans[s][y]:
                    ans[s][y] = [na,nd_]
                    heappush(q,(na,nd_,y))
    q = I()
    for _ in range(q):
        s,t = LI()
        s -= 1
        t -= 1
        ds = ans[s][t][0]
        if ds == float("inf"):
            print(-1)
        else:
            print(ds)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
N,M,L = map(int,input().split())
INF = (500, 10**9)
dist = [[INF if i != j else (0,0) for i in range(N)] for j in range(N)]
edge = [[] for i in range(N)]
for i in range(M):
	A,B,C = map(int,input().split())
	if C <= L:
		edge[A-1].append((B-1, C))
		edge[B-1].append((A-1, C))

import heapq as hq
for i in range(N):
	searched = [False for j in range(N)]
	n = i
	while n != None:
		searched[n] = True
		for e in edge[n]:
			if searched[e[0]]:
				continue
			d = dist[i][n]
			n2, d2 = e[0], e[1]
			if d[1] + d2 > L:
				new_d = (d[0] + 1, d2)
			else:
				new_d = (d[0], d[1] + d2)
			dist[i][n2] = min(dist[i][n2], new_d)
		n = None
		m = INF
		for j in range(N):
			if not searched[j] and dist[i][j] < m:
				n = j
				m = dist[i][j]


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

Solution (Python):
import sys
from heapq import heappush, heappop
input = sys.stdin.readline
def inpl(): return list(map(int, input().split()))

N, M, L = inpl()
G = [[] for _ in range(N)]
G2 = [[999]*(N) for _ in range(N)]

for _ in range(M):
    a, b, c = inpl()
    if c <= L:
        G[a-1].append([b-1, c])
        G[b-1].append([a-1, c])

for i in range(N):
    supply = [999]*N
    remain = [0]*N
    supply[i] = 1
    remain[i] = L
    Q = [(1, -L, i)]
    while Q:
        s, r, p = heappop(Q)
        r = -r
        if s > supply[p]:
            continue
        if (s == supply[p]) and (r < remain[p]):
            continue
        for q, c in G[p]:
            if r >= c:
                if supply[q] > s:
                    supply[q] = s
                    remain[q] = r - c
                    heappush(Q, (s, c-r, q))
                elif (supply[q] == s) & (remain[q] < r - c):
                    remain[q] = r-c
                    heappush(Q, (s, c-r, q))
            else:
                if supply[q] > (s + 1):
                    supply[q] = s + 1
                    remain[q] = L - c
                    heappush(Q, (s+1, c-L, q))
                elif (supply[q] == (s+1)) & (remain[q] < L - c):
                    supply[q] = s+1
                    remain[q] = L-c
                    heappush(Q, (s+1, c-L, q))
    for j in range(N):
        G2[i][j] = supply[j]


for _ in range(int(input())):
    s, t = inpl()
    if G2[s-1][t-1] == 999:
        print(-1)
    else:
        print(G2[s-1][t-1]-1)

Solution (Python):
inf = 10**10
def warshall_floyd(d):
    for i in range(N):
        for a in range(N):
            for b in range(N):
                d[a][b] = min(d[a][b], d[a][i]+ d[i][b])
    return d
N, M, L = map(int, input().split())
G = [[inf]*N for _ in range(N)]
for i in range(N):
    G[i][i] = 0
for _ in range(M):
    a, b, c = map(int, input().split())
    G[a-1][b-1] = c
    G[b-1][a-1] = c
G = warshall_floyd(G)
G2 = [[0]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            G2[i][j] = 0
        elif G[i][j]<=L:
            G2[i][j] = 1
        else:
            G2[i][j] = inf
d = warshall_floyd(G2)
Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    ans = d[s-1][t-1]-1
    print(ans if ans < 10**9 else -1)

Solution (Python):
from scipy.sparse.csgraph import floyd_warshall
import numpy as np
n,m,l,*L=map(int,open(0).read().split())
abc=L[:3*m];st=L[3*m+1:]
town=[[float("inf")]*n for _ in range(n)]
for a,b,c in zip(*[iter(abc)]*3):
	if c<=l:
		town[a-1][b-1]=c
fw=floyd_warshall(town,0)
ti=floyd_warshall(np.where(fw<=l,1,fw),0)
for s,t in zip(*[iter(st)]*2):
	if ti[s-1,t-1]==float("inf"):
		print(-1)
	else:
		print(int(ti[s-1,t-1]-1))

Solution (Python):
import numpy as np
from scipy.sparse.csgraph import floyd_warshall
import sys
input = sys.stdin.readline

def main():
    INF = float("Inf")
    n,m,l = map(int, input().split())
    G = [[INF]*n for i in range(n)]
    for i in range(n):
        G[i][i] = 0
    for i in range(m):
        a,b,c = map(int, input().split())
        a,b = a-1,b-1
        if c <= l:
            G[a][b] = G[b][a] = c
    G = floyd_warshall(G, directed=False)
    G = np.where(G <=l, 1, INF)
    for i in range(n):
        G[i][i] = 0
    GG = floyd_warshall(G, directed=False)

    q = int(input())
    ans = []
    for i in range(q):
        s,t = map(int, input().split())
        a = GG[s-1][t-1]
        if a == INF:
            a = 0
        else:
            a = int(a)
        ans.append(a-1)
    print(*ans, sep="\n")

if __name__ == "__main__":
    main()

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

n, m, l = [int(item) for item in input().split()]
dist = [[INF] * n for _ in range(n)]
for i in range(m):
    a, b, c = [int(item) for item in input().split()]
    a -= 1; b -= 1
    dist[a][b] = c
    dist[b][a] = c

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

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

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 = [int(item) - 1 for item in input().split()]
    if dist[s][t] == INF:
        print(-1)
    else:
        print(dist[s][t] - 1)

Solution (Python):
import sys

def solve():
	input=sys.stdin.readline
	n, m, l = map(int, input().split())
	edge = [[] for _ in range(n)]
	for _ in range(m):
		a, b, c = map(int, input().split())
		if c <= l:
			edge[a-1].append((c, b-1))
			edge[b-1].append((c, a-1))
	inf = 10 ** 20
	dist = [[inf for _ in range(n)]for i in range(n)]
	for i in range(n):
		dist[i][i]=0
		for c, b in edge[i]:
			dist[i][b]=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 = [[n+50 for j in range(n)] for i in range(n)]
	for i in range(n):
		dist2[i][i] = 0
		for j in range(i+1, n):
			if dist[i][j] <= l:
				dist2[i][j] = 1
				dist2[j][i] = 1
	for k in range(n):
		for i in range(n):
			for j in range(n):
				dist2[i][j] = min(dist2[i][k] + dist2[k][j], dist2[i][j])
	q = int(input())		
	for i in range(q):
		s, t = map(int, input().split())
		print(dist2[s-1][t-1] - 1 if dist2[s-1][t-1] < n+50 else -1)
	return 0
	
if __name__ == '__main__':
	solve()

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

import sys
from collections import deque, defaultdict
import copy
import bisect
sys.setrecursionlimit(10 ** 9)
import math
import heapq
from itertools import product, permutations,combinations
import fractions
from operator import itemgetter

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

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

graph = [[] for _ in range(N)]

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

num_matrix = [[0]*N for i in range(N)]


for start in range(N):
	dist = [(1000000, 0)]*N
	que = []
	heapq.heappush(que, (0, 0, start))
	dist[start] = (0, 0)
	while len(que) > 0:

		node = heapq.heappop(que)
		
		if node[0] == dist[node[2]][0] and node[1] == dist[node[2]][1]:
			
			for edge in graph[node[2]]:
				
				
				if edge[1] + dist[node[2]][1] > L:
					next_dist = (dist[node[2]][0] + 1, edge[1], edge[0])
				else:
					next_dist = (dist[node[2]][0], dist[node[2]][1] + edge[1], edge[0])
				if dist[edge[0]][0] > next_dist[0]:
					dist[next_dist[2]] = (next_dist[0], next_dist[1])
					heapq.heappush(que, next_dist)
				elif dist[edge[0]][0] == next_dist[0] and dist[edge[0]][1] > next_dist[1]:
					heapq.heappush(que, next_dist)
					dist[next_dist[2]] = (next_dist[0], next_dist[1])
	for i in range(N):
		num_matrix[start][i] = dist[i][0]



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

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


def main():
    N, M, L = map(int, input().split())
    INF = 10**13
    dis = [[INF for _ in range(N)] for _ in range(N)]
    for _ in range(M):
        a, b, c = map(int, input().split())
        dis[a-1][b-1] = c
        dis[b-1][a-1] = c
    Q = int(input())
    Query = [list(map(int, input().split())) for _ in range(Q)]

    for k in range(N):
        for i in range(N):
            for j in range(N):
                dis[i][j] = min(dis[i][j], dis[i][k]+dis[k][j])
    
    movable = [[] for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if i != j and dis[i][j] <= L:
                movable[i].append(j)
    

    for s, t in Query:
        s, t = s-1, t-1
        q = movable[s]
        checked = [False]*N
        ok = False
        for p in q:
            if p == t:
                ok = True
                break
            checked[p] = True
        checked[s] = True
        
        if ok:
            print(0)
            continue
        c = 0
        while q:
            c += 1
            qq = []
            for p in q:
                for np in movable[p]:
                    if np == t:
                        ok = True
                        break
                    if not checked[np]:
                        qq.append(np)
                        checked[np] = True
            if ok: break
            q = qq
        if ok:
            print(c)
        else:
            print(-1)



            



if __name__ == "__main__":
    main()

Solution (Python):
INF = float('inf')
N, M, L = map(int, input().split())
dist = [[INF]*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())
  dist[a-1][b-1] = dist[b-1][a-1] = c


for k in range(N):
  for i in range(N-1):
    for j in range(i+1, N):
      t = dist[i][k] + dist[k][j]
      if dist[i][j] > t:
        dist[i][j] = dist[j][i] = t


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


for k in range(N):
  for i in range(N-1):
    for j in range(i+1, N):
      t = dist2[i][k] + dist2[k][j]
      if dist2[i][j] > t:
        dist2[i][j] = dist2[j][i] = t

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

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):
import sys
input = sys.stdin.readline

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

E=[[] for i in range(N+1)]

for i in range(M):
    a,b,cost=map(int,input().split())

    E[a].append((b,cost))
    E[b].append((a,cost))


Q=int(input())
Query=[list(map(int,input().split())) for i in range(Q)]

ANS=[[(N+10,0) for i in range(N+1)] for j in range(N+1)]

from collections import deque
import heapq

for start in range(1,N+1):

    ANS[start][start]=(0,0)

    checking=[((0,0),start)]

    while checking:
        
        c,checktown=heapq.heappop(checking)
        h,cost0=c

        if ANS[start][checktown]<c:
            continue

        for to,cost in E[checktown]:
            
            if cost0+cost<=L and (h,cost0+cost)<ANS[start][to]:
                ANS[start][to]=(h,cost0+cost)
                heapq.heappush(checking,((h,cost0+cost),to))

            elif cost<=L and (h+1,cost)<ANS[start][to]:
                ANS[start][to]=(h+1,cost)
                heapq.heappush(checking,((h+1,cost),to))

for x,y in Query:
    if ANS[x][y][0]==N+10:
        print(-1)
    else:
        print(ANS[x][y][0])

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

N, M, L = [int(_) for _ in input().split()]
ABC = [[int(_) for _ in input().split()] for _ in range(M)]
Q = int(input())
ST = [[int(_) for _ in input().split()] for _ in range(Q)]
edge = np.full((N, N), 10**10, dtype=int)
for a, b, c in ABC:
    if c <= L:
        edge[a - 1][b - 1] = edge[b - 1][a - 1] = c
G = csgraph_from_dense(edge, null_value=10**10)
D = floyd_warshall(G)
D = D <= L
E = floyd_warshall(D)
INF = float('inf')
for s, t in ST:
    d = E[s - 1][t - 1]
    if d == INF:
        d = 0
    print(int(d) - 1)

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


from heapq import heappush, heappop


def dijkstra(graph: list, start, INF=float('inf')) -> list:
    n = len(graph)
    dist = [INF] * n
    visited = [False] * n

    dist[start] = 0
    que = [(0, start)]

    while que:
        cost, cur = heappop(que)

        if visited[cur]:
            continue

        visited[cur] = True

        for nextnode, edgecost in graph[cur]:
            nextcost = cost + edgecost
            if nextcost < dist[nextnode]:
                dist[nextnode] = nextcost
                heappush(que, (nextcost, nextnode))

    return dist

n, m, l = li()
graph = [[] for _ in range(n)]
costgraph = [[] for _ in range(n)]
for _ in range(m):
    a, b, c = li()
    a -= 1
    b -= 1
    graph[a].append((b, c))
    graph[b].append((a, c))

for st in range(n):
    dist = dijkstra(graph, st)
    for ds in range(n):
        if st == ds:
            continue

        if dist[ds] <= l:
            costgraph[st].append((ds, 1))

q = ni()
INF = float("inf")

dist = [[INF]*n for _ in range(n)]
for i in range(n):
    dist[i] = dijkstra(costgraph, i, INF)

for _ in range(q):
    st, ds = li_()
    if dist[st][ds] == INF:
        print(-1)
    else:
        print(dist[st][ds] - 1)

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

N,M,L = map(int,input().split())
ABC = [list(map(int,input().split())) for _ in [0]*M]
Q = int(input())
ST = [list(map(int,input().split())) for _ in [0]*Q]


INF = (L+1)*N
d = [[INF]*N for _ in [0]*N]
for i in range(N):
    d[i][i] = 0
for a,b,c in ABC:
    a-=1;b-=1
    d[a][b] = c
    d[b][a] = c

def Warshall_Floyd(d,N=None):
    if N==None : 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])

Warshall_Floyd(d,N)

d2 = [None]*N
for i,row in enumerate(d):
    d2[i] = [1 if e<=L else INF for e in row]
    d2[i][i] = 0

Warshall_Floyd(d2,N)

for s,t in ST:
    s-=1;t-=1
    ans = (d2[s][t]%INF)-1
    print(ans)

Solution (Python):
import sys

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

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

e_list = [[] for i in range(N)]

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 = [[float("inf") for i in range(N)] for i in range(N)] 

for i in range(M):
    x,y,z = list(map(int,input().split()))
    if z<=L:
        d[x-1][y-1] = z
        d[y-1][x-1] = z

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

d = warshall_floyd(d)

e_list = [[] for i in range(N)]

d2 = [[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:
            d2[i][j] = 1
            d2[j][i] = 1

d2 = warshall_floyd(d2)

Q = int(input())
for i in range(Q):
    s,t = list(map(int,input().split()))
    s,t = s-1,t-1
    if d2[s][t]>10**9:
        print(-1)
    else:
        print(d2[s][t]-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):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

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

for _ in range(M):
    a,b,c = inpl()
    a,b = a-1,b-1
    cost[a][b] = c
    cost[b][a] = c

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



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


Q = inp()
ans = []
for _ in range(Q):
    s,t = inpl()
    tmp = cost[s-1][t-1]
    if tmp == INF:
        ans.append(-1)
    else:
        ans.append(tmp-1)

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