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,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = 10**18
    MX = 10**9
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

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

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

    for s in range(N):

        q = [(0,s)]
        town[s][s] = 0
        heapq.heapify(q)
        while q:
            z,u = heapq.heappop(q)
            if town[s][u] < z: continue
            n = z // MX
            w = z % MX
            for v,c in road[u]:
                nn = n
                nw = w + c
                if nw > L:
                    nw = c
                    nn += 1

                nz = nn * MX + nw
                if town[s][v] > nz:
                    town[s][v] = nz
                    heapq.heappush(q,(nz,v))

    Q = NI()
    for _ in range(Q):
        s,t = LI()
        if town[s-1][t-1] == INF:
            print(-1)
        else:
            print(town[s-1][t-1]//MX)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = float('inf')
    MX = 10**9
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

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

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

    for s in range(N):

        q = [(0,s)]
        town[s][s] = 0
        heapq.heapify(q)
        while q:
            z,u = heapq.heappop(q)
            if town[s][u] < z: continue
            n = z // MX
            w = z % MX
            for v,c in road[u]:
                nn = n
                nw = w + c
                if nw > L:
                    nw = c
                    nn += 1

                nz = nn * MX + nw
                if town[s][v] > nz:
                    town[s][v] = nz
                    heapq.heappush(q,(nz,v))

    Q = NI()
    for _ in range(Q):
        s,t = LI()
        if town[s-1][t-1] == INF:
            print(-1)
        else:
            print(town[s-1][t-1]//MX)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = float('inf')
    MX = 10**9
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

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

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

    for s in range(N):

        q = [(0,s)]
        heapq.heapify(q)
        while q:
            z,u = heapq.heappop(q)
            n = z // MX
            w = z % MX
            for v,c in road[u]:
                nn = n
                nw = w + c
                if nw > L:
                    nw = c
                    nn += 1

                if town[s][v] > nn * MX + nw:
                    town[s][v] = nn * MX + nw
                    heapq.heappush(q,(nn*MX + nw,v))

    Q = NI()
    for _ in range(Q):
        s,t = LI()
        if town[s-1][t-1] == INF:
            print(-1)
        else:
            print(town[s-1][t-1]//MX)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = float('inf')
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

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

    town = [[-1] * N for _ in range(N)]

    for s in range(N):

        q = [(0,-L,s)]
        heapq.heapify(q)
        while q:
            n,w,u = heapq.heappop(q)
            for v,c in road[u]:
                nn = n
                nw = w + c
                if nw > 0:
                    nw = c - L
                    nn += 1

                if town[s][v] < 0:
                    town[s][v] = nn
                    heapq.heappush(q,(nn,nw,v))

    Q = NI()
    for _ in range(Q):
        s,t = LI()
        print(town[s-1][t-1])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = float('inf')
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

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

    q = NI()
    for _ in range(q):
        s,t = LI()
        town = [[INF,INF] for _ in range(N)]

        q = [(0,L,s-1)]
        heapq.heapify(q)

        while q:
            n,w,u = heapq.heappop(q)
            if u == t-1:
                print(n)
                break
            for v,c in road[u]:
                nn = n
                nw = w
                if c > w:
                    nn += 1
                    nw += L
                nw -= c
                if town[v][0] > n or town[v][0] == n and town[v][1] > w:
                    town[v] = nn,nw
                    heapq.heappush(q,(nn,nw,v))
        else:
            print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def dijkstra(s,t,n,w,cost):
    prev = [s] * n 
    d = [INF] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        for i in range(n):
            if (not used[i]) and (v == -1):
               v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
               break
        used[v] = True
        
        for i in range(n):
            if d[i] > d[v] + cost[v][i]: 
                d[i] = d[v] + cost[v][i]
                prev[i] = v
        
    path = [t]
    while prev[t] != s:
        path.append(prev[t])
        prev[t] = prev[prev[t]]
    path.append(s)
    path = path[::-1]
    return path




def main():
    N,M,L=mi()

    dp = [[INF]*(N+1) for _ in range(N+1)]
    cost_map = [[INF]*(N+1) for _ in range(N+1)]
    for i in range(M):
        a,b,c = mi()
        if c > L: continue

        dp[a][b] = min(dp[a][b],c)
        dp[b][a] = min(dp[b][a],c)

        cost_map[a][b] = min(cost_map[a][b],c)
        cost_map[b][a] = min(cost_map[b][a],c)



    for i in range(N+1):
        dp[i][i] = 0
    
    for i in range(1,N+1):
        for j in range(1,N+1):
            for k in range(1,N+1):
                pre = dp[i][j]
                dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j])
                if dp[i][j] < pre:
                    cost_map[i][j] = INF
                    cost_map[j][i] = INF


                

    
    Q = ii()
    for q in range(Q):
        s,t = mi()
        cost = dp[s][t]
        if cost == INF:
            print(-1)
            continue

        path = dijkstra(s,t,N+1,M,cost_map)
    
        tank = L
        ans = 0
        pre = s
        for v in path:
            if cost_map[pre][v] == INF: continue

            if tank < cost_map[pre][v]:
                tank = L
                ans += 1

            tank -= cost_map[pre][v]
            pre = v
        
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def dijkstra(s,t,n,w,cost):
    prev = [s] * n 
    d = [INF] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        for i in range(n):
            if (not used[i]) and (v == -1):
               v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
               break
        used[v] = True
        
        for i in range(n):
            if d[i] > d[v] + cost[v][i]: 
                d[i] = d[v] + cost[v][i]
                prev[i] = v
        
    path = [t]
    while prev[t] != s:
        path.append(prev[t])
        prev[t] = prev[prev[t]]
    path.append(s)
    path = path[::-1]
    return path




def main():
    N,M,L=mi()

    cost_map = [[INF]*(N+1) for _ in range(N+1)]
    for i in range(M):
        a,b,c = mi()
        if c > L: continue

        cost_map[a][b] = min(cost_map[a][b],c)
        cost_map[b][a] = min(cost_map[b][a],c)



    
    Q = ii()
    memo = {}
    for q in range(Q):
        s,t = mi()
        if (s,t) in memo:
            print(memo[(s,t)])
            continue

        path = dijkstra(s,t,N+1,M,cost_map)
        tank = L
        ans = 0
        pre = s

        # print(path)
        has_no_path = False
        for v in path[1:]:
            if cost_map[pre][v] == INF:
                has_no_path = True
                break

            if tank < cost_map[pre][v]:
                tank = L
                ans += 1

            tank -= cost_map[pre][v]
            pre = v

        if has_no_path:
            ans = -1

        memo[(s,t)] = ans
        memo[(t,s)] = ans
        print(ans)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def dijkstra(s,t,n,w,cost):
    prev = [s] * n 
    d = [INF] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        for i in range(n):
            if (not used[i]) and (v == -1):
               v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
               break
        used[v] = True
        
        for i in range(n):
            if d[i] > d[v] + cost[v][i]: 
                d[i] = d[v] + cost[v][i]
                prev[i] = v
        
    path = [t]
    while prev[t] != s:
        path.append(prev[t])
        prev[t] = prev[prev[t]]
    path.append(s)
    path = path[::-1]
    return path




def main():
    N,M,L=mi()

    dp = [[INF]*(N+1) for _ in range(N+1)]
    cost_map = [[INF]*(N+1) for _ in range(N+1)]
    for i in range(M):
        a,b,c = mi()
        if c > L: continue

        dp[a][b] = min(dp[a][b],c)
        dp[b][a] = min(dp[b][a],c)

        cost_map[a][b] = min(cost_map[a][b],c)
        cost_map[b][a] = min(cost_map[b][a],c)



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


                

    
    Q = ii()
    for q in range(Q):
        s,t = mi()
        cost = dp[s][t]
        if cost == INF:
            print(-1)
            continue

        path = dijkstra(s,t,N+1,M,cost_map)
    
        tank = L
        ans = 0
        pre = s
        for v in path:
            if cost_map[pre][v] == INF: continue

            if tank < cost_map[pre][v]:
                tank = L
                ans += 1

            tank -= cost_map[pre][v]
            pre = v
        
        print(ans)




    


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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

if M == 0:
    Q = int(input())
    for _ in range(Q):
        print(-1)
    exit()

R = np.array([tuple(map(int, input().split())) for _ in range(M)])
matr = csr_matrix((R[:, 2], (R[:, 0] - 1, R[:, 1] - 1)), shape=(N, N))
way = floyd_warshall(matr, directed=False).astype(int)

way = (way <= L).astype(int)
matr_replenishment = csr_matrix(way)
way_replenishment = floyd_warshall(matr_replenishment, directed=False).astype(int).tolist()

Q = int(input())
for _ in range(Q):
    S, T = map(int, input().split())
    print(res - 1 if (res := way_replenishment[S - 1][T - 1]) >= 0 else -1)

Submission status: Wrong Answer

Solution code:
import numpy as np
from scipy.sparse.csgraph import bellman_ford


def solve(N,L,edges,queries):

    graph = np.zeros((N,N),dtype=int)
    for a,b,c in edges:
        graph[a-1,b-1] = c
        graph[b-1,a-1] = c

    print('test')
    exit()
    dist = bellman_ford(graph, directed=False)
    dist = (dist<=L)
    dist = bellman_ford(dist, directed=False)
    res = []
    for a,b in queries:
        d = dist[a-1,b-1]
        res.append(int(d-1) if d != float('inf') else -1)
    return res

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

if __name__ == '__main__':

    t = read().split()

    N,M,L = map(int,t[:3])

    m = map(int,t[3:M*3+3])
    edges = zip(m,m,m)

    Q = int(t[M*3+3])
    m = map(int, t[M*3+4:])
    queries = zip(m,m)

    print(*solve(N,L,edges,queries), sep='\n')

Submission status: Wrong Answer

Solution code:
import numpy as np
from scipy.sparse.csgraph import bellman_ford


def solve(N,L,edges,queries):

    graph = np.zeros((N,N),dtype=int)
    for a,b,c in edges:
        graph[a-1,b-1] = c
        graph[b-1,a-1] = c

    dist = bellman_ford(graph, directed=False)
    dist = (dist<=L)
    print('test')
    exit()
    dist = bellman_ford(dist, directed=False)
    res = []
    for a,b in queries:
        d = dist[a-1,b-1]
        res.append(int(d-1) if d != float('inf') else -1)
    return res

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

if __name__ == '__main__':

    t = read().split()

    N,M,L = map(int,t[:3])

    m = map(int,t[3:M*3+3])
    edges = zip(m,m,m)

    Q = int(t[M*3+3])
    m = map(int, t[M*3+4:])
    queries = zip(m,m)

    print(*solve(N,L,edges,queries), sep='\n')

Submission status: Wrong Answer

Solution code:

N, M, L = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(M)]
Q = int(input())
Y = [list(map(int, input().split())) for _ in range(Q)]

INF = 10 ** 9 + 7
graph = [[INF] * N for _ in range(N)]
count = [[INF] * N for _ in range(N)]
for i in range(N):
    graph[i][i] = 0
    count[i][i] = 0

for a, b, c in X:
    graph[a - 1][b - 1] = c
    graph[b - 1][a - 1] = c

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

for i in range(N):
    for j in range(N):
        if i != j and graph[i][j] <= L:
            count[i][j] = 1

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

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

Submission status: Wrong Answer

Solution code:

N, M, L = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(M)]
Q = int(input())
Y = [list(map(int, input().split())) for _ in range(Q)]

INF = 10 ** 9 + 7
graph = [[INF] * N for _ in range(N)]
count = [[INF] * N for _ in range(N)]
for i in range(N):
    graph[i][i] = 0
    count[i][i] = 0

for a, b, c in X:
    graph[a - 1][b - 1] = c
    graph[b - 1][a - 1] = c

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

for i in range(N):
    for j in range(N):
        if i != j and graph[i][j] <= L:
            count[i][j] = 1

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

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

Submission status: Wrong Answer

Solution code:
class Graph:
    class Edge:
        def __init__(self, to, cost):
            """
            :param to:  終点ノード
            :param cost: 辺の重み
            """
            self.to, self.cost = to, cost

    def __init__(self, n, directed=False, decrement=True, edges=[]):
        self.n = n
        self.directed = directed
        self.decrement = decrement
        self.edges = [[] for _ in range(self.n)]
        self.edges2 = []                            # ワーシャルフロイド用
        self.parent = [-1]*self.n
        self.info = [-1]*self.n
        for x, y, cost in edges:
            self.add_edge(x, y, cost)

    def add_edge(self, x, y, cost):
        if self.decrement:
            x -= 1
            y -= 1
        self.edges[x].append(self.Edge(y, cost))
        self.edges2.append((x, y, cost))
        if self.directed == False:
            self.edges[y].append(self.Edge(x, cost))
            self.edges2.append((y, x, cost))     # ワーシャルフロイド用

    def warshall_folyd(self, INF=float("inf")):
        """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """
        dist = [[INF] * self.n for _ in range(self.n)]

        for i, j, cost in self.edges2:
            dist[i][j] = cost
        for i in range(self.n):
            dist[i][i] = 0  # 自身のところに行くコストは０

        for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
        return dist

    def warshall_folyd2(self, INF=float("inf")):
        """
        :param INF: INF = 10**18 にすると、負のコストがある場合に少し面倒になる
        :return: 頂点 i, j 間の距離を行列として返す
        """
        dist = [[INF] * self.n for _ in range(self.n)]

        for i, j, cost in self.edges2:
            dist[i][j] = cost
            if dist[i][j] <= L:
                data.append((i, j))
                data.append((j, i))
        for i in range(self.n):
            dist[i][i] = 0

        for k in range(self.n):
            for i in range(self.n):
                for j in range(self.n):
                    if dist[i][j] > dist[i][k] + dist[k][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
                        if dist[i][j] <= L:
                            data.append((i, j))
                            data.append((j, i))
        return dist

def max2(x,y):
    return x if x > y else y

N, M, L = map(int, input().split())
graph = Graph(N, directed=True, decrement=True)
for _ in range(M):
    x, y, cost = map(int, input().split())
    graph.add_edge(x, y, cost)

data = []
graph.warshall_folyd2()

graph2 = Graph(N, directed=True, decrement=False)
for x, y in data:
    graph2.add_edge(x, y, 1)

d = graph2.warshall_folyd()

for _ in range(int(input())):
    s, t, = map(int, input().split())
    s, t = s-1, t-1
    print(d[s][t]-1 if d[s][t] != float("inf") else -1)

Submission status: Wrong Answer

Solution code:
n,m,l=map(int,input().split())
abc=[list(map(int,input().split())) for _ in range(m)]

"""
g=[[] for _ in range(n)]
for a,b,c in abc:
  if c>l:continue
  a,b=a-1,b-1
  g[a].append([b,c])
  g[b].append([a,c])
"""
q=int(input())
st=[list(map(int,input().split())) for _ in range(q)]

def warshall_floyd(d):
  for i in range(n):
    for j in range(n):
      for k in range(n):
        if d[i][j]>d[i][k]+d[k][j]:
          d[i][j]=d[i][k]+d[k][j]
  return d
inf=float('inf')
d=[[inf]*n for _ in range(n)]

for a,b,c in abc:
  a,b=a-1,b-1
  d[a][b]=c
  d[b][a]=c
for i in range(n):
  d[i][i]=0
d=warshall_floyd(d)
nabc=[]
dd=[[inf]*n for _ in range(n)]
#g=[[] for _ in range(n)]
for i in range(n):
  for j in range(n):
    if d[i][j]<=l:
      dd[i][j]=1
for i in range(n):
  dd[i][i]=0
dd=warshall_floyd(dd)
for s,t in st:
  s,t=s-1,t-1
  tmp=dd[s][t]
  print(max(0,tmp-1) if tmp!=inf else -1)
#print(dd)

Submission status: Wrong Answer

Solution code:
n,m,l=map(int,input().split())
abc=[list(map(int,input().split())) for _ in range(m)]

"""
g=[[] for _ in range(n)]
for a,b,c in abc:
  if c>l:continue
  a,b=a-1,b-1
  g[a].append([b,c])
  g[b].append([a,c])
"""
q=int(input())
st=[list(map(int,input().split())) for _ in range(q)]

def warshall_floyd(d):
  for i in range(n):
    for j in range(n):
      for k in range(n):
        if d[i][j]>d[i][k]+d[k][j]:
          d[i][j]=d[i][k]+d[k][j]
  return d
inf=float('inf')
d=[[inf]*n for _ in range(n)]

for a,b,c in abc:
  a,b=a-1,b-1
  d[a][b]=c
  d[b][a]=c
for i in range(n):
  d[i][i]=0
d=warshall_floyd(d)
nabc=[]
dd=[[inf]*n for _ in range(n)]
#g=[[] for _ in range(n)]
for i in range(n):
  for j in range(n):
    if d[i][j]<=l:
      dd[i][j]=1
dd=warshall_floyd(dd)
for s,t in st:
  s,t=s-1,t-1
  tmp=dd[s][t]
  print(tmp-1 if tmp!= inf else -1)

Submission status: Wrong Answer

Solution code:
n,m,l=map(int,input().split())
inf=100000000000000000000
wf=[[inf]*n for i in range(n)]
for i in range(m):
    a,b,c=map(int,input().split())
    wf[a-1][b-1]=c
    wf[b-1][a-1]=c
for i in range(n):
    wf[i][i]=0
for i in range(n):
    for j in range(n):
        for k in range(n):
            wf[i][j]=min(wf[i][j],wf[i][k]+wf[k][j])
for i in range(n):
    for j in range(n):
        if i!=j:
            wf[i][j]=1 if wf[i][j]<=l else inf
for i in range(n):
    for j in range(n):
        for k in range(n):
            wf[i][j]=min(wf[i][j],wf[i][k]+wf[k][j])
q=int(input())
for i in range(q):
    s,t=map(int,input().split())
    x=wf[s-1][t-1]
    print(-1 if x==inf else wf[s-1][t-1]-1)

Submission status: Wrong Answer

Solution code:
n,m,l=map(int,input().split())
inf=100000000000
wf=[[inf]*n for i in range(n)]
for i in range(m):
    a,b,c=map(int,input().split())
    wf[a-1][b-1]=c
    wf[b-1][a-1]=c
for i in range(n):
    wf[i][i]=0
for i in range(n):
    for j in range(n):
        for k in range(n):
            wf[i][j]=min(wf[i][j],wf[i][k]+wf[k][j])
for i in range(n):
    for j in range(n):
        if i!=j:
            wf[i][j]=1 if wf[i][j]<=l else inf
for i in range(n):
    for j in range(n):
        for k in range(n):
            wf[i][j]=min(wf[i][j],wf[i][k]+wf[k][j])
q=int(input())
for i in range(q):
    s,t=map(int,input().split())
    x=wf[s-1][t-1]
    print(-1 if x==inf else wf[s-1][t-1]-1)

Submission status: Wrong Answer

Solution code:
import heapq

def mydijkstra_heap(graph, s, g):
    #始点sから各頂点への最短距離
    d = [INF] * n
    d[s] = 0
    edgelist = []
    cnt = [[0, 0] for i in range(n)]
    cnt[s] = [l, 0]
    for e in graph[s]:
        heapq.heappush(edgelist, e + [e[0]] + cnt[s])
    while len(edgelist):
        me, cv, ce, cf, cc = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if me > d[cv]:
            continue
        d[cv] = me
        cnt[cv] = [cf - ce, cc]
        if cnt[cv][0] < 0:
            cnt[cv][0] = l - ce
            cnt[cv][1] += 1
        for e in graph[cv]:
            if d[e[1]] == INF:
                heapq.heappush(edgelist,[e[0]+d[cv], e[1], e[0], cnt[cv][0], cnt[cv][1]])
    return d, cnt

INF = 10**9
n, m, l = map(int,input().split())

graph = [[] for _ in range(n)]
for i in range(m):
    u, v, w = map(int,input().split())
    u -= 1
    v -= 1
    if w > l:
        continue
    graph[u].append([w,v])
    graph[v].append([w,u])
    
q = int(input())
st = [list(map(int,input().split())) for i in range(q)]

d = [[] for _ in range(n)]
cnt = [[] for _ in range(n)]
for i in range(q):
    s, t = st[i][0] - 1, st[i][1] - 1
    if not d[s]:
        d[s], cnt[s] = mydijkstra_heap(graph, s, t)
    if d[s][t] == INF:
        print(-1)
    else:
        print(cnt[s][t][1])

Submission status: Wrong Answer

Solution code:
n,m,l=map(int,input().split())
abc=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n)]
for a,b,c in abc:
  if c>l:continue
  a,b=a-1,b-1
  g[a].append([b,c])
  g[b].append([a,c])
q=int(input())
st=[list(map(int,input().split())) for _ in range(q)]

def warshall_floyd(d):
  #d[i][j]: iからjへの最短距離
  for i in range(n):
    for j in range(n):
      for k in range(n):# i->k->j
        a,b=d[i][j]
        na=d[i][k][0]+d[k][j][0]
        na+=0 if l-d[i][k][1]>=d[k][j][1] else 1
        nb=d[i][k][1]+d[k][j][1] if l-d[i][k][1]>=d[k][j][1] else d[i][k][1]
        #if i==4 and j==1:
        #  print('k,a,b,na,nb:',k,a,b,na,nb)
        if a>na:
          d[i][j]=[na,nb]
        elif a==na and b>nb:
          d[i][j]=[na,nb]
  return d
inf=float('inf')
d=[[[inf]*2 for _ in range(n)] for _ in range(n)]
# d[i][j]:i->jの情報。[a,b] 残りbあれば補給a回でいける。なければa+1回。
for a,b,c in abc:
  a,b=a-1,b-1
  d[a][b]=[0,c]
  d[b][a]=[0,c]
for i in range(n):
  d[i][i]=[0,0]
d=warshall_floyd(d)
for s,t in st:
  s,t=s-1,t-1
  tmp=d[s][t][0]
  print(tmp if tmp!= inf else -1)
#for di in d:
#  print(di)

Submission status: Wrong Answer

Solution code:
from scipy.sparse.csgraph import csgraph_from_dense, shortest_path


def main():
    import heapq
    import sys
    input = sys.stdin.buffer.readline
    N, M, L = map(int, input().split())

    G = [[-1]*N for i in range(N)]

    for i in range(M):
        A, B, C = map(int, input().split())
        A -= 1
        B -= 1
        G[A][B] = C
        G[B][A] = C
    g = csgraph_from_dense(G, null_value=-1)
    D = shortest_path(g)
    print(D)
    G2 = [[-1]*N for i in range(N)]
    for i in range(N):
        for j in range(N):
            if D[i][j] >= 0 and D[i][j] <= L:
                G2[i][j] = 1

    g2 = csgraph_from_dense(G2, null_value=-1)
    D = shortest_path(g2)

    print(D)

    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        if D[s][t] <= N**2:
            print(int(D[s][t])-1)
        else:
            print(-1)


main()

Submission status: Wrong Answer

Solution code:
#import numpy as np
from scipy.sparse.csgraph import floyd_warshall, csgraph_from_dense
from scipy.sparse import csr_matrix

N,M,L = map(int, input().split())
V = [[ 10**9 for _ in range(N) ] for _ in range(N)]
for _ in range(M):
  a,b,c = map(int, input().split())
  a,b = a-1, b-1
  V[a][b] = c
  V[b][a] = c
  
G = csgraph_from_dense(V, null_value=10**9)
d = floyd_warshall(G)

V2 = [[ 10**9 for _ in range(N) ] for _ in range(N)]
for i in range(N):
  for j in range(N):
    if 0 < d[i][j] <= L:
      V2[i][j] = 1
      
G2 = csgraph_from_dense(V2, null_value=10**9)
d2 = floyd_warshall(G2)

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