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:
INF=10**13
N,M,L=map(int,input().split())
d=[[INF for i in range(N)] for j in range(N)]
for i in range(N):
    d[i][i]=0
for m in range(M):
    A,B,C=map(int,input().split())
    d[A-1][B-1]=C
    d[B-1][A-1]=C

for k in range(N):
    for i in range(N):
        for j in range(N):
            d[i][j]=min(d[i][j],d[i][k]+d[k][j])
for i in range(N):
        for j in range(N):
            if d[i][j]<=L:
                d[i][j]=1
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])
ans=[]
Q=int(input())
for i in range(Q):
    s,t=map(int,input().split())
    dis=d[s-1][t-1]
    if dis==INF:
        ans.append(-1)
    else:
        ans.append(dis-1)

for a in ans:
    print(int(a))

Submission status: Wrong Answer

Solution code:
def main():
    N, M, L = map(int, input().split())
    adj = [{} for _ in range(N)]
    for _ in range(M):
        A, B, C = map(int, input().split())
        adj[A-1][B-1] = C
        adj[B-1][A-1] = C
    ans = [[(float("inf"), 0)] * N for _ in range(N)]
    for i in range(N):
        ans[i][i] = (0, 0)
        for j in adj[i]:
            if adj[i][j] <= L:
                ans[i][j] = (0, adj[i][j])
    for k in range(N):
        for i in range(N):
            for j in range(N):
                a = ans[i][k]
                b = ans[k][j]
                if b[0] == 0 and a[1] + b[1] <= L:
                    tmp = (a[0] + b[0], a[1] + b[1])
                else:
                    tmp = (a[0] + b[0] + 1, b[1])
                ans[i][j] = min(ans[i][j], tmp)
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        x = ans[s-1][t-1][0]
        print(-1 if x == float("inf") else x)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
INF=10**18
N,M,L=map(int,input().split())
d=np.zeros((N,N))
d[:,:]=INF
for i in range(N):
    d[i,i]=0
for m in range(M):
    A,B,C=map(int,input().split())
    d[A-1,B-1]=C
    d[B-1,A-1]=C

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

ans=[]
Q=int(input())
for i in range(Q):
    s,t=map(int,input().split())
    dis=d[s-1,t-1]
    if dis==INF:
        ans.append(-1)
    else:
        ans.append((dis-1)//L)

for a in ans:
    print(int(a))

Submission status: Wrong Answer

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

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

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

dp = [[float('inf')]*n for _ in range(n)]

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

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

dp = warshall_floyd(dp)

dp2 = [[float('inf')]*n for _ in range(n)]

for i in range(n):
    for j in range(n):
        if dp[i][j] <= l:
            dp2[i][j] = 1
    dp2[i][i] = 0
dp2 = warshall_floyd(dp2)

q = int(input())

ans = [0]*q
for i in range(q):
    s, t = map(int,input().split())
    tmp = dp2[s-1][t-1]
    ans[i] = -1 if tmp == float('inf') else tmp - 1

print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
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)]

dp = [[float('inf') for i in range(n)] for i in range(n)]

for a,b,c in abc:
    a, b = a-1, b-1
    if c > l: continue
    dp[a][b] = dp[b][a] = c

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

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

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

for i in range(n):
    for j in range(n):
        for k in range(n):
            dp[i][j] = min(dp[i][j] , dp[i][k] + dp[k][j])
            
for s,t in st:
    s = s-1
    t = t-1
    if dp[s][t] == float('inf'):
        print(-1)
    else:
        print(dp[s][t]-1)

Submission status: Wrong Answer

Solution code:
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)]

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


    return dp

dp = [[float('inf') for i in range(n)] for i in range(n)]

for a,b,c in abc:
    a, b = a-1, b-1
    if c > l: continue
    dp[a][b] = dp[b][a] = c

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

dp= warshall_floyd(dp)

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

dp= warshall_floyd(dp)

for s,t in st:
    s = s-1
    t = t-1
    if dp[s][t] == float('inf'):
        print(-1)
    else:
        print(dp[s][t]-1)

Submission status: Wrong Answer

Solution code:
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)]

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


    return dp


dp1 = [[float('inf') for i in range(n)] for i in range(n)]
dp2 = [[float('inf') for i in range(n)] for i in range(n)]

for a,b,c in abc:
    a = a - 1
    b = b - 1
    if c > l:
        c = float('inf')     
    dp1[a][b] = dp1[b][a] = c

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

dp1= warshall_floyd(dp1)

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

dp2= warshall_floyd(dp2)

for s,t in st:
    s = s-1
    t = t-1
    if dp2[s][t] == float('inf'):
        print(-1)
    else:
        print(dp2[s][t]-1)

Submission status: Wrong Answer

Solution code:
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)]

lmod = 10**12

def warshall_floyd(dp):
    #d[i][j]: iからjへの最短距離 + lmod * 補給回数
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if dp[i][j] > dp[i][k] + dp[k][j]:
                    if (dp[i][k] + dp[k][j])%lmod > l:
                        if j > i:
                            dp[i][j] = (dp[i][k]-dp[i][k]%lmod) + dp[k][j] + lmod
                        else:
                            dp[i][j] = (dp[k][j]-dp[k][j]%lmod) + dp[i][k] + lmod
                    else:
                        dp[i][j] = dp[i][k] + dp[k][j]


    return dp


dp = [[float('inf') for i in range(n)] for i in range(n)] 
for a,b,c in abc:
    a = a - 1
    b = b - 1
    if c > l:
        c = float('inf')     
    
    dp[a][b] = c
    dp[b][a] = c
for i in range(n):
    dp[i][i] = 0

dp_ = warshall_floyd(dp)

for s,t in st:
    s = s-1
    t = t-1
    if dp_[s][t] == float('inf'):
        print(-1)
    else:
        print(dp_[s][t]//lmod)

Submission status: Wrong Answer

Solution code:
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
N,M,L=map(int,input().split())
l=[[10**12 for i in range(N)] for j in range(N)]
for i in range(M):
  a,b,c=map(int,input().split())
  l[a-1][b-1]=c
  l[b-1][a-1]=c
graph=csr_matrix(l)
d=floyd_warshall(graph)
for i in range(N):
  for j in range(N):
    if d[i][j]==0:
      d[i][j]=10**12
    elif d[i][j]<=L:
      d[i][j]=1
d2=floyd_warshall(d)
Q=int(input())
for i in range(Q):
  s,t=map(int,input().split())
  print(-1 if d2[s-1][t-1]==10**12 else d2[s-1][t-1]-1)

Submission status: Wrong Answer

Solution code:
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
N,M,L=map(int,input().split())
l=[[10**12 for i in range(N)] for j in range(N)]
for i in range(M):
  a,b,c=map(int,input().split())
  l[a-1][b-1]=c
  l[b-1][a-1]=c
graph=csr_matrix(l)
d=floyd_warshall(graph)
for i in range(N):
  for j in range(N):
    if d[i][j]==0:
      d[i][j]=10**12
    elif d[i][j]<=L:
      d[i][j]=1
d2=floyd_warshall(d)
Q=int(input())
for i in range(Q):
  s,t=map(int,input().split())
  print(d2[s-1][t-1]-1)

Submission status: Wrong Answer

Solution code:
import sys

N, M, L = list(map(int, sys.stdin.readline().strip().split(" ")))
INF = 10**10

def warshall_floyd(g_mat):
    dp = [[INF for _ in range(N)] for _ in range(N)]
    for k in range(N):
        for i in range(N):
            for j in range(N):
                dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j])
    return dp

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

for _ in range(M):
    a, b, c = list(map(int, sys.stdin.readline().strip().split(" ")))
    graph[a-1][b-1] = c
    graph[b-1][a-1] = c

dist_graph = warshall_floyd(graph)
bool_graph = [[INF for _ in range(N)] for _ in range(N)]

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

ans_graph = warshall_floyd(bool_graph)

Q = int(sys.stdin.readline().strip())
for _ in range(Q):
    s, t = list(map(int, sys.stdin.readline().strip().split(" ")))
    ans = ans_graph[s-1][t-1]
    print(ans if ans != INF else -1)

Submission status: Wrong Answer

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

n,m,l = map(int,input().split())
adj = [[float('inf') for _ in range(n)] for _ in range(n)]
for _ in range(m):
    a,b,c = map(int,input().split())
    if c <= l:
        adj[a-1][b-1] = c
        adj[b-1][a-1] = c
for i in range(n):
    adj[i][i] = 0
for i in range(n):
    for j in range(n):
        for k in range(n):
            adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j])

adj2 = [[float('inf') for _ in range(n)] for _ in range(n)]
for i in range(n):
    adj2[i][i] = 0
for i in range(n):
    for j in range(n):
        if adj[i-1][j-1] <= l:
            adj2[i-1][j-1] = 1

for i in range(n):
    for j in range(n):
        for k in range(n):
            adj2[i][j] = min(adj2[i][j], adj2[i][k] + adj2[k][j])
            
q = int(input())
for _ in range(q):
    s,t = map(int,input().split())
    if (adj2[s-1][t-1] == float('inf')):
        print(-1)
    else:
        print(adj2[s-1][t-1] - 1)

Submission status: Wrong Answer

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

n,m,l = map(int,input().split())
adj = [[float('inf') for _ in range(n)] for _ in range(n)]
for _ in range(m):
    a,b,c = map(int,input().split())
    if c <= l:
        adj[a-1][b-1] = c
        adj[b-1][a-1] = c

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

adj2 = [[float('inf') for _ in range(n)] for _ in range(n)]
for i in range(n):
    for j in range(n):
        if adj[i-1][j-1] <= l:
            adj2[i-1][j-1] = 1

for i in range(n):
    for j in range(n):
        for k in range(n):
            adj2[i][j] = min(adj2[i][j], adj2[i][k] + adj2[k][j])
            
q = int(input())
for _ in range(q):
    s,t = map(int,input().split())
    if (adj2[s-1][t-1] == float('inf')):
        print(-1)
    else:
        print(adj2[s-1][t-1] - 1)

Submission status: Wrong Answer

Solution code:
import numpy as np
INF = 10**12
N, M, L = map(int, input().split())
d = np.zeros((N, N), dtype=np.int64) + INF

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

for i in range(M):
    a, b, c = map(int, input().split())
    if L >= c:
        d[a - 1][b - 1] = c
        d[b - 1][a - 1] = c
        
for i in range(N):
    d[i][i] = 0

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

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    return

#B
def B():
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    n, m, l = LI()
    dist = [[inf] * n for i in range(n)]
    for _ in range(m):
        a, b, c = LI_()
        c += 1
        if c > l:
            continue
        dist[a][b] = c
        dist[b][a] = c
    for i in range(n):
        dist[i][i] = 0
    prev = [[i for j in range(n)] for i in range(n)]
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if dist[i][k] + dist[k][j] < dist[i][j]:
                    dist[i][j] = dist[i][k] + dist[k][j]
                    prev[i][j] = prev[k][j]

    q = II()
    for _ in range(q):
        s, t = LI_()
        tank = 0
        ans = 0
        if dist[s][t] == inf:
            print(-1)
            continue

        while t != s:
            cur = prev[s][t]
            if dist[t][cur] + tank > l:
                tank = 0
                ans += 1
            tank += dist[t][cur]
            t = cur
        print(ans)
    return


#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    E()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    return

#B
def B():
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    n, m, l = LI()
    dist = [[inf] * n for i in range(n)]
    for _ in range(m):
        a, b, c = LI_()
        c += 1
        if c > l:
            continue
        dist[a][b] = c
        dist[b][a] = c
    for i in range(n):
        dist[i][i] = 0
    next = [[j for j in range(n)] for i in range(n)]
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if dist[i][k] + dist[k][j] < dist[i][j]:
                    dist[i][j] = dist[i][k] + dist[k][j]
                    next[i][j] = next[i][k]

    q = II()
    for _ in range(q):
        s, t = LI_()
        tank = l
        ans = 0
        if dist[s][t] == inf:
            print(-1)
            continue
        while s != t:
            cur = next[s][t]
            if dist[s][cur] > tank:
                tank = l
                ans += 1
            tank -= dist[s][cur]
            s = cur
        print(ans)
    return


#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    E()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    return

#B
def B():
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    n, m, l = LI()
    dist = [[inf]*n for i in range(n)]
    for _ in range(m):
        a, b, c = LI_()
        c += 1
        if c > l:
            continue
        dist[a][b] = c
        dist[b][a] = c
    next = [[j for j in range(n)] for i in range(n)]
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if dist[i][k] + dist[k][j] < dist[i][j]:
                    dist[i][j] = dist[i][k] + dist[k][j]
                    next[i][j] = next[i][k]
    q = II()
    for _ in range(q):
        s, t = LI_()
        cur = s
        tank = l
        ans = 0
        if dist[s][t] == inf:
            print(-1)
            continue
        while cur != t:
            cur1 = next[cur][t]
            if dist[cur][cur1] > tank:
                tank = l
                ans += 1
            tank -= dist[cur][cur1]
            cur = cur1
        print(ans)
    return


#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    E()

Submission status: Wrong Answer

Solution code:
from heapq import heapify, heappop, heappush, nlargest, nsmallest


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

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

nei_graph = [[float('inf')] * N for _ in range(N)]
for a, b, c in G:
    # L以下の辺ははらない
    if c <= L:
        nei_graph[a - 1][b - 1] = c
        nei_graph[b - 1][a - 1] = c

for a, b, c in G:
    nei_graph[a - 1][a - 1] = 0


def dijkstra(start_node, end_node, nei_graph):
    visited = set()

    nodes = []
    heapify(nodes)
    heappush(nodes, [start_node, 0, L, 0])

    dist = [float('inf')] * N
    dist[start_node] = 0

    while nodes:
        node, d, remain, cnt = heappop(nodes)
        if node in visited:
            continue

        # end_nodeまでの最小距離が確定したらreturn
        if node == end_node:
            return cnt

        visited.add(node)

        for i, edge in enumerate(nei_graph[node]):
            if edge == float('inf') or d + edge > dist[i]:
                continue

            dist[i] = d + edge

            if remain < edge:
                new_remain = L - edge
                new_cnt = cnt + 1
            else:
                new_remain = remain - edge
                new_cnt = cnt

            heappush(nodes, [i, dist[i], new_remain, new_cnt])

    if end_node not in visited:
        return -1


for start, end in Query:
    print(dijkstra(start - 1, end - 1, nei_graph))

Submission status: Wrong Answer

Solution code:
from heapq import heapify, heappop, heappush, nlargest, nsmallest


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

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

nei_graph = [[float('inf')] * N for _ in range(N)]
for a, b, c in G:
    # L以下の辺ははらない
    if c <= L:
        nei_graph[a - 1][b - 1] = c
        nei_graph[b - 1][a - 1] = c

for a, b, c in G:
    nei_graph[a - 1][a - 1] = 0


def dijkstra(start_node, end_node, nei_graph):
    visited = set()

    nodes = []
    heapify(nodes)
    heappush(nodes, [start_node, 0, L, 0])

    dist = [float('inf')] * N
    dist[start_node] = 0

    while nodes:
        node, d, remain, cnt = heappop(nodes)
        if node in visited:
            continue

        # end_nodeまでの最小距離が確定したらreturn
        if node == end_node:
            return cnt

        visited.add(node)

        for i, edge in enumerate(nei_graph[node]):
            if edge == float('inf') or d + edge > dist[i]:
                continue

            dist[i] = d + edge

            if remain < edge:
                new_remain = L - edge
                new_cnt = cnt + 1
            else:
                new_remain = remain - edge
                new_cnt = cnt

            heappush(nodes, [i, dist[i], new_remain, new_cnt])

    if end_node not in visited:
        return -1

Submission status: Wrong Answer

Solution code:
from heapq import heapify, heappop, heappush, nlargest, nsmallest


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

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

nei_graph = [[float('inf')] * N for _ in range(N)]
for a, b, c in G:
    if c <= L:
        nei_graph[a - 1][b - 1] = c
        nei_graph[b - 1][a - 1] = c

for a, b, c in G:
    nei_graph[a - 1][a - 1] = 0


def dijkstra(start_node, end_node, nei_graph):
    visited = set()

    nodes = []
    heapify(nodes)
    heappush(nodes, [start_node, 0, L, 0])

    dist = [float('inf')] * N
    dist[start_node] = 0

    while nodes:
        node, d, remain, cnt = heappop(nodes)
        if node in visited:
            continue

        if node == end_node:
            return cnt

        visited.add(node)

        for i, edge in enumerate(nei_graph[node]):
            if edge == float('inf') or d + edge >= dist[i]:
                continue

            dist[i] = d + edge

            if remain < edge:
                new_remain = L - edge
                new_cnt = cnt + 1
            else:
                new_remain = remain - edge
                new_cnt = cnt

            heappush(nodes, [i, dist[i], new_remain, new_cnt])

    if end_node not in visited:
        return -1


for start, end in Query:
    print(dijkstra(start - 1, end - 1, nei_graph))

Submission status: Wrong Answer

Solution code:
from heapq import heapify, heappop, heappush, nlargest, nsmallest


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

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

nei_graph = [[float('inf')] * N for _ in range(N)]
for a, b, c in G:
    if c <= L:
        nei_graph[a - 1][b - 1] = c
        nei_graph[b - 1][a - 1] = c

for a, b, c in G:
    nei_graph[a - 1][a - 1] = 0


def dijkstra(start_node, end_node, nei_graph):
    visited = set()

    nodes = []
    heapify(nodes)
    heappush(nodes, [start_node, 0, L, 0])

    dist = [float('inf')] * N
    dist[start_node] = 0

    while nodes:
        node, d, remain, cnt = heappop(nodes)
        if node in visited:
            continue

        if node == end_node:
            return cnt

        visited.add(node)

        for i, edge in enumerate(nei_graph[node]):
            if edge == float('inf') or d + edge >= dist[i]:
                continue

            dist[i] = d + edge

            if remain < edge:
                new_remain = L - edge
                new_cnt = cnt + 1
            else:
                new_remain = remain - edge
                new_cnt = cnt

            heappush(nodes, [i, dist[i], new_remain, new_cnt])

        if end_node not in visited:
            return -1


for start, end in Query:
    print(dijkstra(start - 1, end - 1, nei_graph))

Submission status: Wrong Answer

Solution code:
import heapq

INF = float("inf")

def s_in():
    return input()

def n_in():
    return int(input())

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

def warshall_floyd(n, 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])

N, M, L = l_in()
first_matrix = [[INF for _ in range(N)] for _ in range(N)]

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

for _ in range(M):
    A, B, C = l_in()
    first_matrix[A-1][B-1] = C
    first_matrix[B-1][A-1] = C

warshall_floyd(N, first_matrix)
second_matrix = [[INF for _ in range(N)] for _ in range(N)]

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

warshall_floyd(N, second_matrix)
Q = n_in()

for _ in range(Q):
    s, t = l_in()
    v = second_matrix[s-1][t-1]
    if second_matrix == INF:
        print(-1)
    else:
        print(v-1)

Submission status: Wrong Answer

Solution code:
import heapq

INF = float("inf")

def s_in():
    return input()

def n_in():
    return int(input())

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


N, M, L = l_in()
edges = [[] for _ in range(N)]

for i in range(M):
    A, B, C = l_in()
    edges[A-1].append((C, B-1))
    edges[B-1].append((C, A-1))


new_graph = []

for i in range(N):
    q = [(0,i)]
    visited = set()
    while len(q) != 0:
        c, j = heapq.heappop(q)
        visited.add(j)
        for c2, j2 in edges[j]:
            if c + c2 < L and j2 in visited:
                heapq.heappush(q, (c+c2, j2))

    new_graph.append(visited)

# print(new_graph)

res = []

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

    tmp = [2*N for _ in range(N)]
    tmp[i] = 0

    while len(q) != 0:
        c, j = heapq.heappop(q)

        for j2 in new_graph[j]:
            if c + 1 < tmp[j2]:
                tmp[j2] = c + 1
                heapq.heappush(q, (c+1, j2))
    res.append(tmp)

# print(res)

Q = n_in()

for _ in range(Q):
    s, t = l_in()
    v = res[s-1][t-1]
    if v == 2*N:
        print(-1)
    else:
        print(v-1)

Submission status: Wrong Answer

Solution code:


class Graph(object):
  def __init__(self, N):
    self.N = N
    self.adj = None
    self.init_adj()
  
  def init_adj(self):
    self.adj = [[float("INF") for _ in range(self.N)] for _  in range(self.N)]
    for i in range(self.N):
      self.adj[i][i] = 0

  def add_edge(self, u, v, w):
    self.adj[u-1][v-1] = w
    self.adj[v-1][u-1] = w

  def print_adj(self):
    for i in range(self.N):
      print(self.adj[i])

  def floyd_warshall(self):
    for k in range(self.N):
      for i in range(self.N):
        for j in range(self.N):
          self.adj[i][j] = min(self.adj[i][j], self.adj[i][k] + self.adj[k][j])




if __name__ == "__main__":
  N, M, L = map(int, input().split())
  queries = []
  graph = Graph(N)
  for i in range(M):
    u, v, w = list(map(int, input().split()))
    graph.add_edge(u, v, w)
  Q = int(input())
  for i in range(Q):
    s, t = list(map(int, input().split()))
    queries.append([s, t])
  
  # print("queries:", queries)
  # print("-----")
  graph.floyd_warshall()
  # print("adj:")
  # graph.print_adj()
  for s, t in queries:
    min_dist = graph.adj[s-1][t-1]
    # print(f"from: {s} to: {t}, min dist = {min_dist}")
    if min_dist == float("INF"):
      print(-1)
    else:
      print((min_dist-1)//L)

Submission status: Wrong Answer

Solution code:


class Graph(object):
  def __init__(self, N):
    self.N = N
    self.adj = None
    self.init_adj()
  
  def init_adj(self):
    self.adj = [[float("INF") for _ in range(self.N)] for _  in range(self.N)]
    for i in range(self.N):
      self.adj[i][i] = 0

  def add_edge(self, u, v, w):
    self.adj[u-1][v-1] = w
    self.adj[v-1][u-1] = w

  def print_adj(self):
    for i in range(self.N):
      print(self.adj[i])

  def floyd_warshall(self):
    for k in range(self.N):
      for i in range(self.N):
        for j in range(self.N):
          self.adj[i][j] = min(self.adj[i][j], self.adj[i][k] + self.adj[k][j])




if __name__ == "__main__":
  N, M, L = map(int, input().split())
  queries = []
  graph = Graph(N)
  for i in range(M):
    u, v, w = list(map(int, input().split()))
    graph.add_edge(u, v, w)
  Q = int(input())
  for i in range(Q):
    s, t = list(map(int, input().split()))
    queries.append([s, t])
  
  print("queries:", queries)
  print("-----")
  graph.floyd_warshall()
  print("adj:")
  graph.print_adj()
  for s, t in queries:
    min_dist = graph.adj[s-1][t-1]
    # print(f"from: {s} to: {t}, min dist = {min_dist}")
    if min_dist == float("INF"):
      print(-1)
    else:
      print((min_dist-1)//L)