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


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


def read_is(n=1):
    return [read_i() for _ in range(n)]


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=None, undirected=False):
        if d is None:
            d = 1
        self.edges[s][t] = d
        if undirected:
            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.nexts = [[None] * 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
                self.nexts[source][destination] = destination

    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
                        self.nexts[i][j] = self.nexts[i][k]


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, d=c, undirected=True)
fw = FloydWarshall(graph)
fw.compute_shortest_path_lengths()

graph2 = Graph(n)
for s in range(n):
    for t in range(n):
        if s >= t:
            continue
        if fw.distances[s][t] > l:
            continue
        graph2.add_edge(s, t, d=1, undirected=True)
fw = FloydWarshall(graph2)
fw.compute_shortest_path_lengths()

q = int(input())
for _ in range(q):
    s, t = read_i()
    s -= 1
    t -= 1
    d = fw.distances[s][t] - 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()))


def read_is(n=1):
    return [read_i() for _ in range(n)]


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

    def add_edge(self, s, t, d=None, undirected=False):
        if d is not None:
            self.edges[s].append((t, d))
        else:
            self.edges[s].append(t)
        if undirected:
            self.add_edge(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.nexts = [[None] * 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 self.graph.edges[source]:
                self.distances[source][destination] = distance
                self.nexts[source][destination] = destination

    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
                        self.nexts[i][j] = self.nexts[i][k]


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, d=c, undirected=True)
fw = FloydWarshall(graph)
fw.compute_shortest_path_lengths()

graph2 = Graph(n)
for s in range(n):
    for t in range(n):
        if s >= t:
            continue
        if fw.distances[s][t] > l:
            continue
        graph2.add_edge(s, t, d=1, undirected=True)
fw = FloydWarshall(graph2)
fw.compute_shortest_path_lengths()

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

Solution (Python):
N,M,L=map(int,input().split())
G=[[10**15 for i in range(N)] for i 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])
H=[[10**15 for i in range(N)] for i in range(N)]
for i in range(N):
    for j in range(N):
        if G[i][j]<=L:
            H[i][j]=1
for k in range(N):
    for i in range(N):
        for j in range(N):
            H[i][j]=min(H[i][j],H[i][k]+H[k][j])
Q=int(input())
for line in range(Q):
    s,t=map(int,input().split())
    s-=1;t-=1
    if H[s][t]==10**15:
        print(-1)
    else:
        print(H[s][t]-1)

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

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


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

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

    q = int(input())

    def f(x):
        return int(x) - 1

    st = (tuple(map(f, input().split())) for _ in range(q))

    g = csr_matrix(g)

    d = floyd_warshall(csgraph=g)

    refuel = [[float("inf")] * n for _ in range(n)]

    for i in range(n):
        for j in range(n):
            if d[i][j] <= l:
                refuel[i][j] = 1

    refuel = csr_matrix(refuel)

    ans = floyd_warshall(refuel)

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


if __name__ == "__main__":
    main()

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 = float('inf')
dist1 = [[INF]*N for i in range(N)]
dist2 = [[INF]*N for i in range(N)]
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    dist1[a][b] = dist1[b][a] = c
for i in range(N):
    dist1[i][i] = 0
    dist2[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist1[i][k] + dist1[k][j]
            if dist1[i][j] <= d: continue
            dist1[i][j] = d
for i in range(N):
    for j in range(N):
        if i==j: continue
        if dist1[i][j] <= L:
            dist2[i][j] = 1
for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist2[i][k] + dist2[k][j]
            if dist2[i][j] <= d: continue
            dist2[i][j] = d
ans = []
for s,t in ST:
    d = dist2[s-1][t-1]
    if d == INF:
        ans.append(-1)
    else:
        ans.append(d-1)
print(*ans, sep='\n')

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

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    inf = 1001001001
    dist = [[inf] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        a, b = a - 1, b - 1
        dist[a][b] = dist[b][a] = c
    for w in range(n):
        for u in range(n):
            for v in range(n):
                dist[u][v] = min(dist[u][v], dist[u][w] + dist[w][v])
    dist2 = [[inf] * n for _ in range(n)]
    for u in range(n):
        for v in range(n):
            if dist[u][v] <= l: dist2[u][v] = 1
    for w in range(n):
        for u in range(n):
            for v in range(n):
                dist2[u][v] = min(dist2[u][v], dist2[u][w] + dist2[w][v])

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

main()

Solution (Python):
import sys
input = sys.stdin.buffer.readline
from collections import deque
N, M, L = map(int, input().split())
D = [[1<<40] * 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())
    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):
            D[i][j] = min(D[i][j], D[i][k] + D[k][j])
Y = [[] for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j: continue
        if D[i][j] <= L:
            Y[i].append(j)
def BFS(n, E, i0=0):
    Q = deque([i0])
    D = [-1] * n
    D[i0] = 0
    while Q:
        x = Q.popleft()
        for c in E[x]:
            if D[c] == -1:
                D[c] = D[x] + 1
                Q.append(c)
    return D
D2 = []
for i in range(N):
    D2.append(BFS(N, Y, i))

ans = []
for _ in range(int(input())):
    s, t = map(int, input().split())
    a = D2[s-1][t-1]-1
    ans.append(str(a if a >= 0 else -1))
print("\n".join(ans))

Solution (Python):
import sys


def main():
    n, m, l = [int(s) for s in sys.stdin.readline().strip().split()]
    a = [0] * m
    b = [0] * m
    c = [0] * m
    for i in range(m):
        a[i], b[i], c[i] = [int(s) for s in sys.stdin.readline().strip().split()]

    INF = float('inf')
    dist = [[INF] * n for _ in range(n)]
    for i in range(m):
        dist[a[i]-1][b[i]-1] = c[i]
        dist[b[i]-1][a[i]-1] = c[i]

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

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

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

    q = int(sys.stdin.readline().strip())
    s = [0] * q
    t = [0] * q
    for i in range(q):
        s[i], t[i] = [int(s) for s in sys.stdin.readline().strip().split()]

    for i in range(q):
        s0, t0 = s[i] - 1, t[i] - 1
        if ldist[s0][t0] == INF:
            print(-1)
        else:
            print(ldist[s0][t0]-1)


main()

Solution (Python):
n,m,l=map(int,raw_input().split())
INF=(10**9)+1

g=[]
f=[]
for i in xrange(n):
    g.append([INF]*(n))
    f.append([INF]*(n))

for i in xrange(len(g)):
    for j in xrange(len(g)):
        if i==j:
            g[i][j]=0
            f[i][j]=0

for i in xrange(m):
    a,b,c=map(int,raw_input().split())
    g[a-1][b-1]=c
    g[b-1][a-1]=c
    f[a-1][b-1]=c
    f[b-1][a-1]=c

for k in xrange(n):
    for i in xrange(n):
        for j in xrange(n):
            g[i][j]=min(g[i][j],g[i][k]+g[k][j])
            f[i][j]=min(f[i][j],f[i][k]+f[k][j])

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

for k in xrange(n):
    for i in xrange(n):
        for j in xrange(n):
            g[i][j]=min(g[i][j],g[i][k]+g[k][j])

q=int(raw_input())
for i in xrange(q):
    s,t=map(int,raw_input().split())
    u=g[s-1][t-1]

    if u>=INF:
        print (-1)
    else:
        print (u-1)

Solution (Python):
def use_fast_io():
    global input
    import io
    import sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline




from scipy.sparse.csgraph import floyd_warshall
import numpy as np


def solve():
    n, m, l = map(int, input().split())
    graph = np.full((n, n), 10 ** 10)
    for _ in range(m):
        s, t, w = map(int, input().split())
        if w <= l:
            graph[s - 1][t - 1] = graph[t - 1][s - 1] = w
    graph = floyd_warshall(graph, directed=False)
    graph = floyd_warshall(graph <= l, directed=False)
    graph[np.isinf(graph)] = 0
    Q = int(input())
    ans = []
    for _ in range(Q):
        s, t = map(int, input().split())
        ans.append(int(graph[s - 1][t - 1]) - 1)
    print(*ans, sep='\n')


if __name__ == '__main__':
    solve()

Solution (Python):
def use_fast_io():
    global input
    import io
    import sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline


use_fast_io()

from scipy.sparse.csgraph import floyd_warshall
import numpy as np


def solve():
    n, m, l = map(int, input().split())
    graph = np.full((n, n), 10 ** 10)
    for _ in range(m):
        s, t, w = map(int, input().split())
        if w <= l:
            graph[s - 1][t - 1] = graph[t - 1][s - 1] = w
    graph = floyd_warshall(graph, directed=False)
    graph = floyd_warshall(graph <= l, directed=False)
    graph[np.isinf(graph)] = 0
    Q = int(input())
    ans = []
    for _ in range(Q):
        s, t = map(int, input().split())
        ans.append(int(graph[s - 1][t - 1]) - 1)
    print(*ans, sep='\n')


if __name__ == '__main__':
    solve()

Solution (Python):
from scipy.sparse.csgraph import floyd_warshall
import numpy as np
 
N, M, L = map(int, input().split())
graph = np.full((N, N), 10**10)
for _ in range(M):
    s, t, w = map(int, input().split())
    if w <= L:
        graph[s-1][t-1] = graph[t-1][s-1] = w
graph = floyd_warshall(graph, directed=False)
graph = floyd_warshall(graph <= L, directed=False)
graph[np.isinf(graph)] = 0
 
Q = int(input())
ans = []
for _ in range(Q):
    s, t = map(int, input().split())
    ans.append(int(graph[s-1][t-1]) - 1)
print(*ans, sep='\n')

Solution (Python):
def use_fast_io():
    global input
    import io
    import sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline
use_fast_io()
 
 
from scipy.sparse.csgraph import floyd_warshall
import numpy as np
 
N, M, L = map(int, input().split())
graph = np.full((N, N), 10**10)
for _ in range(M):
    s, t, w = map(int, input().split())
    if w <= L:
        graph[s-1][t-1] = graph[t-1][s-1] = w
graph = floyd_warshall(graph, directed=False)
graph = floyd_warshall(graph <= L, directed=False)
graph[np.isinf(graph)] = 0
 
Q = int(input())
ans = []
for _ in range(Q):
    s, t = map(int, input().split())
    ans.append(int(graph[s-1][t-1]) - 1)
print(*ans, sep='\n')

Solution (Python):

from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L = map(int, input().split())
graph = np.full((N, N), 10 ** 10)
for _ in range(M):
    s, t, w = map(int, input().split())
    if w <= L:
        graph[s - 1][t - 1] = graph[t - 1][s - 1] = w
graph = floyd_warshall(graph, directed=False)
graph = floyd_warshall(graph <= L, directed=False)
graph[np.isinf(graph)] = 0

Q = int(input())
ans = []
for _ in range(Q):
    s, t = map(int, input().split())
    ans.append(int(graph[s - 1][t - 1]) - 1)
print(*ans, sep='\n')

Solution (Python):
from scipy.sparse import*
i=lambda:tuple(map(int,input().split()))
f=csgraph.dijkstra
n,m,l=i()
d=eval('[0]*-~n,'*-~n)
while m:a,b,c=i();d[a][b]=c;m-=1
d=f(f(d,0)<=l)
d[d>n]=0
for _ in'_'*i()[0]:print(d[i()]-1)

Solution (Python):
from scipy.sparse import*
i=lambda:tuple(map(int,input().split()))
f=csgraph.dijkstra
n,m,l=i()
d=eval('[0]*-~n,'*-~n)
while m:a,b,c=i();d[a][b]=c;m-=1
d=f(f(d,0)<=l)-1
d[d>n]=-1
for _ in'_'*i()[0]:print(d[i()])

Solution (Python):
from scipy.sparse import*
i=lambda:tuple(map(int,input().split()))
f=csgraph.dijkstra
n,m,l=i()
d=eval('[0]*-~n,'*-~n)
while m:a,b,c=i();d[a][b]=c;m-=1
d=f(f(d,0)<=l)
d[d>n]=0
for _ in'_'*i()[0]:print(int(d[i()])-1)

Solution (Python):
import sys
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L = map(int, sys.stdin.buffer.readline().split())
rest = np.array(sys.stdin.buffer.read().split(), np.int)
in_, out, weight = rest[0:3*M:3], rest[1:3*M:3], rest[2:3*M:3]

graph = csr_matrix((weight, (in_, out)), shape=(N+1, N+1))
graph = floyd_warshall(graph, directed=False)
graph = floyd_warshall(graph <= L, directed=False)
graph[np.isinf(graph)] = 0

S, T = rest[3*M+1::2], rest[3*M+2::2]
print('\n'.join(map(str, graph[S, T].astype(int) - 1)))

Solution (Python):
import sys
from collections import defaultdict
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix
import numpy as np

def solve(N: int, M: int, L: int, A: "List[int]", B: "List[int]", C: "List[int]", Q: int, s: "List[int]", t: "List[int]"):
    distance = [[0 for i in range(N)] for _ in range(N)] 
    for abc in zip(A,B,C):
        distance[abc[0]-1][abc[1]-1] = abc[2]
        distance[abc[1]-1][abc[0]-1] = abc[2]
    graph = csr_matrix(distance)
    dist_matrix = floyd_warshall(graph,directed=False) 
    
    fuel_matrix = [[0 for i in range(N)] for _ in range(N)] 
    for i in range(N):
      for j in range(N):
        if 0 < dist_matrix[i][j] <= L:
          fuel_matrix[i][j] = 1
    graph2 = csr_matrix(fuel_matrix)
    dist_matrix2 = floyd_warshall(graph2,directed=False) 

    for i in range(Q):
      if dist_matrix2[s[i]-1][t[i]-1] == float("inf"):
        print(-1)
      else:
        print(int(dist_matrix2[s[i]-1][t[i]-1])-1)
                
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    L = int(next(tokens))  
    A = [int()] * (M)  
    B = [int()] * (M)  
    C = [int()] * (M)  
    for i in range(M):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
        C[i] = int(next(tokens))
    Q = int(next(tokens))  
    s = [int()] * (Q)  
    t = [int()] * (Q)  
    for i in range(Q):
        s[i] = int(next(tokens))
        t[i] = int(next(tokens))
    solve(N, M, L, A, B, C, Q, s, t)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L = map(int, sys.stdin.buffer.readline().split())
rest = np.array(sys.stdin.buffer.read().split(), np.int)
in_, out, weight = rest[0:3*M:3], rest[1:3*M:3], rest[2:3*M:3]

graph = csr_matrix((weight, (in_, out)), shape=(N+1, N+1))
graph = floyd_warshall(graph, directed=False)
graph = floyd_warshall(graph <= L, directed=False)
graph[np.isinf(graph)] = 0

S, T = rest[3*M+1::2], rest[3*M+2::2]
print(*(graph[S, T].astype(int) - 1), sep='\n')

Solution (Python):
import sys
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L, *rest = map(int, sys.stdin.buffer.read().split())
in_, out, weight = rest[0:3*M:3], rest[1:3*M:3], rest[2:3*M:3]

graph = csr_matrix((weight, (in_, out)), shape=(N+1, N+1))
graph = floyd_warshall(graph, directed=False)
graph = floyd_warshall(graph <= L, directed=False)
graph[np.isinf(graph)] = 0

S, T = rest[3*M+1::2], rest[3*M+2::2]
print(*(graph[S, T] - 1).astype(int), sep='\n')

Solution (Python):
def use_fast_io():
    global input
    import io
    import sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline
use_fast_io()


from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L = map(int, input().split())
graph = [[10**10] * N for _ in range(N)]
for _ in range(M):
    s, t, w = map(int, input().split())
    if w <= L:
        graph[s-1][t-1] = graph[t-1][s-1] = w
graph = floyd_warshall(graph, directed=False)
graph = floyd_warshall(graph <= L, directed=False)
graph[np.isinf(graph)] = 0

Q = int(input())
ans = []
for _ in range(Q):
    s, t = map(int, input().split())
    ans.append(int(graph[s-1][t-1]) - 1)
print(*ans, sep='\n')

Solution (Python):
def use_fast_io():
    global input
    import io
    import sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline
use_fast_io()


from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L = map(int, input().split())
graph = np.full((N, N), 10**10)
for _ in range(M):
    s, t, w = map(int, input().split())
    if w <= L:
        graph[s-1][t-1] = graph[t-1][s-1] = w
graph = floyd_warshall(graph, directed=False)
graph = floyd_warshall(graph <= L, directed=False)
graph[np.isinf(graph)] = 0

Q = int(input())
ans = []
for _ in range(Q):
    s, t = map(int, input().split())
    ans.append(int(graph[s-1][t-1]) - 1)
print(*ans, sep='\n')

Solution (Python):
from scipy.sparse import*
i=lambda:tuple(map(int,input().split()))
f=csgraph.floyd_warshall
n,m,l=i()
d=eval('[0]*-~n,'*-~n)
exec('a,b,c=i();d[a][b]=c;'*m)
d=f(f(d,0)<=l)
d[d>n]=0
for _ in'_'*i()[0]:print(int(d[i()])-1)

Solution (Python):
def use_fast_io():
    global input
    import io
    import sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline
use_fast_io()


from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L = map(int, input().split())
graph = np.full((N, N), np.inf)
for _ in range(M):
    s, t, w = map(int, input().split())
    if w <= L:
        graph[s-1][t-1] = graph[t-1][s-1] = w
graph = floyd_warshall(graph, directed=False)
graph = floyd_warshall(graph <= L, directed=False)
graph[np.isinf(graph)] = 0

Q = int(input())
ans = []
for _ in range(Q):
    s, t = map(int, input().split())
    ans.append(int(graph[s-1][t-1]) - 1)
print(*ans, sep='\n')

Solution (Python):
def use_fast_io():
    global input
    import io
    import sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline
use_fast_io()


from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L = map(int, input().split())
graph = [[float("inf")] * N for _ in range(N)]
for _ in range(M):
    s, t, w = map(int, input().split())
    if w <= L:
        graph[s-1][t-1] = graph[t-1][s-1] = w
graph = floyd_warshall(graph, directed=False)
graph = floyd_warshall(graph <= L, directed=False)
graph[np.isinf(graph)] = 0

Q = int(input())
ans = []
for _ in range(Q):
    s, t = map(int, input().split())
    ans.append(int(graph[s-1][t-1]) - 1)
print(*ans, sep='\n')

Solution (Python):
def use_fast_io():
    global input
    import io
    import sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline
use_fast_io()


from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L = map(int, input().split())
graph = [[0] * N for _ in range(N)]
for _ in range(M):
    s, t, w = map(int, input().split())
    if w <= L:
        graph[s-1][t-1] = graph[t-1][s-1] = w
graph = floyd_warshall(graph, directed=False)
graph = floyd_warshall(graph <= L, directed=False)
graph[np.isinf(graph)] = 0

Q = int(input())
ans = []
for _ in range(Q):
    s, t = map(int, input().split())
    ans.append(int(graph[s-1][t-1]) - 1)
print(*ans, sep='\n')

Solution (Python):
def use_fast_io():
    global input
    import io
    import sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline
use_fast_io()


from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L = map(int, input().split())
graph = np.zeros((N, N), dtype=int)
for _ in range(M):
    s, t, w = map(int, input().split())
    if w <= L:
        graph[s-1, t-1] = graph[t-1, s-1] = w
graph = floyd_warshall(graph, directed=False)
graph = floyd_warshall(graph <= L, directed=False)
graph[np.isinf(graph)] = 0

Q = int(input())
ans = []
for _ in range(Q):
    s, t = map(int, input().split())
    ans.append(int(graph[s-1][t-1]) - 1)
print(*ans, sep='\n')

Solution (Python):
from scipy.sparse import*
i=lambda:tuple(map(int,input().split()))
f=csgraph.floyd_warshall
n,m,l=i()
d=eval('[0]*-~n,'*-~n)
exec('a,b,c=i();d[a][b]=c;'*m)
d=f(f(d,0)<=l)
d[d>9e9]=0
for _ in'_'*i()[0]:print(int(d[i()])-1)

Solution (Python):
from scipy.sparse import*
i=lambda:map(int,input().split())
f=csgraph.floyd_warshall
n,m,l=i()
d=eval('[0]*-~n,'*-~n)
exec('a,b,c=i();d[a][b]=c;'*m)
d=f(f(d,0)<=l)
d[d>9e9]=0
q,=i()
while q:s,t=i();print(int(d[s,t])-1);q-=1

Solution (Python):
def io_tmp():
    global input
    import io, sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline

io_tmp()


from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L = map(int, input().split())
graph = np.zeros((N, N))
for _ in range(M):
    s, t, w = map(int, input().split())
    if w <= L:
        graph[s-1, t-1] = graph[t-1, s-1] = w
floyd_warshall(graph, directed=False, overwrite=True)
dist = floyd_warshall(graph <= L, directed=False)
dist[np.isinf(dist)] = 0.0

Q = int(input())
ans = []
for _ in range(Q):
    s, t = map(int, input().split())
    ans.append(int(dist[s-1][t-1]) - 1)
print(*ans, sep='\n')

Solution (Python):
from scipy.sparse import*
i=lambda:map(int,input().split())
f=csgraph.floyd_warshall
I=9e9
n,m,l=i()
d=eval('[I]*-~n,'*-~n)
exec('a,b,c=i();d[a][b]=c;'*m)
d=f(f(d,0)<=l)
d[d>I]=I
q,=i()
while q:s,t=i();print(int(d[s,t]%I)-1);q-=1

Solution (Python):
def io_tmp():
    global input
    import io, sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline

io_tmp()


from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L = map(int, input().split())
graph = np.zeros((N, N))
for _ in range(M):
    s, t, w = map(int, input().split())
    if w <= L:
        graph[s-1, t-1] = graph[t-1, s-1] = w
dist = floyd_warshall(graph, directed=False)
dist = floyd_warshall(dist <= L, directed=False)
dist[np.isinf(dist)] = 0.0

Q = int(input())
ans = []
for _ in range(Q):
    s, t = map(int, input().split())
    ans.append(int(dist[s-1][t-1]) - 1)
print(*ans, sep='\n')

Solution (Python):
def use_fast_io():
    global input
    import io, sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline

use_fast_io()


from scipy.sparse.csgraph import floyd_warshall

N, M, L = map(int, input().split())
graph = [[float("inf")] * N for _ in range(N)]
for _ in range(M):
    s, t, w = map(int, input().split())
    if w <= L:
        graph[s-1][t-1] = graph[t-1][s-1] = w
dist = floyd_warshall(graph, directed=False)
dist = floyd_warshall(dist <= L, directed=False)
dist[dist == float("inf")] = 0.0

Q = int(input())
ans = []
for _ in range(Q):
    s, t = map(int, input().split())
    ans.append(int(dist[s-1][t-1]) - 1)
print(*ans, sep='\n')

Solution (Python):
def io_tmp():
    global input
    import io, sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline

io_tmp()


from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L = map(int, input().split())
graph = np.zeros((N, N), dtype=np.int64)
for _ in range(M):
    s, t, w = map(int, input().split())
    if w <= L:
        graph[s-1, t-1] = graph[t-1, s-1] = w
dist = floyd_warshall(graph, directed=False)
dist = floyd_warshall(dist <= L, directed=False)
dist[np.isinf(dist)] = 0.0

Q = int(input())
ans = []
for _ in range(Q):
    s, t = map(int, input().split())
    ans.append(int(dist[s-1][t-1]) - 1)
print(*ans, sep='\n')

Solution (Python):
from scipy.sparse import*
i=lambda:map(int,input().split())
f=csgraph.floyd_warshall
I=9e9
n,m,l=i()
d=eval('[I]*-~n,'*-~n)
exec('a,b,c=i();d[a][b]=c;'*m)
d=f(d,0)
d[d>l]=I
d[d<I]=1
d=f(d)
d[d>I]=I
q,=i()
while q:s,t=i();print(int(d[s,t]%I)-1);q-=1

Solution (Python):
def io_tmp():
    global input
    import io
    f = io.StringIO(open(0).read())
    input = f.readline

io_tmp()


from scipy.sparse.csgraph import floyd_warshall
import numpy as np

N, M, L = map(int, input().split())
graph = np.zeros((N, N), dtype=np.int64)
for _ in range(M):
    s, t, w = map(int, input().split())
    if w <= L:
        graph[s-1, t-1] = graph[t-1, s-1] = w
dist = floyd_warshall(graph, directed=False)
dist = floyd_warshall(dist <= L, directed=False)
dist[np.isinf(dist)] = 0.0

Q = int(input())
ans = []
for _ in range(Q):
    s, t = map(int, input().split())
    ans.append(int(dist[s-1][t-1]) - 1)
print(*ans, sep='\n')