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:
N, M, L = map(int,input().split())
MAX=999999999
A=[]
B=[]
C=[]
D=[[MAX for _ in range(N)] for _ in range(N)]
F=[[MAX for _ in range(N)] for _ in range(N)]
s=[]
t=[]

for i in range(M):
    l=list(map(int,input().split()))
    A.append(l[0]-1)
    B.append(l[1]-1)
    C.append(l[2])



Q=int(input())

for i in range(Q):
    m=list(map(int,input().split()))
    s.append(m[0]-1)
    t.append(m[1]-1)


for i in range(M):
    if C[i]>L : continue
    D[A[i]][B[i]]=C[i]
    D[B[i]][A[i]]=C[i]

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

for k in range(N):
    for i in range(N):
        for j in range(N):
            if (D[i][k]+D[k][j])<D[i][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: F[i][j]=1


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

for k in range(N):
    for i in range(N):
        for j in range(N):
            if (F[i][k]+F[k][j])<F[i][j] :
                F[i][j]=F[i][k]+F[k][j]


for p in range(Q) :
    if F[s[p]][t[p]] < MAX :
        print(F[s[p]][t[p]]-1)
    else :
        print(-1)

Submission status: Wrong Answer

Solution code:
inf=10**9+1
n,m,l=map(int,input().split())
adj=[[0]*(n) for j in range(n)]

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

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

for k in range(n):
    for i in range(n):
        for j in range(n):
            adj[i][j]=min(adj[i][j],adj[i][k]+adj[k][j])
            
q=int(input())
for w in range(q):
    a,b=map(int,input().split())
    if adj[a-1][b-1]==inf:
        print(-1)
    else:
        print(adj[a-1][b-1]-1)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
N, M, L = list(map(int,input().split()))
P = [list(map(int,input().split())) for i in range(M)]
Q = int(input())
st = [list(map(int,input().split())) for i in range(Q)]

INF = 100100100

dp = [[INF]*N for i in range(N)]
for s,e,d in P:
	if d<=L:
		dp[s-1][e-1] = d
		dp[e-1][s-1] = d
for i in range(N):
	dp[i][i] = 0

for k in range(N):
	for y in range(N):
		for x in range(N):
			dp[y][x] = min(dp[y][x], dp[y][k]+dp[k][x])

dp2 = [[INF]*N for i in range(N)]
for y in range(N):
	for x in range(N):
		if dp[y][x]<=L:
			dp2[y][x] = 1

for k in range(N):
	for y in range(N):
		for x in range(N):
			dp2[y][x] = min(dp2[y][x], dp2[y][k]+dp2[k][x])

for s, t in st:
	count = dp2[s-1][t-1]
	if count==INF:
		print(-1)
	else:
		print(count-1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!



import sys
#sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

#n = int(input())
#a = [int(i) for i in readline().split()]

n,m,l = [int(i) for i in readline().split()]


inf = 1100100100
g = [[inf for _ in range(n)] for i in range(n)]
for i in range(n): g[i][i] = 0

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

for i in range(m):
    a,b,c = [int(i) for i in readline().split()]
    if c <= l:
#        g[a-1][b-1] = c
#        g[b-1][a-1] = c
        dist[a-1][b-1] = (0,c,c)
        dist[b-1][a-1] = (0,c,c)




def add(p,q):
    a,b,e = p
    c,d,f = q
    if a >= inf or c >= inf: return (inf,0,0)
    a1 = a+c
    a2 = b+d
    a3 = e+f
    if b+f > l or a2 > l:
        a1 += 1
        a2 = d
        a3 = e
    return (a1,a2,a3)    

#print(dist)

num_of_vertices = n
for k in range(num_of_vertices):
    for i in range(num_of_vertices):
        for j in range(num_of_vertices):
#            if i==4 and j == 1: print(dist[i][j],k,dist[i][k],dist[k][j],add(dist[i][k],dist[k][j]))
            dist[i][j] = min(dist[i][j], add(dist[i][k],dist[k][j]))

#print(dist)

q = int(input())
for _ in range(q):
    s,t = [int(i)-1 for i in readline().split()]
    a,b,c = dist[s][t]
    
    if a >= inf: print(-1)
    else: print(a)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/pypy
# coding: utf-8

N,M,L=map(int, raw_input().split())
RG=[[ 0 if i==j else float('inf') for i in range(N)] for j in range(N)]

for i in xrange(M):
    f,t,c = map(int, raw_input().split())
    if c <= L:
        RG[f-1][t-1] = c
        RG[t-1][f-1] = c

for i in xrange(N):
    for j in xrange(N):
        for k in xrange(N):
            t = RG[i][k] + RG[k][j]
            if RG[i][j] > t:
                RG[i][j] = t
                RG[j][i] = t

FG=[[float('inf') for i in range(N)] for j in range(N)]
for i in xrange(N):
    for j in xrange(i, N):
        if RG[i][j] <= L:
            FG[i][j] = 1
            FG[j][i] = 1

for i in xrange(N):
    for j in xrange(N):
        for k in xrange(N):
            t = FG[i][k] + FG[k][j]
            if FG[i][j] > t:
                FG[i][j] = t
                FG[j][i] = t

Q=int(raw_input())
for i in xrange(Q):
    s, t = map(int, raw_input().split())
    v = FG[s-1][t-1]
    if v == float('inf'):
        print (str(-1))
    else:
        print (str(int(v-1)))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/pypy
# coding: utf-8

N,M,L=map(int, raw_input().split())
RG=[[ 0 if i==j else float('inf') for i in range(N)] for j in range(N)]

for i in xrange(M):
    f,t,c = map(int, raw_input().split())
    if c <= L:
        RG[f-1][t-1] = c
        RG[t-1][f-1] = c

for i in xrange(N):
    for j in xrange(i+1, N):
        for k in xrange(N):
            t = RG[i][k] + RG[k][j]
            if RG[i][j] > t:
                RG[i][j] = t
                RG[j][i] = t

FG=[[float('inf') for i in range(N)] for j in range(N)]
for i in xrange(N):
    for j in xrange(i, N):
        if RG[i][j] <= L:
            FG[i][j] = 1
            FG[j][i] = 1

for i in xrange(N):
    for j in xrange(i+1, N):
        for k in xrange(N):
            t = FG[i][k] + FG[k][j]
            if FG[i][j] > t:
                FG[i][j] = t
                FG[j][i] = t

Q=int(raw_input())
for i in xrange(Q):
    s, t = map(int, raw_input().split())
    v = FG[s-1][t-1]
    if v == float('inf'):
        print (str(-1))
    else:
        print (str(int(v-1)))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/pypy
# coding: utf-8

N,M,L=map(int, raw_input().split())
RG=[[ 0 if i==j else float('inf') for i in range(N)] for j in range(N)]

for i in xrange(M):
    f,t,c = map(int, raw_input().split())
    if c <= L:
        RG[f-1][t-1] = c
        RG[t-1][f-1] = c

for i in xrange(N):
    for j in xrange(i, N):
        for k in xrange(N):
            t = RG[i][k] + RG[k][j]
            if RG[i][j] > t:
                RG[i][j] = t
                RG[j][i] = t

FG=[[0 if i==j else float('inf') for i in range(N)] for j in range(N)]
for i in xrange(N):
    for j in xrange(i, N):
        if RG[i][j] <= L:
            FG[i][j] = 1
            FG[j][i] = 1

for i in xrange(N):
    for j in xrange(i, N):
        for k in xrange(N):
            t = FG[i][k] + FG[k][j]
            if FG[i][j] > t:
                FG[i][j] = t
                FG[j][i] = t

Q=int(raw_input())
for i in xrange(Q):
    s, t = map(int, raw_input().split())
    v = FG[s-1][t-1]
    if s == t:
        print (str(0))
    if v == float('inf'):
        print (str(-1))
    else:
        print (str(int(v-1)))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/pypy
# coding: utf-8

N,M,L=map(int, raw_input().split())
RG=[[ 0 if i==j else float('inf') for i in range(N)] for j in range(N)]

for i in xrange(M):
    f,t,c = map(int, raw_input().split())
    if c <= L:
        RG[f-1][t-1] = c
        RG[t-1][f-1] = c

for i in xrange(N):
    for j in xrange(i, N):
        for k in xrange(i, N):
            t = RG[i][k] + RG[k][j]
            if RG[i][j] > t:
                RG[i][j] = t
                RG[j][i] = t

FG=[[0 if i==j else float('inf') for i in range(N)] for j in range(N)]
for i in xrange(N):
    for j in xrange(i, N):
        if RG[i][j] <= L:
            FG[i][j] = 1
            FG[j][i] = 1

for i in xrange(N):
    for j in xrange(i, N):
        for k in xrange(i, N):
            t = FG[i][k] + FG[k][j]
            if FG[i][j] > t:
                FG[i][j] = t
                FG[j][i] = t

Q=int(raw_input())
for i in xrange(Q):
    s, t = map(int, raw_input().split())
    v = FG[s-1][t-1]
    if s == t:
        print (str(0))
    if v == float('inf'):
        print (str(-1))
    else:
        print (str(int(v-1)))

Submission status: Wrong Answer

Solution code:
n,m,l = map(int,input().split())
A = [list(map(int,input().split())) for i in range(m)]
q = int(input())
S = [list(map(int,input().split())) for i in range(q)]

from scipy.sparse.csgraph import floyd_warshall

for i in range(m):  # ノード番号が1始まりで与えられている場合
    for j in range(2):
        A[i][j] -= 1

for i in range(q):  # ノード番号が1始まりで与えられている場合
    for j in range(2):
        S[i][j] -= 1

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

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

for i in range(m):
    M[A[i][0]][A[i][1]] = A[i][2]
    M[A[i][1]][A[i][0]] = A[i][2]

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

M = floyd_warshall(M)

for i in range(n):
    for j in range(n):
        M[i][j] = 1 if M[i][j] <= l else float("inf")

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

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

M = floyd_warshall(M)

for i in range(q):
    ans = M[S[i][0]][S[i][1]]
    if ans == float("inf"):
        ans = -1
    print(ans)

Submission status: Wrong Answer

Solution code:
n,m,l = map(int,input().split())
A = [list(map(int,input().split())) for i in range(m)]
q = int(input())
S = [list(map(int,input().split())) for i in range(q)]

for i in range(m):  # ノード番号が1始まりで与えられている場合
    for j in range(2):
        A[i][j] -= 1

for i in range(q):  # ノード番号が1始まりで与えられている場合
    for j in range(2):
        S[i][j] -= 1

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

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

for i in range(m):
    M[A[i][0]][A[i][1]] = A[i][2]
    M[A[i][1]][A[i][0]] = A[i][2]

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

for i in range(n):
    for j in range(n):
        M[i][j] = 1 if M[i][j] <= l else float("inf")

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

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

for i in range(q):
    ans = M[S[i][0]][S[i][1]]
    if ans == float("inf"):
        ans = -1
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n,m,l = [int(j) for j in raw_input().split()]
adj_list = [[] for i in range(n)]
adj_list2 = [[] for i in range(n)]
dist = defaultdict(lambda: 10**12 )
for i in range(m):
    a,b,c = [int(j) for j in raw_input().split()]
    a = a-1
    b = b-1
    adj_list[a].append((b, c))
    adj_list[b].append((a, c))
    dist[(a,b)] = c
    dist[(b,a)] = c

for i in range(n):
    for j in range(n):
        for k in range(n):
                if i!=j:
                        dist[(i,j)] = min(dist[(i,j)], dist[(i,k)] + dist[(k,j)])
                else:
                        dist[(i,j)] = 0
dist2 = defaultdict(lambda: 10**12)
for i in dist:
    if dist[i]<=l:
        dist2[(i[0], i[1])] = 1
        dist2[(i[1], i[0])] = 1
        # adj_list2[i[0]].append(i[1])
        # adj_list2[i[1]].append(i[0])
        # dist2 = 

for i in range(n):
        for j in range(n):
                for k in range(n):
                        if i!=j:
                                dist2[(i,j)] = min(dist2[(i,j)], dist2[(i,k)] + dist2[(k,j)])
                        else:
                                dist2[(i,j)] = 0
# print(dist2, dist)
for i in range(int(input())):
        s,t = [int(j) for j in raw_input().split()]
        if dist2[(s-1,t-1)] == 10**12:
                print(-1)
        else:
                print(dist2[(s-1,t-1)]-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n,m,l = map(int,input().split())
town = [list(map(int,input().split())) for _ in range(m)]
q = int(input())
query = [list(map(int,input().split())) for _ in range(q)]

inf = (l+1)*n
d = [[inf]*n for _ in range(n)]
for i in range(n):
    d[i][i] = 0
for a,b,c in town:
    a -= 1
    b -= 1
    d[a][b] = c
    d[b][a] = c
    
def warshall_floyd(d,n):
    for i in range(n):
        for j in range(n):
            for k 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 val<=l else inf for val in row]
    d2[i][i] = 0

warshall_floyd(d2,n)
 
for s,t in query:
    s -= 1
    t -= 1
    count = (d2[s][t]%inf)-1
    print(count)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n,m,l = map(int,input().split())
town = [list(map(int,input().split())) for _ in range(m)]
q = int(input())
query = [list(map(int,input().split())) for _ in range(q)]

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

inf = (l+1)*n
d = [[inf]*n for _ in range(n)]
for i in range(n):
    d[i][i] = 0
for a,b,c in town:
    a -= 1
    b -= 1
    d[a][b] = c
    d[b][a] = c
    
warshall_floyd(d,n)

d2 = [None]*n
for i,row in enumerate(d):
    d2[i] = [1 if val<=l else inf for val in row]
    d2[i][i] = 0

warshall_floyd(d2,n)
 
for s,t in query:
    s -= 1
    t -= 1
    count = (d2[s][t]%inf)-1
    print(count)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

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

inf = (l+1)*n
matrix = [[inf]*n for _ in [0]*n]
for i in range(n):
    matrix[i][i] = 0
for a,b,c in town:
    a -= 1
    b -= 1
    matrix[a][b] = c
    matrix[b][a] = c
    
warshall_floyd(n,matrix)

matrix2 = [None]*n
for i,row in enumerate(matrix):
    matrix2[i] = [1 if val<=l else inf for val in row]
    matrix2[i][i] = 0

warshall_floyd(n,matrix2)
 
for s,t in query:
    s -= 1
    t -= 1
    count = (matrix2[s][t] % inf) - 1
    print(count)

Submission status: Wrong Answer

Solution code:
n,m,l = map(int,input().split())
town = [list(map(int,input().split())) for i in range(m)]
q = int(input())
query = [list(map(int,input().split())) for i in range(q)]

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

inf = (l+1)*n
matrix = [[inf]*n for _ in [0]*n]
for i in range(n):
    matrix[i][i] = 0
for a,b,c in town:
    a -= 1
    b -= 1
    matrix[a][b] = c
    matrix[b][a] = c
    
warshall_floyd(n,matrix)

matrix2 = [None]*n
for i,row in enumerate(matrix):
    matrix2[i] = [1 if val<=l else inf for val in row]
    matrix2[i][i] = 0

warshall_floyd(n,matrix2)
 
for s,t in query:
    s -= 1
    t -= 1
    count = (matrix2[s][t] % inf) - 1
    print(count)

Submission status: Wrong Answer

Solution code:
n,m,l = map(int,input().split())
town = [list(map(int,input().split())) for i in range(m)]
q = int(input())
query = [list(map(int,input().split())) for i in range(q)]

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

inf = (l+1)*n
matrix = [[inf]*n for _ in range(n)]
for i in range(n):
    matrix[i][i] = 0
for a,b,c in town:
    a -= 1
    b -= 1
    matrix[a][b] = matrix[b][a] = c
    
warshall_floyd(n,matrix)

matrix2 = [None]*n
for i,row in enumerate(matrix):
    matrix2[i] = [1 if val<=l else inf for val in row]
    matrix2[i][i] = 0

warshall_floyd(n,matrix2)
 
for s,t in query:
    s -= 1
    t -= 1
    count = (matrix2[s][t] % inf) - 1
    print(count)

Submission status: Wrong Answer

Solution code:
n,m,l = map(int,input().split())
town = [list(map(int,input().split())) for i in range(m)]
q = int(input())
query = [list(map(int,input().split())) for i in range(q)]

def warshall_floyd(n,mat):
    for i in range(n):
        for j in range(n):
            for k in range(n):
                mat[i][j] = min(mat[i][j],mat[i][k]+mat[k][j])
    return mat

inf = (l+1)*n
matrix = [[inf]*n for _ in range(n)]
for i in range(n):
    matrix[i][i] = 0
for a,b,c in town:
    a -= 1
    b -= 1
    matrix[a][b] = matrix[b][a] = c
    
matrix = warshall_floyd(n,matrix)

matrix2 = [None]*n
for i,row in enumerate(matrix):
    matrix2[i] = [1 if val<=l else inf for val in row]
    matrix2[i][i] = 0

matrix2 = warshall_floyd(n,matrix2)
 
for s,t in query:
    s-=1
    t-=1
    count = (matrix2[s][t] % inf) - 1
    print(count)

Submission status: Wrong Answer

Solution code:
n,m,l=map(int,input().split())
g=[[999999999999 if i!=j else 0 for j in range(n)] for i in range(n)]
for i in range(m):
    a,b,c=map(int,input().split())
    a,b=a-1,b-1
    if c>l:
        continue
    g[a][b]=c
    g[b][a]=c

for k in range(n):
    for i in range(n):
        for j in range(n):
            if g[i][j]>g[i][k]+g[k][j]:
                g[i][j] = g[i][k]+g[k][j]

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

for k in range(n):
    for i in range(n):
        for j in range(n):
            if g[i][j]>g[i][k]+g[k][j]:
                g[i][j] = g[i][k]+g[k][j]

for i in range(int(input())):
    s,t=map(int,input().split())
    s-=1
    t-=1
    print(g[s][t]-1)

Submission status: Wrong Answer

Solution code:
def dijkstra(g,st,ed):
    from heapq import heappush, heappop, heapify
    inf=10**18
    dist=[inf for i in range(len(g))]
    visited=[False for i in range(len(g))]
    pre=[-1 for i in range(len(g))]
    dist[st]=0
    que=[]
    heappush(que,(0,st))
    while que:
        c,v=heappop(que)
        if visited[v]:
            continue
        visited[v]=True
        for x in g[v]:
            tmp=x[0]+c
            if tmp<dist[x[1]] and not visited[x[1]]:
                dist[x[1]]=tmp
                pre[x[1]]=v
                heappush(que,(tmp,x[1]))
    route=[ed]
    while True:
        if pre[ed]!=-1:
            route.append(pre[ed])
            ed=pre[ed]
        else:
            break
    return dist, route[::-1]



n,m,l=map(int,input().split())
g=[[] for i in range(n)]
for i in range(m):
    a,b,c=map(int,input().split())
    a,b=a-1,b-1
    if c>l:
        continue
    g[a].append((c,b))
    g[b].append((c,a))
for i in range(int(input())):
    ans=0
    s,t=map(int,input().split())
    s,t=s-1,t-1
    dist, route = dijkstra(g,s,t)
    if len(route)<2:
        print('-1')
        continue
    run=l
    for j in range(1,len(route)):
        #print('run:',run,end=' ')
        run-=dist[route[j]]-dist[route[j-1]]
        if run<0:
            run=l
            run-=dist[route[j]]-dist[route[j-1]]
            ans+=1
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
input = sys.stdin.readline

def floyd_warshall(cost,n):
    for i in range(n):
        for j in range(n):
            for k in range(n):
                new = min(cost[i][j], cost[i][k] + cost[k][j])
                cost[i][j] = new
                cost[j][i] = new
    return np.array(cost)

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,n)
    G = np.where(G <=l, 1, INF)
    for i in range(n):
        G[i][i] = 0
    GG = floyd_warshall(G,n)

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

Submission status: Wrong Answer

Solution code:
n,m,l = map(int, input().split())
inf = 10**9
cost = [[inf for _ in range(n)] for _ in range(n)] 

ans = []

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

for _ in range(m):
    a,b,c = map(int, input().split())
    cost[a-1][b-1] = c
    cost[b-1][a-1] = c
    
for i in range(n):
    for j in range(n):
        for k in range(n):
            new = min(cost[i][j], cost[i][k] + cost[k][j])
            cost[i][j] = new
            cost[j][i] = new

q = int(input())
for _ in range(q):
    s,t = map(int, input().split())
    ans.append(cost[s-1][t-1])
    
for i in range(q):
    if ans[i]==inf:
      print(-1)
    else:
      p,r = divmod(ans[i],l)
      if r==0:
        print(p-1)
      else:
        print(p)

Submission status: Wrong Answer

Solution code:
n,m,l = map(int, input().split())
inf = 10**9
cost = [[inf for _ in range(n)] for _ in range(n)] 

ans = []

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

for _ in range(m):
    a,b,c = map(int, input().split())
    cost[a-1][b-1] = c
    cost[b-1][a-1] = c
    
for i in range(n):
    for j in range(n):
        for k in range(n):
            new = min(cost[i][j], cost[i][k] + cost[k][j])
            cost[i][j] = new
            cost[j][i] = new

q = int(input())
for _ in range(q):
    s,t = map(int, input().split())
    ans.append(cost[s-1][t-1])
    
for i in range(q):
    p,r = divmod(ans[i],l)
    if r==0:
      print(p-1)
    else:
      print(p)

Submission status: Wrong Answer

Solution code:
n,m,l = map(int, input().split())
inf = 10**9
cost = [[inf for _ in range(n)] for _ in range(n)] 

ans = []

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

for _ in range(m):
    a,b,c = map(int, input().split())
    cost[a-1][b-1] = c
    cost[b-1][a-1] = c
    
for i in range(n):
    for j in range(n):
        for k in range(n):
            new = min(cost[i][j], cost[i][k] + cost[k][j])
            cost[i][j] = new
            cost[j][i] = new

q = int(input())
for _ in range(q):
    s,t = map(int, input().split())
    ans.append(cost[s-1][t-1])
    
for i in range(q):
    print((ans[i]+0.5)//l)

Submission status: Wrong Answer

Solution code:
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d


n,m,l= map(int,input().split()) #n:頂点数　w:辺の数

d=[[float("inf") for i in range(n)] for i in range(n)] 
#d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(m):
    a,b,c = map(int,input().split())
    d[a-1][b-1]=c
    d[b-1][a-1]=c
for i in range(n):
    d[i][i] = 0 #自身のところに行くコストは０
#print(warshall_floyd(d))

d2=[[float("inf") for i in range(n)] for i in range(n)] 
for i in range(n):
    for j in range(n):
        if d[i-1][j-1]<=l:
            d2[i-1][j-1]=1
            d2[j-1][i-1]=1
#print(warshall_floyd(d2))
q=int(input())
l=[list(map(int, input().split())) for i in range(q)]
for i in range(q):
    if d2[l[i][0]-1][l[i][1]-1]-1!=float('inf'):
        print(d2[l[i][0]-1][l[i][1]-1]-1)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
class WarshallFloyd():
    def __init__(self, N):
        self.N = N
        self.d = [[float("inf") for i in range(N)]
                  for i in range(N)]  # d[u][v] : 辺uvのコスト(存在しないときはinf)

    def add(self, u, v, c, directed=False):
        """
        0-indexedであることに注意
        u = from, v = to, c = cost
        directed = Trueなら、有向グラフである
        """
        if directed is False:
            self.d[u][v] = c
            self.d[v][u] = c
        else:
            self.d[u][v] = c

    def WarshallFloyd_search(self):
        # これを d[i][j]: iからjへの最短距離 にする
        # 本来無向グラフでのみ全域木を考えるが、二重辺なら有向でも行けそう
        # d[i][i] < 0 なら、グラフは負のサイクルを持つ
        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])
        hasNegativeCycle = False
        for i in range(self.N):
            if self.d[i][i] < 0:
                hasNegativeCycle = True
                break
        for i in range(self.N):
            self.d[i][i] = 0
        return hasNegativeCycle, self.d


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

graph=WarshallFloyd(n)
for a,b,c in ABC:
    graph.add(a-1,b-1,c)
hasNegativeCycle, d = graph.WarshallFloyd_search()
#ans = sorted([[i, max(d[i])] for i in range(n)], key=lambda x: x[0])
ans=graph.WarshallFloyd_search()
#print(ans)
L=[]
for i in range(n):
    for j in range(n):
        L.append([i+1,j+1,ans[1][i][j]])
#print(L)
for a,b,c in L:
    if 0<c<=l:
        graph.add(a-1,b-1,1)
ans1=graph.WarshallFloyd_search()
#print(ans1)
for i,j in st:
    if ans1[1][i-1][j-1]-1!=float('inf'):
        print(ans1[1][i-1][j-1]-1)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
N,M,L=map(int,input().split())
INF=float('inf')
arr=[[INF]*N for _ in range(N)]
arr2=[[INF]*N for _ in range(N)]
for i in range(M):
  x,y,l=map(int,input().split())
  arr[x-1][y-1]=l
  arr[y-1][x-1]=l
  for j in range(N):
    arr[j][j]=0
Q=int(input())
lst=[[] for _ in range(Q)]
for i in range(Q):
  lst[i]=list(map(int,input().split()))

def warshal_floyd(arr):
  for i in range(N):
    for j in range(N):
      for k in range(N):
        arr[i][j]=min(arr[i][j],arr[i][k]+arr[k][j])
      if arr[i][j]<=L:
         arr2[i][j]=1     
  return arr2

def warshal_floyd2(arr):
  for i in range(N):
    for j in range(N):
      for k in range(N):
        arr[i][j]=min(arr[i][j],arr[i][k]+arr[k][j])
  return arr
arr2=warshal_floyd(arr)
arr2=warshal_floyd2(arr2)

for i in range(Q):
  s,t=lst[i][0],lst[i][1]
  if arr2[s-1][t-1]==INF:
    print(-1)
  else:
    print(arr2[s-1][t-1]-1)

Submission status: Wrong Answer

Solution code:
N,M,L=map(int,input().split())
INF=float('inf')
arr=[[INF]*N for _ in range(N)]
for i in range(M):
  x,y,l=map(int,input().split())
  arr[x-1][y-1]=l
  arr[y-1][x-1]=l
  for j in range(N):
    arr[j][j]=0
Q=int(input())
lst=[[] for _ in range(Q)]
for i in range(Q):
  lst[i]=list(map(int,input().split()))

def warshal_floyd(arr):
  for i in range(N):
    for j in range(N):
      for k in range(N):
        arr[i][j]=min(arr[i][j],arr[i][k]+arr[k][j])
  return arr
arr=warshal_floyd(arr)

arr2=[[INF]*N for _ in range(N)]
for i in range(N):
  for j in range(N):
    if arr[i][j]<=L:
      arr2[i][j]=1

arr2=warshal_floyd(arr2)
              
for i in range(Q):
  s,t=lst[i][0],lst[i][1]
  if arr2[s-1][t-1]==INF:
    print(-1)
  else:
    if arr2[s-1][t-1]%L==0:
      print(arr[s-1][t-1])
    else:
      print(arr2[s-1][t-1]-1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N,M,L = [int(c) for c in input().strip().split(' ')]
adj=defaultdict(list)
for i in range(M):
    a,b,c = [int(c) for c in input().strip().split(' ')]
    adj[a].append((b,c))
    adj[b].append((a,c))

q = int(input().strip())
while(q>0):
    a,b = [int(c) for c in input().strip().split(' ')]
    queue = [(a,L,0)]
    visit=defaultdict(lambda:False)
    flag=False
    while len(queue)!=0:
        s = queue.pop(0)
        if s[0]==b:
            flag = True
            print(s[2])
            break
        visit[s[0]] = True
        for el in adj[s[0]]:
            if not visit[el[0]]:
                if s[1] >= el[1]:
                    queue.append((el[0],s[1]-el[1],s[2]))
                else:
                    queue.append((el[0],s[1]-el[1]+L,s[2]+1))
    if not flag:
        print(-1)
    
    q-=1