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:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, L, ABC, Q, ST):

    g = defaultdict(dict)
    for a, b, c in ABC:
        g[a][b] = c
        g[b][a] = c
    
    dd = defaultdict(lambda: -1)
    def f(s, t):
        if (s, t) in dd:
            return dd[(s, t)]
        q = [(0, s)]
        done = set([s])
        d = 0
        while q:
            d, u = heapq.heappop(q)
            qq = [(L, u)]
            cand = set()
            while qq:
                rem, uu = heapq.heappop(qq)
                for vv in g[uu]:
                    if rem-g[uu][vv] < 0:
                        continue
                    if vv in cand or vv in done:
                        continue
                    qq.append((rem-g[uu][vv], vv))
                    cand.add(vv)
            
            done |= cand
            for v in cand:
                dd[(s, v)] = d
                heapq.heappush(q, (d+1, v))

        for i in range(1, N+1):
            if i not in done:
                dd[(s, i)] = -1

        return dd[(s, t)]

    for s, t in ST:
        print(f(s, t))



def main():
    N, M, L = read_int_n()
    ABC = [read_int_n() for _ in range(M)]
    Q = read_int()
    ST = [read_int_n() for _ in range(Q)]
    (slv(N, M, L, ABC, Q, ST))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m,L=map(int,input().split())
    A=[[INF]*n for _ in range(n)]
    for _ in range(m):
        a,b,c=map(int,input().split())
        if(c>L): continue
        a-=1; b-=1
        A[a][b]=c
        A[b][a]=c
    B=[a[:] for a in A]
    next=[[0]*n for _ in range(n)]
    from itertools import product
    for i,j in product(range(n),repeat=2):
        next[i][j]=j
    for k,i,j in product(range(n),repeat=3):
        if(B[i][j]>B[i][k]+B[k][j]):
            B[i][j]=B[i][k]+B[k][j]
            next[i][j]=next[i][k]
    Q=int(input())
    for _ in range(Q):
        s,t=map(int,input().split())
        s-=1; t-=1
        if(B[s][t]==INF):
            print(-1)
            continue
        # path restoration
        now=L
        count=0
        while(next[s][t]!=t):
            w=next[s][t]
            c=A[s][w]
            if(now<c):
                count+=1
                now=L-c
            else:
                now-=c
            s=w
        print(count+(now<A[s][t]))
resolve()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
import sys
input = sys.stdin.readline

N, M, L = map(int, input().split())
v = [[] for _ in range(N)]
for _ in range(M) :
    A, B, C = map(int, input().split())
    if C > L :
        continue
    v[A-1].append((B-1, C))
    v[B-1].append((A-1, C))
    
Q = int(input())


start = defaultdict(lambda : [])
for i in range(Q) :
    s, t = map(int, input().split())
    start[s-1].append((t-1, i))
    
ret = [-1] * Q
for s, d in start.items() :
    # print(s, d)
    pre1 = [-1] * N
    pre2 = [float('inf')] * N
    pre1[s] = L #のこりのガソリン
    pre2[s] = 0 #補充回数
    queue = deque([(s, L, 0)])
    
    while queue :
        # print(queue)
        cur, rest, cost = queue.popleft()
        if pre2[cur] < cost or (pre2[cur] == cost and pre1[cur] > rest):
            continue
            
        for nex, c in v[cur] :
            if rest >= c :
                nrest = rest - c
                ncost = cost
            else :
                nrest = L - c
                ncost = cost + 1
                
            if ncost < pre2[nex] or (ncost == pre2[nex] and nrest < pre1[nex]) :                    
                queue.append((nex, nrest, ncost))
                pre1[nex] = nrest
                pre2[nex] = ncost
                
    # print(pre1)
    # print(pre2)
    # print()
    
    for dest, i in d :
        ret[i] = pre2[dest]
        
for r in ret :
    if r == float('inf') :
        print(-1)
    else :
        print(r)

Submission status: Wrong Answer

Solution code:
INF = 1000000000 * 300


def dijkstra(s: int) -> None:
	d[s] = 0	
	while True:
		mincost = INF
		for i in range(n):
			if color[i] != "BLACK" and d[i] < mincost:
				mincost = d[i]
				u = i
		if mincost == INF:
			break
		color[u] = "BLACK"
		for v in range(n):
			if color[v] != "BLACK" and M[u][v] != None:
				if d[u] + M[u][v] < d[v]:
					d[v] = d[u] + M[u][v]
					pi[v] = u
					color[v] = "GRAY"


if __name__ == "__main__":
	n, m, l = map(int, input().split())
	M = [[None for _ in range(n)] for _ in range(n)]
	for _ in range(m):
		a, b, c = map(int, input().split())
		a -= 1
		b -= 1
		M[a][b] = c
		M[b][a] = c
	# print(M)

	Q = int(input())
	for _ in range(Q):
		color = ["WHITE" for _ in range(n)]
		d = [INF for _ in range(n)]
		pi = [None for _ in range(n)]

		s, t = map(int, input().split())
		s, t = s - 1, t - 1
		dijkstra(s)
		v = t
		path = []
		while pi[v] != None:
			path.append(v)
			v = pi[v]
		path.reverse()
		# print(path)
		u = s
		oil = l
		ans = 0
		if not path:
			print("-1")
		else:
			for v in path:
				# print(v)
				if M[u][v] > l:
					print("-1")
					break
				if oil - M[u][v] < 0:
					oil = l
					ans += 1
				oil -= M[u][v]
				u = v
			# print(d[t])
			print(ans)

Submission status: Wrong Answer

Solution code:
inf = 10**6
def warshall_floyd(d):#d[u][v]:辺uvのコスト.存在しなければinf.d[i][i]は0
    for i in range(N):#0~iとa, bのみを使う最短経路を考える
        for a in range(N):
            for b in range(N):
                d[a][b] = min(d[a][b], d[a][i]+ d[i][b])#頂点iを使う場合と使わない場合
    return d
N, M, L = map(int, input().split())
G = [[inf]*N for _ in range(N)]
for i in range(N):
    G[i][i] = 0
for _ in range(M):
    a, b, c = map(int, input().split())
    G[a-1][b-1] = c
    G[b-1][a-1] = c
G = warshall_floyd(G)
G2 = [[0]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            G2[i][j] = 0
        elif G[i][j]<=L:
            G2[i][j] = 1
        else:
            G2[i][j] = inf
d = warshall_floyd(G2)
Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    ans = d[s-1][t-1]-1
    print(ans if ans < 10**9 else -1)

Submission status: Wrong Answer

Solution code:
n,m,l = map(int,input().split())
inf = 10**18
dist = [[inf for i in range(n)] for j in range(n)]

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

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

next_point = [[j for j in range(n)] for i in range(n)]
for i in range(n):
    for j in range(n):
        for k in range(n):
            if dist[j][k] > dist[j][i] + dist[i][k]:
                next_point[j][k] = next_point[j][i]
                dist[j][k] = min(dist[j][k],dist[j][i] + dist[i][k])
            elif  i != j and dist[j][k] == dist[j][i] + dist[i][k]:
                if dist[j][next_point[j][k]] > dist[j][next_point[j][i]]:
                    next_point[j][k] = next_point[j][i]
q = int(input())
#print(dist)
#print(next_point)
for i in range(q):
    s,t = map(int,input().split())
    s -= 1
    t -= 1

    ans = 0
    now = l
    u = s
    while u != t:
        # print(now)
        next_u = next_point[u][t]
        if now < dist[u][next_u]:
            ans += 1
            now = l
        now -= dist[u][next_u]
        u = next_u
        if now < 0:
            ans = -1
            break
    print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, L, ABC, Q, ST):

    g = defaultdict(dict)
    for a, b, c in ABC:
        g[a][b] = c
        g[b][a] = c
    
    dd = defaultdict(lambda: -1)
    def f(s, t):
        if (s, t) in dd:
            return dd[(s, t)]
        q = [(0, s)]
        done = set([s])
        d = 0
        while q:
            d, u = heapq.heappop(q)
            qq = [(L, u)]
            cand = set()
            while qq:
                rem, uu = heapq.heappop(qq)
                for vv in g[uu]:
                    if rem-g[uu][vv] < 0:
                        continue
                    if vv in cand or vv in done:
                        continue
                    qq.append((rem-g[uu][vv], vv))
                    cand.add(vv)
            if t in cand:
                return d
            
            done |= cand
            for v in cand:
                dd[(s, v)] = d
                heapq.heappush(q, (d+1, v))

        for i in range(1, N+1):
            if i not in done:
                dd[(s, i)] = -1

        return dd[(s, t)]

    for s, t in ST:
        print(f(s, t))



def main():
    N, M, L = read_int_n()
    ABC = [read_int_n() for _ in range(M)]
    Q = read_int()
    ST = [read_int_n() for _ in range(Q)]
    (slv(N, M, L, ABC, Q, ST))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

from collections import deque
import copy

N,M,L = II()
A,B,C = Line(M,3)
Q = I()
s,t = Line(Q,2)

E = [[float('inf')]*N for _ in range(N)]
for i in range(N):
    E[i][i] = 0
for i in range(M):
    E[A[i]-1][B[i]-1] = C[i]
    E[B[i]-1][A[i]-1] = C[i]

E2 = copy.deepcopy(E)

d = [[-1]*N for _ in range(N)]
for k in range(N):
    for i in range(N):
        for j in range(N):
            if E[i][j] > E[i][k]+E[k][j]:
                E[i][j] = E[i][k]+E[k][j]
                d[i][j] = k


path = [[deque([]) for _ in range(N)] for _ in range(N)]
for i in range(N):
    for j in range(N):
        k = j
        while d[i][k]!=-1:
            k = d[i][k]
            path[i][j].appendleft(k)
        #path[i][j] = list(reversed(path[i][j]))

ans = [[0]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        l = L
        now = i
        p = 0
        if path[i][j]:
            ne = path[i][j][p]
        else:
            ne = j
        while now != j:
            if E[now][ne]>L:
                ans[i][j] = -1
                break
            elif l < E[now][ne]:
                ans[i][j] += 1
                l = L-E[now][ne]
                now = ne
                if path[ne][j]:
                    ne = path[ne][j][0]
                else:
                    ne = j
            else:
                l -= E[now][ne]
                now = ne
                if path[ne][j]:
                    ne = path[ne][j][0]
                else:
                    ne = j                

for i in range(Q):
    print(ans[s[i]-1][t[i]-1])

Submission status: Wrong Answer

Solution code:
import heapq,sys
input = sys.stdin.readline
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)]

es = [[] for i in range(N)]
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    es[a].append((b,c))
    es[b].append((a,c))

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

for i in range(N):
    hq = [i]
    heapq.heapify(hq)
    while hq:
        v = heapq.heappop(hq)
        k,j = divmod(v,N)
        k,l = divmod(k,L)
        for to,c in es[j]:
            if l+c > L:
                newcost = (k+1)*L + c
            else:
                newcost = k*L + l + c
            if newcost < dist[i][to]:
                dist[i][to] = newcost
                heapq.heappush(hq, newcost*N + to)
    for j in range(N):
        dist[j][i] = dist[i][j]

ans = []
for s,t in ST:
    s,t = s-1,t-1
    if dist[s][t] == INF:
        ans.append(-1)
    else:
        ans.append((dist[s][t]-1)//L)
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input = sys.stdin.readline


n, m, l = map(int, input().split())
info = [list(map(int, input().split())) for i in range(m)]
graph = [[] for i in range(n)]

for i in range(m):
    a, b, cost = info[i]
    if cost > l:
        continue
    a -= 1
    b -= 1
    graph[a].append((b, cost))
    graph[b].append((a, cost))


def dfs(start):
    INF = float("inf")
    visited_t = [INF]*n # 回数, 残り燃料
    visited_l = [0]*n
    visited_t[start] = 0
    prev = -1
    q = deque([(start, prev)])
    while q:
        pos, prev = q.popleft()
        for next_pos, cost in graph[pos]:
            if next_pos < start:
                continue
            if prev == next_pos:
                continue
            tmp = cost + visited_l[pos]
            times = visited_t[pos]
            next_times = visited_t[next_pos]
            if tmp <= l:
                if next_times > times:
                    q.append((next_pos, pos))
                    visited_t[next_pos] = times
                    visited_l[next_pos] = tmp
                elif next_times == times:
                    if visited_l[next_pos] > tmp:
                        q.append((next_pos, pos))
                        visited_t[next_pos] = times
                        visited_l[next_pos] = tmp
                else:
                    continue
            else:
                tmp = cost
                if next_times > times + 1:
                    q.append((next_pos, pos))
                    visited_t[next_pos] = times + 1
                    visited_l[next_pos] = tmp
                elif next_times == times + 1:
                    if visited_l[next_pos] > tmp:
                        q.append((next_pos, pos))
                        visited_t[next_pos] = times + 1
                        visited_l[next_pos] = tmp
                else:
                    continue
    return visited_t

ans = [None]*n
for i in range(n):
    ans[i] = dfs(i)

INF = float("inf")
q = int(input())
query = [list(map(int, input().split())) for i in range(q)]
for i in range(q):
    a, b = query[i]
    a -= 1
    b -= 1
    if b < a:
        a, b= b, a
    tmp = ans[a][b]
    if tmp == INF:
        print(-1)
    else:
        print(tmp)

Submission status: Wrong Answer

Solution code:
import sys

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

D = [[0] * N for _ in range(N)]
R = {}

for a, b, c in ABC:
  a -= 1
  b -= 1
  D[a][b] = c
  D[b][a] = c
  if a not in R:
    R[a] = [[b, c]]
  else:
    R[a].append([b, c])
  if b not in R:
    R[b] = [[a, c]]
  else:
    R[b].append([a, c])

inf = 10 ** 10
T = [[inf] * N for _ in range(N)]
import copy
for i in range(N):
  if i not in R: continue
  S = copy.deepcopy(R[i])
  T[i][i] = 0
  while S:
    t = S.pop()
    if t[1] <= L:
      if t[1] < T[i][t[0]]:
        T[i][t[0]] = t[1]
        for j in R[t[0]]:
          if t[1] + j[1] <= L:
            S.append([j[0], t[1] + j[1]])

for i in range(N):
  for j in range(N):
    if i == j:
      T[i][j] = 0
      continue
    if T[i][j] <= L:
      T[i][j] = 1
    else:
      T[i][j] = inf

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

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

Submission status: Wrong Answer

Solution code:
import heapq
n,m,l=map(int,input().split())
edge=[[] for i in range(n)]
for i in range(m):
  a,b,c=map(int,input().split())
  edge[a-1].append([c,b-1])
  edge[b-1].append([c,a-1])

def f(s):
  d=[float("inf")]*n
  used=[True]*n
  d[s]=0
  used[s]=False
  edgelist=[]
  ans=0
  cur=l
  for c,e in edge[s]:
    heapq.heappush(edgelist,c*10**3+e)
  while edgelist:
    minedge=heapq.heappop(edgelist)
    v=minedge%(10**3)
    ans,cur=minedge//(10**13),minedge%(10**13)//(10**3)
    used[v]=False
    d[v]=ans
    
    for e in edge[v]:
      if used[e[1]]:
        if e[0]>l:
          continue
        elif e[0]+cur>l:
          heapq.heappush(edgelist,(ans+1)*10**13+e[0]*10**3+e[1])
        else:
          heapq.heappush(edgelist,(ans)*10**13+(e[0]+cur)*10**3+e[1])
  return d
Ans=[[] for i in range(n)]
for i in range(n):
  Ans[i]=f(i)
q=int(input())
for _ in range(q):
  s,t=map(int,input().split())
  print(-1 if Ans[s-1][t-1]==float("inf") else Ans[s-1][t-1])

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m,l = LI()
    v = [[] for i in range(n)]
    for i in range(m):
        a,b,c = LI()
        a -= 1
        b -= 1
        v[a].append((b,c))
        v[b].append((a,c))
    ans = [[float("inf")]*n for i in range(n)]
    q = []
    for s in range(n):
        heappush(q,(0,s))
        ans[s][s] = 0
        while q:
            dx,x = heappop(q)
            na = ans[s][x]
            for y,w in v[x]:
                if w > l:
                    continue
                nd = dx+w
                na_ = na+(nd > l)
                nd_ = nd if nd <= l else w
                if na_ < ans[s][y]:
                    ans[s][y] = na_
                    heappush(q,(nd_,y))
    q = I()
    for _ in range(q):
        s,t = LI()
        s -= 1
        t -= 1
        ds = ans[s][t]
        if ds == float("inf"):
            print(-1)
        else:
            print(ds)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import heapq,sys
input = sys.stdin.readline
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)]

es = [[] for i in range(N)]
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    es[a].append((b,c))
    es[b].append((a,c))

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

for i in range(N):
    hq = [i]
    heapq.heapify(hq)
    while hq:
        v = heapq.heappop(hq)
        k,j = divmod(v,N)
        k,l = divmod(k,L)
        for to,c in es[j]:
            if l+c > L:
                newcost = (k+1)*L + c
            else:
                newcost = k*L + l + c
            if newcost < dist[i][to]:
                dist[i][to] = dist[to][i] = newcost
                heapq.heappush(hq, newcost*N + to)

ans = []
for s,t in ST:
    s,t = s-1,t-1
    if dist[s][t] == INF:
        ans.append(-1)
    else:
        ans.append((dist[s][t]-1)//L)
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
import heapq
n, m, l = list(map(int, raw_input().split(" ")))

roads = [[-1 for _ in range(n)] for _ in range(n)]
for _ in range(m):
	a, b, c = list(map(int, raw_input().split(" ")))
	roads[a-1][b-1] = c
	roads[b-1][a-1] = c


def solve(s):
	dp = [None for _ in range(n)]
	h = [(0, l, s)]

	while h:
		val, cur_cap, v = heapq.heappop(h)
		if dp[v] is not None:
			continue

		dp[v] = (val, cur_cap)
		for t in range(n):
			if v == t or roads[v][t] == -1 or dp[t] is not None:
				continue

			if roads[v][t] <= cur_cap:
				ex_ans, ex_cap = val, cur_cap-roads[v][t]
			else:
				ex_ans, ex_cap = val+1, l-roads[v][t]

			heapq.heappush(h, (ex_ans, ex_cap, t))
	return dp

ans = [solve(i) for i in range(n)]
print(ans)

k = int(raw_input())
for _ in range(k):
	s, t = list(map(int, raw_input().split(" ")))
	print(ans[s-1][t-1][0] if ans[s-1][t-1] is not None else -1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
from bisect import bisect_left, bisect_right
import random
from itertools import permutations, accumulate, combinations
import sys
import string



INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7



def dijkstra(graph, vst=0):
    que = deque([(0, l, vst)])
    D = [INF] * n
    D[vst] = 0
    update_time = 0
    while que:
        sup_num, remain, now_v = que.popleft()
        for to_v, c in graph[now_v]:
            if remain - c < 0:
                continue
            if sup_num > D[to_v]:
                continue
            que += [(sup_num, remain - c, to_v)]
            if sup_num < D[to_v] - 1:
                que += [(sup_num + 1, l, to_v)]
            D[to_v] = sup_num
    return D


n, m, l = LI()
graph = [[] for _ in range(n)]
for i in range(m):
    a, b, c = LI()
    graph[a - 1] += [(b - 1, c)]
    graph[b - 1] += [(a - 1, c)]



sup_num_matrix = []


for j in range(n):
    D = dijkstra(graph, vst=j)
    sup_num_matrix += [D]



q = I()
for k in range(q):
    a, b = LI()
    ans = sup_num_matrix[a - 1][b - 1]
    if ans == INF:
        print(-1)
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
n,m,l = map(int,input().split())
road = [[] for _ in range(n)]
for _ in range(m):
  a,b,c = map(int,input().split())
  a -= 1
  b -= 1
  road[a].append([b,c])
  road[b].append([a,c])
cango = []
for i in range(n):
  tmp = [l+1 for _ in range(n)]
  tmp[i] = 0
  now = [[i,0]]
  while now:
    last = now
    now = []
    for x in last:
      for yz in road[x[0]]:
        y,z = yz
        if x[1] + z < tmp[y]:
          tmp[y] = x[1] + z
          now.append([y,tmp[y]])
  add = []
  for i in range(n):
    if tmp[i] == 0:
      add.append(0)
    elif tmp[i] <= l:
      add.append(1)
    else:
      add.append(1000)
  cango.append(add)
#print(cango)
for k in range(n):
  for i in range(n):
    for j in range(n):
      if cango[i][k] + cango[k][j] < cango[i][j]:
        cango[i][j] = cango[i][k] + cango[k][j]
#print(cango)
q = int(input())
for _ in range(q):
  s,t = map(int,input().split())
  print(cango[s-1][t-1]-1)

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

import copy

N,M,L = II()
A,B,C = Line(M,3)
Q = I()
s,t = Line(Q,2)

E = [[float('inf')]*N for _ in range(N)]
for i in range(N):
    E[i][i] = 0
for i in range(M):
    E[A[i]-1][B[i]-1] = C[i]
    E[B[i]-1][A[i]-1] = C[i]

E2 = copy.deepcopy(E)

d = [[-1]*N for _ in range(N)]
for k in range(N):
    for i in range(N):
        for j in range(N):
            if E[i][j] > E[i][k]+E[k][j]:
                E[i][j] = E[i][k]+E[k][j]
                d[i][j] = k


path = [[[] for _ in range(N)] for _ in range(N)]
for i in range(N):
    for j in range(N):
        k = j
        while d[i][k]!=-1:
            k = d[i][k]
            path[i][j].append(k)
        path[i][j] = list(reversed(path[i][j]))

ans = [[0]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        l = L
        now = i
        p = 0
        if path[i][j]:
            ne = path[i][j][p]
        else:
            ne = j
        while now != j:
            if E2[now][ne]>L:
                ans[i][j] = -1
                break
            elif l < E2[now][ne]:
                ans[i][j] += 1
                l = L-E2[now][ne]
                now = ne
                if path[ne][j]:
                    ne = path[ne][j][0]
                else:
                    ne = j
            else:
                l -= E2[now][ne]
                now = ne
                if path[ne][j]:
                    ne = path[ne][j][0]
                else:
                    ne = j                

for i in range(Q):
    print(ans[s[i]-1][t[i]-1])