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:
from scipy.sparse import*
def main():
  f=csgraph.floyd_warshall
  n,m,l,*t=map(int,open(0).read().split())
  d=[[0]*(n+1)for _ in range(n+1)]
  for a,b,c in zip(*[iter(t[:m*3])]*3):d[a][b]=c
  d=f(f(d,0)<=l)-1
  d[d>n]=-1
  print(' '.join(str(d[i])for i in zip(*[iter(t[m*3+1:])]*2)))
main()

Submission status: Wrong Answer

Solution code:
def e_travel_by_car(INF=float('inf')):
    from copy import deepcopy
    from scipy.sparse.csgraph import floyd_warshall
    N, M, L = [int(i) for i in input().split()]
    Roads = [[int(i) for i in input().split()] for j in range(M)]
    Q = int(input())
    Queries = [[int(i) for i in input().split()] for j in range(Q)]

    graph = [[INF] * i + [0] + [INF] * (N - i - 1) for i in range(N)]
    fuel = deepcopy(graph)

    for a, b, c in Roads:
        graph[a - 1][b - 1] = graph[b - 1][a - 1] = c
    dist = floyd_warshall(graph)

    for row in range(N):
        for col in range(row):
            fuel[row][col] = fuel[col][row] = int(dist[row][col] <= L)
    fuel = floyd_warshall(fuel)

    ans = [-1 if fuel[s - 1][t - 1] == INF else int(fuel[s - 1][t - 1]) - 1
           for s, t in Queries]
    return ' '.join(map(str, ans))

print(e_travel_by_car())

Submission status: Wrong Answer

Solution code:
import heapq
inf = 10**11
def dijkstra(v0):
    d = [(inf, inf)] * N
    d[v0] = (0, L)
    edgelist = []
    for v, c in G[v0]:
        if c<=L:
            heapq.heappush(edgelist,(0, -(L-c), v))
    while len(edgelist):
        print(edgelist)
        c, r, v = heapq.heappop(edgelist)
        r = -r
        if d[v][0] != inf:
            continue
        d[v] = (c, r)
        for nv, nr in G[v]:
            if d[nv][0] == inf:
                if nr<=r:
                    heapq.heappush(edgelist,(c, -(r-nr), nv))
                elif nr<L:
                    heapq.heappush(edgelist,(c+1,-(L-nr), nv))
    return d
N, M, L = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    G[a-1].append((b-1, c))
    G[b-1].append((a-1, c))
ans = []
for i in range(N):
    ans.append(dijkstra(i))
Q = int(input())
for _ in range(Q):
    a, b = map(int, input().split())
    res = ans[a-1][b-1][0]
    print(res if res<10**10 else -1)

Submission status: Wrong Answer

Solution code:
import heapq
inf = 10**10
def dijkstra(v0):
    d = [(inf, inf)] * N
    used = [False] * N
    d[v0] = (0, L)
    used[v0] = True
    edgelist = []
    for v, c in G[v0]:
        if c<=L:
            heapq.heappush(edgelist,(0, -(L-c), v))
    while len(edgelist):
        c, r, v = heapq.heappop(edgelist)
        r = -r
        #まだ使われてない頂点の中から最小の距離のものを探す
        if used[v]:
            continue
        d[v] = (c, r)
        used[v] = True
        for nv, nr in G[v]:
            if not used[nv]:
                if nr<=r:
                    heapq.heappush(edgelist,(c, -(r-nr), nv))
                elif nr<L:
                    heapq.heappush(edgelist,(c+1,-(L-nr), nv))
    return d
N, M, L = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    G[a-1].append((b-1, c))
    G[b-1].append((a-1, c))
ans = []
for i in range(N):
    ans.append(dijkstra(i))
Q = int(input())
for _ in range(Q):
    a, b = map(int, input().split())
    print(ans[a-1][b-1][0])

Submission status: Wrong Answer

Solution code:
import numpy as np
from scipy.sparse.csgraph import csgraph_from_dense
from scipy.sparse.csgraph import dijkstra
 
import sys
input = sys.stdin.readline
def main():
  [N,M,L]=list(map(int,input().split()))
  abc=[list(map(int,input().split())) for _ in range(M)]
  Q=int(input())
  st=[list(map(int,input().split())) for _ in range(Q)]
  
  if M==0:
    for _ in range(Q):
      print(-1)
      exit()
  
  g=np.zeros((N,N))
  for road in abc:
    if road[2]<=L:
      g[road[0]-1][road[1]-1]=road[2]
      g[road[1]-1][road[0]-1]=road[2]
  
  for item in st:
    csg=csgraph_from_dense(g,null_value=0) #重み0==ノードなし，とする
    #print(item)
    (d,path) = dijkstra(csg,
                 indices = item[0]-1,
                 return_predecessors=True)
    #print(d,path)
    
    shouhi=[]
    s=item[1]-1
    while s!=-9999:
      shouhi.append(d[s])
      s=path[s]
    #shouhi=[shouhi[-i-1]-shouhi[-i] for i in range(1,len(shouhi))]
    #print(shouhi)
    if shouhi==[]:
      print(-1)
      continue
    
    ans=0
    lim=L
    for i in range(1,len(shouhi)):
      temporary_shouhi=shouhi[-i-1]-shouhi[-i]
      
      if lim<temporary_shouhi:
        lim=L
        ans=ans+1
        lim=lim-temporary_shouhi
      else:
        lim=lim-temporary_shouhi
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)
INF = 100100100
n, m, l = map(int, input().split())
rinsetsu = [[INF]*n for i in range(n)]
for i in range(m):
    a, b, c = map(int, input().split())
    rinsetsu[a-1][b-1] = c
    rinsetsu[b-1][a-1] = c
for k in range(n):
    for i in range(n):
        for j in range(n):
            rinsetsu[i][j]=min(rinsetsu[i][j],rinsetsu[i][k]+rinsetsu[j][k])
for i in range(n):
    for j in range(n):
        rinsetsu[i][j]=1 if rinsetsu[i][j]<=l else INF
for k in range(n):
    for i in range(n):
        for j in range(n):
            rinsetsu[i][j] = min(rinsetsu[i][j], rinsetsu[i][k]+rinsetsu[j][k])
q=int(input())
for i in range(q):
    s,g=map(int,input().split())
    s-=1;g-=1
    print(rinsetsu[s][g]-1 if rinsetsu[s][g]!=INF else -1)

Submission status: Wrong Answer

Solution code:
#################################
##ワーシャルフロイド法 n~3
#################################
def warshall_floyd(n,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
##############################
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)
N,M,L =LI()
s = []
t = []
dp = [[inf] * (N) for _ in range(N)]
for i in range(M):
    a, b, c = map(int, input().split())
    dp[a-1][b-1] = c
    dp[b-1][a-1] = c
Q = I()
for i in range(Q):
    a,b = LI()
    s.append(a-1)
    t.append(b-1)

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

#print(warshall_floyd(N,dp))

newdp = [[inf] * (N) for _ in range(N)]
for i in range(N):
    for j in range(i+1,N):
        if dp[i][j]<L:
            newdp[i][j] = 1
            newdp[j][i] = 1

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

#print(warshall_floyd(N,newdp))

for i in range(Q):
    ans = newdp[s[i]][t[i]]-1
    if ans!= inf:
        print(newdp[s[i]][t[i]]-1)
    else:
        print("-1")

Submission status: Wrong Answer

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

INF = 10 ** 9 + 6
graph = [[INF] * N for _ in range(N)]

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

for u, v, l in edges:
    graph[u - 1][v - 1] = l
    graph[v - 1][u - 1] = l

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

cnt = [[INF] * N for _ in range(N)]

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

for i in range(N):
    for j in range(N):
        if i == j:
            continue
        if graph[i][j] <= L:
            cnt[i][j] = 1
            cnt[j][i] = 1

for k in range(N):
    for i in range(N):
        if cnt[i][k] == INF:
            continue
        for j in range(N):
            if cnt[k][j] == INF:
                continue
            cnt[i][j] = min(cnt[i][j], cnt[i][k] + cnt[k][j])

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

def warshall_floyd(d,n,l):
    for k in range(n):
        for i in range(n):
            dist_k = d[i][k]
            for j in range(n):
                weight = d[k][j]
                alt = dist_k + weight
                r = (dist_k - 1) // l
                if (r != (alt - 1) // 1) and dist_k != 0:
                    alt = (r + 1) * l + weight
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return 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)]
path = [[float("inf") for i in range(n)] for i in range(n)]
ans_list = []
for i in abc:
  a,b,cost = i[0]-1, i[1]-1, i[2]
  if cost <= l:
    path[a][b] = cost
    path[b][a] = cost
ans_list = warshall_floyd(path,n,l)
for i in st:
  s, t = i[0] - 1, i[1] - 1
  ans = ans_list[s][t]
  print(ans)
  if ans == float("inf"):
    ans = -1
  else:
    ans = (ans - 1) // l
  print(ans)

Submission status: Wrong Answer

Solution code:
N,M,L = map(int, input().split())
INF = 10**18

# 初期化
d = [[INF]*N for i in range(N)]
for i in range(N):
    d[i][i] = 0
for i in range(M):
    a,b,c = map(int, input().split())
    a -= 1
    b -= 1
    # 多重辺の場合はMIN
    d[a][b] = c
    d[b][a] = c
# 最適化(わーしゃゆふよいど)
for k in range(N):
    for i in range(N):
        # 左右は対称だろう
        for j in range(i+1,N):
            d[i][j] = min(d[i][j], d[i][k] + d[k][j])
            d[j][i] = d[i][j]

# 初期化2
d2 = [[INF]*N for i 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] = 1
for k in range(N):
    for i in range(N):
        for j in range(i+1,N):
            d2[i][j] = min(d2[i][j], d2[i][k] + d2[k][j])
            d2[j][i] = d2[i][j]
            

#query
Q = int(input())
for i in range(Q):
    l  = list(map(int, input().split()))
    s = min(l)-1
    t = max(l)-1
    
    ans = d2[s][t]-1
    if ans == INF:
        ans = -1
    print(ans)

Submission status: Wrong Answer

Solution code:
N,M,L = map(int, input().split())
INF = float("inf")
# 初期化
d = [[INF]*N for i in range(N)]
for i in range(N):
    d[i][i] = 0
for i in range(M):
    a,b,c = map(int, input().split())
    s = min(a,b) - 1
    t = max(a,b) - 1
    # 多重辺の場合はMIN
    d[s][t] = c
    d[s][t] = c
# 最適化(わーしゃゆふよいど)
for k in range(N):
    for i in range(N):
        # 左右は対称だろう
        for j in range(i+1,N):
            d[i][j] = min(d[i][j], d[i][k] + d[k][j])

# 初期化2
d2 = [[INF]*N for i 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] = 1
for k in range(N):
    for i in range(N):
        for j in range(i+1,N):
            d2[i][j] = min(d2[i][j], d2[i][k] + d2[k][j])

#query
Q = int(input())
for i in range(Q):
    l  = list(map(int, input().split()))
    s = min(l)-1
    t = max(l)-1
    
    ans = d2[s][t]-1
    if ans == INF:
        ans = -1
    print(ans)

Submission status: Wrong Answer

Solution code:
N,M,L = map(int, input().split())
INF = float("inf")
# 初期化
d = [[INF]*N for i in range(N)]
for i in range(N):
    d[i][i] = 0
for i in range(M):
    a,b,c = map(int, input().split())
    a -= 1
    b -= 1   
    # 多重辺の場合はMIN
    d[a][b] = c
    d[b][a] = c
# 最適化(わーしゃゆふよいど)
for k in range(N):
    for i in range(N):
        # 左右は対称だろう
        for j in range(i+1,N):
            d[i][j] = min(d[i][j], d[i][k] + d[k][j])

# 初期化2
d2 = [[INF]*N for i 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] = 1
for k in range(N):
    for i in range(N):
        for j in range(i+1,N):
            d2[i][j] = min(d2[i][j], d2[i][k] + d2[k][j])

#query
Q = int(input())
for i in range(Q):
    l  = list(map(int, input().split()))
    s = min(l)-1
    t = max(l)-1
    
    ans = d2[s][t]-1
    if ans == INF:
        ans = -1
    print(ans)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n,m,l = LI()
    aa = [LI() for _ in range(m)]
    q = I()
    qa = [LI_() for _ in range(q)]

    e = collections.defaultdict(list)
    for a,b,c in aa:
        if c > l:
            continue
        a -= 1
        b -= 1
        e[a].append((b,c))
        e[b].append((a,c))

    def search(s):
        d = collections.defaultdict(lambda: (inf, inf))
        d[s] = (0,l)
        q = []
        heapq.heappush(q, ((0,l), s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True
            kk = k[0]
            kl = k[1]

            for uv, ud in e[u]:
                if v[uv]:
                    continue
                if kl - ud < 0:
                    vd = (kk+1, l-ud)
                else:
                    vd = (kk, kl-ud)
                if d[uv] > vd:
                    d[uv] = vd
                    heapq.heappush(q, (vd, uv))

        return d

    dd = {}
    for i in range(n):
        d = search(i)
        for k in range(n):
            v = d[k]
            if v[0] == inf:
                d[k] = -1
            else:
                d[k] = v[0]
        dd[i] = d

    rr = []
    for s,t in qa:
        rr.append(dd[s][t])

    return JA(rr, "\n")


print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n,m,l = LI()
    aa = [LI() for _ in range(m)]
    q = I()
    qa = [LI_() for _ in range(q)]

    e = collections.defaultdict(list)
    for a,b,c in aa:
        a -= 1
        b -= 1
        e[a].append((b,c))
        e[b].append((a,c))

    def search(s):
        d = collections.defaultdict(lambda: (inf, inf))
        d[s] = (0,l)
        q = []
        heapq.heappush(q, ((0,l), s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True
            kk = k[0]
            kl = k[1]

            for uv, ud in e[u]:
                if v[uv]:
                    continue
                if kl - ud < 0:
                    vd = (kk+1, l-ud)
                else:
                    vd = (kk, kl-ud)
                if d[uv] > vd:
                    d[uv] = vd
                    heapq.heappush(q, (vd, uv))

        return d

    dd = {}
    for i in range(n):
        d = search(i)
        for k in range(n):
            v = d[k]
            if v[0] == inf:
                d[k] = -1
            else:
                d[k] = v[0]
        dd[i] = d

    rr = []
    for s,t in qa:
        rr.append(dd[s][t])

    return JA(rr, "\n")


print(main())

Submission status: Wrong Answer

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

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


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

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

    q = int(input())

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

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

    g = csr_matrix(g)

    d, p = floyd_warshall(csgraph=g, return_predecessors=True)

    for s, t in st:
        if d[s][t] == float("inf"):
            ans = -1
        else:
            way = []
            prev = t
            while prev != s:
                way.append(p[s][prev])
                prev = p[s][prev]

            way = way[::-1]
            way.append(t)

            tank = l
            ans = 0
            for i in range(len(way) - 1):
                u, v = way[i:i+2]
                dist = d[u][v]
                if tank - dist < 0:
                    ans += 1
                    tank = l
                tank -= dist

        print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


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

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

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

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

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

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

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


main()

Submission status: Wrong Answer

Solution code:
import sys


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

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

    for i in range(n-2):
        for j in range(i+1, n-1):
            for k in range(j+1, n):
                total = dist[i][j] + dist[j][k]
                if dist[i][k] > total:
                    dist[i][k] = total
                    dist[k][i] = total

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

    for i in range(n-2):
        for j in range(i+1, n+1):
            for k in range(j+1, n):
                total = ldist[i][j] + ldist[j][k]
                if ldist[i][k] > total:
                    ldist[i][k] = total
                    ldist[k][i] = total

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

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


main()

Submission status: Wrong Answer

Solution code:
import math

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

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

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

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

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

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

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

for i in g:
    print (i)

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

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

Submission status: Wrong Answer

Solution code:
import sys


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

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

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

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

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

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

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


main()

Submission status: Wrong Answer

Solution code:
import sys


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

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

    for i in range(n-2):
        for j in range(i+1, n-1):
            for k in range(j+1, n):
                total = dist[i][j] + dist[j][k]
                if dist[i][k] > total:
                    dist[i][k] = total
                    dist[k][i] = total

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

    for i in range(n-2):
        for j in range(i+1, n-1):
            for k in range(j+1, n):
                total = ldist[i][j] + ldist[j][k]
                if ldist[i][k] > total:
                    ldist[i][k] = total
                    ldist[k][i] = total

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

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


main()

Submission status: Wrong Answer

Solution code:
import queue

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

dist = [[float('inf')] * N for _ in range(N)]
for i in range(N):
    dist[i][i] = 0

for i in range(M):
    A, B, C = map(int, input().split())
    dist[A-1][B-1] = C
    dist[B-1][A-1] = C

for k in range(N):
    for i in range(N):
        for j in range(N):
            if dist[i][j] > dist[i][k] + dist[k][j]:
                dist[i][j] = dist[i][k] + dist[k][j]

dist2 = [[float('inf')] * N for _ in range(N)]

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

for k in range(N):
    for i in range(N):
        for j in range(N):
            if dist2[i][j] > dist2[i][k] + dist2[k][j]:
                dist2[i][j] = dist2[i][k] + dist2[k][j]

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():    
    n, m, L = [int(x) for x in input().split()]
    from collections import defaultdict
    
    class Graph():
        def __init__(self):
            """
            self.edges is a dict of all possible next nodes
            e.g. {'X': ['A', 'B', 'C', 'E'], ...}
            self.weights has all the weights between two nodes,
            with the two nodes as a tuple as the key
            e.g. {('X', 'A'): 7, ('X', 'B'): 2, ...}
            """
            self.edges = defaultdict(list)
            self.weights = {}
        
        def add_edge(self, from_node, to_node, weight):
            # Note: assumes edges are bi-directional
            self.edges[from_node].append(to_node)
            self.edges[to_node].append(from_node)
            self.weights[(from_node, to_node)] = weight
            self.weights[(to_node, from_node)] = weight
            
    graph = Graph()
    
    
    adj = []
    for i in range(n + 1):
        te = []
        for j in range(n + 1):
            te.append(0)
        adj.append(te)
    edges = []
    for i in range(m):
        a, b, c = [int(x) for x in input().split()]
        edges.append((a, b, c))
        adj[a][b] = c
        adj[b][a] = c
    
    for edge in edges:
        graph.add_edge(*edge)
    
    def dijsktra(graph, initial, end):
        # shortest paths is a dict of nodes
        # whose value is a tuple of (previous node, weight)
        shortest_paths = {initial: (None, 0)}
        current_node = initial
        visited = set()
        
        while current_node != end:
            visited.add(current_node)
            destinations = graph.edges[current_node]
            weight_to_current_node = shortest_paths[current_node][1]
    
            for next_node in destinations:
                weight = graph.weights[(current_node, next_node)] + weight_to_current_node
                if next_node not in shortest_paths:
                    shortest_paths[next_node] = (current_node, weight)
                else:
                    current_shortest_weight = shortest_paths[next_node][1]
                    if current_shortest_weight > weight:
                        shortest_paths[next_node] = (current_node, weight)
            
            next_destinations = {node: shortest_paths[node] for node in shortest_paths if node not in visited}
            if not next_destinations:
                return -1
            # next node is the destination with the lowest weight
            current_node = min(next_destinations, key=lambda k: next_destinations[k][1])
        
        # Work back through destinations in shortest path
        path = []
        while current_node is not None:
            path.append(current_node)
            next_node = shortest_paths[current_node][0]
            current_node = next_node
        # Reverse path
        path = path[::-1]
        return path
    
    q = int(input())
    while q:
        q -= 1
        ans = 1
        l = L
        coun = 0
        s, t = [int(x) for x in input().split()]
        dik = dijsktra(graph, s, t)
        # print(dik)
        if dik == -1: print(-1)
        else:
            for i in range(len(dik) - 1):
                cc = adj[dik[i]][dik[i + 1]]
                if cc > L:
                    ans = 0
                    break
                elif cc > l:
                    coun += 1
                    l = L
                
                l -= cc
                    # if l < 0:
                    #     coun += 1
                    #     l = L
            if ans: print(coun)
            else: print(-1)
if __name__ == "__main__":
    main()