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



Sample input:
3 2 5
1 2 3
2 3 3
2
3 2
1 3


Sample output:
0
1

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.buffer.readline

n, m, limit = map(int, input().split())
infi = 10 ** 15
graph = [[infi] * (n + 1) for _ in range(n + 1)]
step = [[infi] * (n + 1) for _ in range(n + 1)]
for _ in range(m):
    a, b, c = map(int, input().split())
    graph[a][b] = c
    graph[b][a] = c
for k in range(1, n + 1):
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            graph[i][j] = min(graph[i][k] + graph[k][j], graph[i][j])
for i in range(1, n + 1):
    for j in range(1, n + 1):
        if graph[i][j] <= limit:
            graph[i][j] = 1
        else:
            graph[i][j] = infi
for k in range(1, n + 1):
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            graph[i][j] = min(graph[i][k] + graph[k][j], graph[i][j])


q = int(input())
for _ in range(q):
    a, b = map(int, input().split())
    print(graph[a][b] - 1 if graph[i][j] != infi else -1)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.buffer.readline
n, m, L = map(int, input().split())
infi = 10 ** 20
graph = [[infi] * (n + 1) for _ in range(n + 1)]
step = [[infi] * (n + 1) for _ in range(n + 1)]
for _ in range(m):
    a, b, c = map(int, input().split())
    graph[a][b] = c
    graph[b][a] = c
for k in range(1, n + 1):
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            graph[i][j] = min(graph[i][k] + graph[k][j], graph[i][j])
for i in range(1, n + 1):
    for j in range(1, n + 1):
        if graph[i][j] <= L:
            step[i][j] = 1
for k in range(1, n + 1):
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            step[i][j] = min(step[i][k] + step[k][j], step[i][j])


q = int(input())
for _ in range(q):
    a, b = map(int, input().split())
    print(step[a][b] - 1 if step[i][j] != infi else -1)

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys
"""
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import time

import math
"""
"""def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
sys.setrecursionlimit(10**7)"""
input=lambda :sys.stdin.readline().rstrip()



N,M,L=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(M)]
ST=[list(map(int,input().split())) for i in range(int(input()))]


d=[[[301,L,0]]*(N+1) for i in range(N+1)]
for i in range(M):
    ABC[i][0]-=1
    ABC[i][1]-=1
for i in range(len(ST)):
    ST[i][0]-=1
    ST[i][1]-=1

for A,B,C in ABC:
    if C<=L:
        d[A][B]=[0,C,-(L-C)]
        d[B][A]=[0,C,-(L-C)]
for i in range(N):
    d[i][i]=[0,0,L]
def main(X,Y,Z):
    m2,l2,h2=Y
    m3,l3,h3=Z
    if not l3:
        return(X)

    h2=-h2
    if h2-l3<0:
        lst=[m2+m3+1,0,-(L-l3)]
    else:
        lst=[m2+m3,0,-(h2-l3)]
    return(min(X,lst))

for _ in range(N):
    for k in range(N):
        for i in range(N):
            for m in range(N):
                d[i][m]=main(d[i][m],d[i][k],d[k][m])

for s,t in ST:
    if d[s][t][0]!=301:
        print(d[s][t][0])
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys
"""
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import time

import math
"""
"""def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
sys.setrecursionlimit(10**7)"""
input=lambda :sys.stdin.readline().rstrip()



N,M,L=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(M)]
ST=[list(map(int,input().split())) for i in range(int(input()))]

data=[[] for i in range(N+1)]
for A,B,C in ABC:
    data[A].append([B,C])
    data[B].append([A,C])



d=[[[301,0]]*(N+1) for i in range(N+1)]
for i in range(1,N+1):
    visited=set()
    stack=[[0,0,i]]
    while stack:
        m,l,a=heappop(stack)
        if not a in visited:
            visited.add(a)
            if len(visited)==N:
                break
            for p,c in data[a]:
                if not p in visited:
                    if c<=L:
                        if l+c>L:
                            if d[i][p]>[m+1,c]:
                                d[i][p]=[m+1,c]
                                if p>i:
                                    d[p][i]=[m+1,L]
                                heappush(stack,[m+1,c,p])
                        else:
                            if d[i][p]>=[m,l+c]:
                                d[i][p]=[m,l+c]
                                if p>i:
                                    d[p][i]=[m,l+c]
                                heappush(stack,[m,l+c,p])
for s,t in ST:
    if d[s][t][0]!=301:
        print(d[s][t][0])
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys
"""
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import time

import math
"""
"""def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
sys.setrecursionlimit(10**7)"""
input=lambda :sys.stdin.readline().rstrip()



N,M,L=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(M)]
ST=[list(map(int,input().split())) for i in range(int(input()))]

data=[{} for i in range(N+1)]
se=[set() for i in range(N+1)]

for A,B,C in ABC:
    data[A][B]=C
    data[B][A]=C
    se[A].add(B)
    se[B].add(A)

for i in range(N+1):
    data[i][0]=0
maxp=[max([l[a] for a in l]) for l in data]

d=[[[301,0]]*(N+1) for i in range(N+1)]
for i in range(1,N+1):
    visited=set()
    stack=[[0,0,i]]
    while stack:
        m,l,a=heappop(stack)
        if not a in visited:
            visited.add(a)
            if len(visited)==N:
                break
            for p in se[a]-visited:
                    c=data[a][p]
                    if c<=L:
                        if l+c>L:
                            if d[i][p]>[m+1,c]:
                                d[i][p]=[m+1,c]
                                if p>i:
                                    d[p][i]=[m+1,L]
                                heappush(stack,[m+1,c,p])
                        else:
                            if d[i][p]>=[m,l+c]:
                                d[i][p]=[m,l+c]
                                if p>i:
                                    d[p][i]=[m,l+max(maxp[p],c)]
                                heappush(stack,[m,l+c,p])
for s,t in ST:
    if d[s][t][0]!=301:
        print(d[s][t][0])
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys
"""
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import time

import math
"""
"""def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
sys.setrecursionlimit(10**7)"""
input=lambda :sys.stdin.readline().rstrip()



N,M,L=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(M)]
ST=[list(map(int,input().split())) for i in range(int(input()))]

data=[{} for i in range(N+1)]
se=[set() for i in range(N+1)]
for A,B,C in ABC:
    data[A][B]=C
    data[B][A]=C
    se[A].add(B)
    se[B].add(A)

d=[[[301,0]]*(N+1) for i in range(N+1)]
for i in range(1,N+1):
    visited=set()
    stack=[[0,0,i]]
    while stack:
        m,l,a=heappop(stack)
        if not a in visited:
            visited.add(a)
            if len(visited)==N:
                break
            for p in se[a]-visited:
                    c=data[a][p]
                    if c<=L:
                        if l+c>L:
                            if d[i][p]>[m+1,c]:
                                d[i][p]=[m+1,c]
                                if p>i:
                                    d[p][i]=[m+1,L]
                                heappush(stack,[m+1,c,p])
                        else:
                            if d[i][p]>=[m,l+c]:
                                d[i][p]=[m,l+c]
                                if p>i:
                                    d[p][i]=[m,l+c]
                                heappush(stack,[m,l+c,p])
for s,t in ST:
    if d[s][t][0]!=301:
        print(d[s][t][0])
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys
"""
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import time

import math
"""
"""def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
sys.setrecursionlimit(10**7)"""
input=lambda :sys.stdin.readline().rstrip()



N,M,L=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(M)]
ST=[list(map(int,input().split())) for i in range(int(input()))]

data=[{} for i in range(N+1)]
se=[set() for i in range(N+1)]
for A,B,C in ABC:
    data[A][B]=C
    data[B][A]=C
    se[A].add(B)
    se[B].add(A)

d=[[[301,0]]*(N+1) for i in range(N+1)]
for i in range(1,N+1):
    visited=set()
    stack=[[0,0,i]]
    while stack:
        m,l,a=heappop(stack)
        if not a in visited:
            visited.add(a)
            if len(visited)==N:
                break
            for p in se[a]-visited:
                    c=data[a][p]
                    if c<=L:
                        if l+c>L:
                            if d[i][p]>[m+1,c]:
                                d[i][p]=[m+1,c]
                                if p>i:
                                    d[p][i]=[m+1,L]
                                heappush(stack,[m+1,c,p])
                        else:
                            if d[i][p]>[m,l+c]:
                                d[i][p]=[m,l+c]
                                if p>i:
                                    d[p][i]=[m,l+c]
                                heappush(stack,[m,l+c,p])
for s,t in ST:
    if d[s][t][0]!=301:
        print(d[s][t][0])
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys
"""
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import time

import math
"""
"""def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
sys.setrecursionlimit(10**7)"""
input=lambda :sys.stdin.readline().rstrip()



N,M,L=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(M)]
ST=[list(map(int,input().split())) for i in range(int(input()))]

data=[{} for i in range(N+1)]
se=[set() for i in range(N+1)]
for A,B,C in ABC:
    data[A][B]=C
    data[B][A]=C
    se[A].add(B)
    se[B].add(A)

d=[[[301,0]]*(N+1) for i in range(N+1)]
for i in range(1,N+1):
    visited=set()
    stack=[[0,0,i]]
    while stack:
        m,l,a=heappop(stack)
        if not a in visited:
            visited.add(a)
            if len(visited)==N:
                break
            for p in se[a]-visited:
                    c=data[a][p]
                    if c<=L:
                        if l+c>L:
                            if d[i][p]>[m+1,c]:
                                d[i][p]=[m+1,c]
                                d[p][i]=[m+2,0]
                                heappush(stack,[m+1,c,p])
                        else:
                            if d[i][p]>[m,l+c]:
                                d[i][p]=[m,l+c]
                                d[p][i]=[m+1,0]
                                heappush(stack,[m,l+c,p])
for s,t in ST:
    if d[s][t][0]!=301:
        print(d[s][t][0])
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys
"""
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import time

import math
"""
"""def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
sys.setrecursionlimit(10**7)"""
input=lambda :sys.stdin.readline().rstrip()



N,M,L=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(M)]
ST=[list(map(int,input().split())) for i in range(int(input()))]

data=[{} for i in range(N+1)]
se=[set() for i in range(N+1)]
for A,B,C in ABC:
    data[A][B]=C
    data[B][A]=C
    se[A].add(B)
    se[B].add(A)

d=[[[301,0]]*(N+1) for i in range(N+1)]
for i in range(1,N+1):
    visited=set()
    stack=[[0,0,i]]
    while stack:
        m,l,a=heappop(stack)
        if not a in visited:
            visited.add(a)
            if len(visited)==N:
                break
            for p in se[a]-visited:
                    c=data[a][p]
                    if c<=L:
                        if l+c>L:
                            if d[i][p]>=[m+1,c]:
                                d[i][p]=[m+1,c]
                                d[p][i]=[m+1,L]
                                heappush(stack,[m+1,c,p])
                        else:
                            if d[i][p]>=[m,l+c]:
                                d[i][p]=[m,l+c]
                                #d[p][i]=[m,l+c]
                                heappush(stack,[m,l+c,p])
for s,t in ST:
    if d[s][t][0]!=301:
        print(d[s][t][0])
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys
"""
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import time

import math
"""
"""def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
sys.setrecursionlimit(10**7)"""
input=lambda :sys.stdin.readline().rstrip()



N,M,L=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(M)]
ST=[list(map(int,input().split())) for i in range(int(input()))]

data=[{} for i in range(N+1)]
se=[set() for i in range(N+1)]
for A,B,C in ABC:
    data[A][B]=C
    data[B][A]=C
    se[A].add(B)
    se[B].add(A)

d=[[[301,0]]*(N+1) for i in range(N+1)]
for i in range(1,N+1):
    visited=set()
    stack=[[0,0,i]]
    while stack:
        m,l,a=heappop(stack)
        if not a in visited:
            visited.add(a)
            if len(visited)==N:
                break
            for p in se[a]-visited:
                    c=data[a][p]
                    if c<=L:
                        if l+c>L:
                            if d[i][p]>=[m+1,c]:
                                d[i][p]=[m+1,c]
                                d[p][i]=[m+1,L]
                                heappush(stack,[m+1,c,p])
                        else:
                            if d[i][p]>=[m,l+c]:
                                d[i][p]=[m,l+c]
                                d[p][i]=[m,l+c]
                                heappush(stack,[m,l+c,p])
for s,t in ST:
    if d[s][t][0]!=301:
        print(d[s][t][0])
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys
"""
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import time

import math
"""
"""def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
sys.setrecursionlimit(10**7)"""
input=lambda :sys.stdin.readline().rstrip()



N,M,L=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(M)]
ST=[list(map(int,input().split())) for i in range(int(input()))]

data=[{} for i in range(N+1)]
se=[set() for i in range(N+1)]
for A,B,C in ABC:
    data[A][B]=C
    data[B][A]=C
    se[A].add(B)
    se[B].add(A)

d=[[[301,0]]*(N+1) for i in range(N+1)]
for i in range(1,N+1):
    visited=set()
    stack=[[0,0,i]]
    while stack:
        m,l,a=heappop(stack)
        if not a in visited:
            visited.add(a)
            if len(visited)==N:
                break
            for p in se[a]-visited:
                    c=data[a][p]
                    if c<=L:
                        if l+c>L:
                            if d[i][p]>=[m+1,c]:
                                d[i][p]=[m+1,c]
                                d[p][i]=[m+1,c]
                                heappush(stack,[m+1,c,p])
                        else:
                            if d[i][p]>=[m,l+c]:
                                d[i][p]=[m,l+c]
                                d[p][i]=[m,l+c]
                                heappush(stack,[m,l+c,p])
for s,t in ST:
    if d[s][t][0]!=301:
        print(d[s][t][0])
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys
"""
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import time

import math
"""
"""def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
sys.setrecursionlimit(10**7)"""
input=lambda :sys.stdin.readline().rstrip()



N,M,L=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(M)]
ST=[list(map(int,input().split())) for i in range(int(input()))]

data=[{} for i in range(N+1)]
se=[set() for i in range(N+1)]
for A,B,C in ABC:
    data[A][B]=C
    data[B][A]=C
    se[A].add(B)
    se[B].add(A)

d=[[[301,0]]*(N+1) for i in range(N+1)]
for i in range(1,N+1):
    visited=set()
    stack=[[0,0,i]]
    while stack:
        m,l,a=heappop(stack)
        if not a in visited:
            visited.add(a)
            if len(visited)==N:
                break
            for p in se[a]-visited:
                    c=data[a][p]
                    if c<=L:
                        if l+c>L:
                            if d[i][p]>[m+1,c]:
                                d[i][p]=[m+1,c]
                                d[p][i]=[m+1,c]
                                heappush(stack,[m+1,c,p])
                        else:
                            if d[i][p]>[m,l+c]:
                                d[i][p]=[m,l+c]
                                d[p][i]=[m,l+c]
                                heappush(stack,[m,l+c,p])
for s,t in ST:
    if d[s][t][0]!=301:
        print(d[s][t][0])
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from collections import *
from heapq import *
from itertools import *
#from fractions import gcd

from decimal import *
import copy
from bisect import *
import time
import sys
import math

input=lambda :sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**7)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))

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

data=[[] for i in range(N+1)]
for A,B,C in ABC:
    data[A].append([B,C])
    data[B].append([A,C])

d=[[301 for n in range(N+1)] for i in range(N+1)]
for i in range(1,N+1):
    visited=set()
    stack=[[0,L,i]]
    while stack:
        m,l,a=heappop(stack)
        if not a in visited:
            visited.add(a)
            d[i][a]=m
            if len(visited)==N:
                break
            for p,c in data[a]:
                if not p in visited:
                    if l-c<0:
                        if c<=L:
                            heappush(stack,[m+1,L-c,p])
                    else:
                        heappush(stack,[m,l-c,p])
for s,t in ST:
    if d[s][t]!=301:
        print(d[s][t])
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from collections import *
from heapq import *
from itertools import *
#from fractions import gcd

from decimal import *
import copy
from bisect import *
import time
import sys
import math

input=lambda :sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**7)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))

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

data=[[] for i in range(N+1)]
for A,B,C in ABC:
    data[A].append([B,C])
    data[B].append([A,C])

d=[[301 for n in range(N+1)] for i in range(N+1)]
for i in range(1,N+1):
    visited=set()
    stack=[[0,L,i]]
    while stack:
        m,l,a=heappop(stack)
        if not a in visited:
            visited.add(a)
            d[i][a]=m
            if len(visited)==N:
                break
            for p,c in data[a]:
                if not p in visited:
                    if l-c<0:
                        heappush(stack,[m+1,L-c,p])
                    else:
                        if c<=L:
                            heappush(stack,[m,l-c,p])
for s,t in ST:
    if d[s][t]!=301:
        print(d[s][t])
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from collections import *
from heapq import *
from itertools import *
#from fractions import gcd

from decimal import *
import copy
from bisect import *
import time
import sys
import math

input=lambda :sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**7)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))

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

data=[[] for i in range(N+1)]
for A,B,C in ABC:
    data[A].append([B,C])
    data[B].append([A,C])

d=[[301 for n in range(N+1)] for i in range(N+1)]
for i in range(1,N+1):
    visited=set()
    stack=[[0,L,i]]
    while stack:
        m,l,a=heappop(stack)
        if not a in visited:
            visited.add(a)
            d[i][a]=m
            if len(visited)==N:
                break
            for p,c in data[a]:
                if not p in visited:
                    if l-c<0:
                        heappush(stack,[m+1,L-c,p])
                    else:
                        heappush(stack,[m,l-c,p])
for s,t in ST:
    if d[s][t]!=301:
        print(d[s][t])
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
n, m, l = [int(i) for i in input().split()]
d = [[10 ** 9] * n for _ in range(n)]
for i in range(n):
    d[i][i] = 0

for _ in range(m):
    a, b, c = [int(i) for i in input().split()]
    a -= 1
    b -= 1
    d[a][b] = min(d[a][b], c)
    d[b][a] = min(d[b][a], c)

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

d2 = [[10 ** 9] * n for _ 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] = min(d2[i][j], 1)

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

q = int(input())
for _ in range(q):
    s, t = [int(i) - 1 for i in input().split()]
    if d2[s][t] == 10 ** 9:
        print(-1)
    else:
        print(d2[s][t]-1)

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2**61+1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

def iip(listed=True):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def iip_ord():
    return [ord(i) - ord("a") for i in input()]


def main():
    ans = solve()
    if ans is not None:
        print(ans)

def solve():
    N, M, L = iip()

    dis_table = [[10 ** 12 for _ in range(N)] for _ in range(N)]
    for i in range(N):
        dis_table[i][i] = 0

    for i in range(M):
        a, b, c = iip()
        a -= 1
        b -= 1
        if c <= L:
            dis_table[a][b] = c
            dis_table[b][a] = c

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

    #print(dis_table)

    nt = [[10 ** 12 for _ in range(N)] for _ in range(N)]


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

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

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

    q = iip(False)
    ans = []

    for i in range(q):
        a, b = iip()
        c = nt[a-1][b-1]
        ans.append(c-1)

    split_print_enter(ans)


#####################################################ライブラリ集ここから

def split_print_space(s):
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):
    print("\n".join([str(i) for i in s]))


def koenai_saidai_x_index(sorted_list, n):
    l = 0
    r = len(sorted_list)
    if len(sorted_list) == 0:
        return False
    if sorted_list[0] > n:
        return False

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] == n:
            return x
        elif sorted_list[x] > n:
            r = x
        else:
            l = x
    return l


def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        # print(sorted_list)
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):
    return power(n, mod - 2)


def power(n, p, mod_= mod):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()