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: Time Limit Exceeded

Solution code:
N, M, L = map(int,input().split())
rd = [[] for _ in range(N+1)]
rc = [[] for _ in range(N+1)]

for _ in range(M):
    a,b,c = map(int,input().split())
    if c <= L:
        rd[a].append(b)
        rc[a].append(c)
        rd[b].append(a)
        rc[b].append(c)

Q = int(input())
S = []
T = []
for _ in range(Q):
    s,t = map(int,input().split())
    S.append(s)
    T.append(t)

ZO = [[-1 for i in range(N+1)] for i in range(N+1)]
NP = [[10**9+1 for i in range(N+1)] for i in range(N+1)]

for i in range(1,N+1):
    for d,c in zip(rd[i], rc[i]):
        NP[i][d] = 0
        ZO[i][d] = L - c

up = True
while up:
    up = False
    for i in range(1,N+1):
        for j in range(1,N+1):
            if ZO[i][j] < 0: continue
            for d,c in zip(rd[j], rc[j]):
                if d == i: continue
                if ZO[i][j] < c:
                    nnp = NP[i][j] + 1
                    nzo = L - c
                else:
                    nnp = NP[i][j]
                    nzo = ZO[i][j] - c
                if NP[i][d] > nnp or NP[i][d] == nnp and ZO[i][d] < nzo:
                    ZO[i][d] = nzo
                    NP[i][d] = nnp
                    up = True
                    
for i in range(Q):
    if NP[S[i]][T[i]] > 10**9:
        print (-1)
    else:
        print(NP[S[i]][T[i]])

Submission status: Time Limit Exceeded

Solution code:
N, M, L = map(int,input().split())
rd = [[] for _ in range(N+1)]
rc = [[] for _ in range(N+1)]

for _ in range(M):
    a,b,c = map(int,input().split())
    if c <= L:
        rd[a].append(b)
        rc[a].append(c)
        rd[b].append(a)
        rc[b].append(c)

Q = int(input())
S = []
T = []
for _ in range(Q):
    s,t = map(int,input().split())
    S.append(s)
    T.append(t)

ZO = [[-1 for i in range(N+1)] for i in range(N+1)]
NP = [[10**9+1 for i in range(N+1)] for i in range(N+1)]

for i in range(1,N+1):
    for j in range(len(rd[i])):
        d = rd[i][j]
        c = rc[i][j]
        NP[i][d] = 0
        ZO[i][d] = L - c

up = True
while up:
    up = False
    for i in range(1,N+1):
        for j in range(1,N+1):
            if ZO[i][j] < 0: continue
            for k in range(len(rd[j])):
                d = rd[j][k]
                if d == i: continue
                c = rc[j][k]
                if ZO[i][j] < c:
                    nnp = NP[i][j] + 1
                    nzo = L - c
                else:
                    nnp = NP[i][j]
                    nzo = ZO[i][j] - c
                if NP[i][d] > nnp or NP[i][d] == nnp and ZO[i][d] < nzo:
                    ZO[i][d] = nzo
                    NP[i][d] = nnp
                    up = True

for i in range(Q):
    if NP[S[i]][T[i]] > 10**9:
        print (-1)
    else:
        print(NP[S[i]][T[i]])

Submission status: Time Limit Exceeded

Solution code:
import sys
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())

def dijkstra(s,n,g):
    que = []
    d = [10**15 for _ in range(n)]
    d[s] = 0
    heappush( que,(0,s) )
    while len(que)>0:
        p =  heappop(que)
        v = p[1]
        if d[v] < p[0]:
            continue
        for i in range(len(g[v])):
            edge = g[v][i]
            if d[edge[0]] > d[v] + edge[1]:
                d[edge[0]] = d[v] + edge[1]
                heappush( que,(d[edge[0]] , edge[0]) )
    return d

def main():
    n,w,l = inpm()
    g = [[] for _ in range(n)]
    for _ in range(w):
        x,y,z = inpm()
        x -= 1
        y -= 1
        if z > l:
            continue
        g[x].append((y,z))
        g[y].append((x,z))
    d = [[] for _ in range(n)]
    for i in range(n):
        d[i] = dijkstra(i,n,g)
    G = [[] for _ in range(n)]
    for i in range(n-1):
        for j in range(i+1,n):
            if d[i][j] <= l:
                G[i].append((j,1))
                G[j].append((i,1))
    q = inp()
    d = [[] for _ in range(n)]
    for i in range(n):
        d[i] = dijkstra(i,n,G)
    for _ in range(q):
        s,t = inpm()
        s -= 1
        t -= 1
        ans = d[s][t]
        if ans == 10**15:
            print(-1)
            continue
        print(ans-1)
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())

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

def main():
    n,w,l = inpm()
    d = [10**20 for _ in range(n**2)]
    for _ in range(w):
        x,y,z = inpm()
        x -= 1
        y -= 1
        if z > l:
            continue
        d[x*n+y] = z
        d[y*n+x] = z
    d = warshall_floyd(d,n)
    d = tuple(d)
    e = [10**20 for _ in range(n**2)]
    for i in range(n-1):
        for j in range(i+1,n):
            if d[i*n+j] <= l:
                e[i*n+j] = 1
                e[j*n+i] = 1
    q = inp()
    e = warshall_floyd(e,n)
    e = tuple(e)
    for _ in range(q):
        s,t = inpm()
        s -= 1
        t -= 1
        ans = e[s*n+t]
        if ans == 10**20:
            print(-1)
            continue
        print(ans-1)
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

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

def main():
    n,w,l = inpm()
    d = [[10**20 for i in range(n)] for i in range(n)] 
    for _ in range(w):
        x,y,z = inpm()
        x -= 1
        y -= 1
        if z > l:
            continue
        d[x][y] = z
        d[y][x] = z
    d = warshall_floyd(d,n)
    e = [[10**20 for _ in range(n)] for _ in range(n)]
    for i in range(n-1):
        for j in range(i+1,n):
            if d[i][j] <= l:
                e[i][j] = 1
                e[j][i] = 1
    q = inp()
    e = warshall_floyd(e,n)
    for _ in range(q):
        s,t = inpm()
        ans = e[s-1][t-1]
        if ans == 10**20:
            print(-1)
            continue
        print(ans-1)
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    wf = [[0 for i in range(n)] for j 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 k in range(n):
        for i in range(n):
            for j in range(n):
                if (wf[i][k] != 0 and wf[k][j] != 0) and (wf[i][j] > wf[i][k] + wf[k][j] or wf[i][j] == 0):
                    wf[i][j] = wf[i][k] + wf[k][j]
    for i in range(n):
        for j in range(n):
            if wf[i][j] <= l and wf[i][j] != 0:
                wf[i][j] = 1
            else:
                wf[i][j] = 301
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if wf[i][j] > wf[i][k] + wf[k][j]:
                    wf[i][j] = wf[i][k] + wf[k][j]
    q = int(input())
    ans = [None] * q
    for i in range(q):
        s, t = map(int, input().split())
        if wf[s-1][t-1] < 301:
            ans[i] = wf[s-1][t-1]
        else:
            ans[i] = 0
    for i in range(q):
        print(ans[i] - 1)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    wf = [[(300 * (10**9) + 1) for i in range(n)] for j 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 k in range(n):
        for i in range(n):
            for j in range(n):
                if wf[i][j] > wf[i][k] + wf[k][j]:
                    wf[i][j] = wf[i][k] + wf[k][j]
    for i in range(n):
        for j in range(n):
            if wf[i][j] <= l:
                wf[i][j] = 1
            else:
                wf[i][j] = 301
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if wf[i][j] > wf[i][k] + wf[k][j]:
                    wf[i][j] = wf[i][k] + wf[k][j]
    q = int(input())
    ans = [None] * q
    for i in range(q):
        s, t = map(int, input().split())
        if wf[s-1][t-1] < 301:
            ans[i] = wf[s-1][t-1]
        else:
            ans[i] = 0
    for i in range(q):
        print(ans[i] - 1)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N, m, l = map(int, input().split())
graph = [[] for i in range(N+1)]

INF = 2 ** 63 - 1
d = [[INF for i in range(N+1)] for j in range(N+1)]
for i in range(N+1):
    d[i][i] = 0
for i in range(m):
    a, b, c = map(int, input().split())
    d[a][b] = c
    d[b][a] = c


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

d2 = [[INF for i in range(N+1)] for j in range(N+1)]
for i in range(N+1):
    d[i][i] = 0

for i in range(N+1):
    for j in range(N+1):
        if d[i][j] <= l:
            d2[i][j] = 1

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

q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    if d2[s][t] == INF:
        print(-1)
    else:
        print(d2[s][t] - 1)

Submission status: Time Limit Exceeded

Solution code:
N, m, l = map(int, input().split())
graph = [[] for i in range(N+1)]

INF = 2 ** 63 - 1
d = [[INF for i in range(N+1)] for j in range(N+1)]
for i in range(N+1):
    d[i][i] = 0
for i in range(m):
    a, b, c = map(int, input().split())
    d[a][b] = c
    d[b][a] = c


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

d2 = [[INF for i in range(N+1)] for j in range(N+1)]
for i in range(N+1):
    d[i][i] = 0

for i in range(N+1):
    for j in range(N+1):
        if d[i][j] <= l:
            d2[i][j] = 1

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

q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    if d2[s][t] == INF:
        print(-1)
    else:
        print(d2[s][t] - 1)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    def input():
        return sys.stdin.readline()

    N, M, L = map(int, input().split())
    D = [[float('inf')]*(N+1) for _ in range(N+1)]
    Ans = [[[float('inf'),0] for i in range(N+1)] for _ in range(N+1)]
    G = [[] for i in range(N+1)]

    import heapq
#heapq.heappush(heap, item)
#heapq.heappop(heap)
 
    for i in range(N+1):
        D[i][i] = 0
        Ans[i][i] = [0,L]

    for _ in range(M):
        A,B,C = map(int, input().split())
        if C <= L:
            D[A][B]=C
            D[B][A]=C
            G[A].append(B)
            G[B].append(A)

    for i in range(1,N+1):
        heap = []
        heapq.heappush(heap, [0,L,i])
        while heap != []:
            s = heapq.heappop(heap)
            for j in G[s[2]]:
                if Ans[i][s[2]][1] >= D[s[2]][j]:
                    tmp = [Ans[i][s[2]][0], Ans[i][s[2]][1] - D[s[2]][j]]
                else:
                    tmp = [Ans[i][s[2]][0]+1, L - D[s[2]][j]]
                if (Ans[i][j][0] > tmp[0]) or (Ans[i][j][0] == tmp[0] and Ans[i][j][1] < tmp[1]):
                    Ans[i][j] = tmp
                    heapq.heappush(heap,[tmp[0], tmp[1], j])
                
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        if Ans[s][t][0] != float('inf'):
            print(Ans[s][t][0])
        else:
            print(-1)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    def input():
        return sys.stdin.readline()

    N, M, L = map(int, input().split())
    D = [[float('inf')]*(N+1) for _ in range(N+1)]
    Ans = [[[float('inf'),0] for i in range(N+1)] for _ in range(N+1)]
    from collections import defaultdict
    G = defaultdict(list)

    import heapq
#heapq.heappush(heap, item)
#heapq.heappop(heap)

    for i in range(N+1):
        D[i][i] = 0
        Ans[i][i] = [0,L]

    for _ in range(M):
        A,B,C = map(int, input().split())
        if C <= L:
            D[A][B]=C
            D[B][A]=C
            G[A].append(B)
            G[B].append(A)

    for i in range(1,N+1):
        heap = []
        heapq.heappush(heap, [0,L,i])
        while heap != []:
            s = heapq.heappop(heap)
            for j in G[s[2]]:
                if Ans[i][s[2]][1] >= D[s[2]][j]:
                    tmp = [Ans[i][s[2]][0], Ans[i][s[2]][1] - D[s[2]][j]]
                else:
                    tmp = [Ans[i][s[2]][0]+1, L - D[s[2]][j]]
                if (Ans[i][j][0] > tmp[0]) or (Ans[i][j][0] == tmp[0] and Ans[i][j][1] < tmp[1]):
                    Ans[i][j] = tmp
                    heapq.heappush(heap,[tmp[0], tmp[1], j])
                
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        if Ans[s][t][0] != float('inf'):
            print(Ans[s][t][0])
        else:
            print(-1)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    def input():
        return sys.stdin.readline()

    N, M, L = map(int, input().split())
    D = [[float('inf')]*(N+1) for _ in range(N+1)]
    Ans = [[[float('inf'),0] for i in range(N+1)] for _ in range(N+1)]
    from collections import defaultdict
    G = defaultdict(list)

    import heapq
#heapq.heappush(heap, item)
#heapq.heappop(heap)

    for i in range(N+1):
        D[i][i] = 0
        Ans[i][i] = [0,L]

    for _ in range(M):
        A,B,C = map(int, input().split())
        if C <= L:
            D[A][B]=C
            D[B][A]=C
            G[A].append(B)
            G[B].append(A)

    for i in range(1,N+1):
        heap = []
        heapq.heappush(heap, [0,L,i])
        while heap != []:
            s = heapq.heappop(heap)
            for j in G[s[2]]:
                if Ans[i][s[2]][1] >= D[s[2]][j]:
                    tmp = [Ans[i][s[2]][0], Ans[i][s[2]][1] - D[s[2]][j]]
                else:
                    tmp = [Ans[i][s[2]][0]+1, L - D[s[2]][j]]
                if Ans[i][j][0] > tmp[0]:
                    Ans[i][j] = tmp
                    heapq.heappush(heap,[tmp[0], tmp[1], j])
                elif Ans[i][j][0] == tmp[0] and Ans[i][j][1] < tmp[1]:
                    Ans[i][j] = tmp
                    heapq.heappush(heap,[tmp[0], tmp[1], j])
                
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        if Ans[s][t][0] != float('inf'):
            print(Ans[s][t][0])
        else:
            print(-1)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()

N, M, L = map(int, input().split())
D = [[float('inf')]*(N+1) for _ in range(N+1)]
Ans = [[[float('inf'),0] for i in range(N+1)] for _ in range(N+1)]
from collections import defaultdict
G = defaultdict(list)

import heapq
#heapq.heappush(heap, item)
#heapq.heappop(heap)

for i in range(N+1):
    D[i][i] = 0
    Ans[i][i] = [0,L]

for _ in range(M):
    A,B,C = map(int, input().split())
    if C <= L:
        D[A][B]=C
        D[B][A]=C
        G[A].append(B)
        G[B].append(A)

for i in range(1,N+1):
    heap = []
    heapq.heappush(heap, [0,L,i])
    while heap != []:
        s = heapq.heappop(heap)
        for j in G[s[2]]:
            if Ans[i][s[2]][1] >= D[s[2]][j]:
                tmp = [Ans[i][s[2]][0], Ans[i][s[2]][1] - D[s[2]][j]]
            else:
                tmp = [Ans[i][s[2]][0]+1, L - D[s[2]][j]]
            if Ans[i][j][0] > tmp[0]:
                Ans[i][j] = tmp
                heapq.heappush(heap,[tmp[0], tmp[1], j])
            elif Ans[i][j][0] == tmp[0] and Ans[i][j][1] < tmp[1]:
                Ans[i][j] = tmp
                heapq.heappush(heap,[tmp[0], tmp[1], j])
                
Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    if Ans[s][t][0] != float('inf'):
        print(Ans[s][t][0])
    else:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
N, M, L = map(int, input().split())
D = [[float('inf')]*(N+1) for _ in range(N+1)]
Ans = [[[float('inf'),0] for i in range(N+1)] for _ in range(N+1)]
from collections import defaultdict
G = defaultdict(list)

import heapq
#heapq.heappush(heap, item)
#heapq.heappop(heap)

for i in range(N+1):
    D[i][i] = 0
    Ans[i][i] = [0,L]

for _ in range(M):
    A,B,C = map(int, input().split())
    if C <= L:
        D[A][B]=C
        D[B][A]=C
        G[A].append(B)
        G[B].append(A)

for i in range(1,N+1):
    heap = []
    heapq.heappush(heap, [0,L,i])
    while heap != []:
        s = heapq.heappop(heap)
        for j in G[s[2]]:
            if Ans[i][s[2]][1] >= D[s[2]][j]:
                tmp = [Ans[i][s[2]][0], Ans[i][s[2]][1] - D[s[2]][j]]
            else:
                tmp = [Ans[i][s[2]][0]+1, L - D[s[2]][j]]
            if Ans[i][j][0] > tmp[0]:
                Ans[i][j] = tmp
                heapq.heappush(heap,[tmp[0], tmp[1], j])
            elif Ans[i][j][0] == tmp[0] and Ans[i][j][1] < tmp[1]:
                Ans[i][j] = tmp
                heapq.heappush(heap,[tmp[0], tmp[1], j])
                
Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    if Ans[s][t][0] != float('inf'):
        print(Ans[s][t][0])
    else:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
def warshall_floyd(v_count: int, matrix: list) -> list:
    """ ワーシャルフロイド
    v_count: 頂点数
    matrix: 隣接行列(到達不能はfloat("inf"))
    """
    for i in range(v_count):
        for j, c2 in enumerate(row[i] for row in matrix):
            for k, (c1, c3) in enumerate(zip(matrix[j], matrix[i])):
                if c1 > c2+c3:
                    matrix[j][k] = c2+c3
    return matrix


n, m, l = map(int, input().split())
mat = [[float('inf')] * n for _ in range(n)]
for i in range(n):
    mat[i][i] = 0
for _ in range(m):
    a, b, c = map(int, input().split())
    mat[a - 1][b - 1] = c
    mat[b - 1][a - 1] = c
mat = warshall_floyd(n, mat)
fuel_mat = [[float('inf')] * n for _ in range(n)]
for i in range(n):
    for j in range(n):
        if i == j:
            fuel_mat[i][j] = 0
        elif mat[i][j] <= l:
            fuel_mat[i][j] = 1
fuel_mat = warshall_floyd(n, fuel_mat)
q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    d = fuel_mat[s - 1][t - 1] - 1
    if d == float('inf'):
        print(-1)
    else:
        print(d)

Submission status: Time Limit Exceeded

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

def war(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])
    return d

d=[[float("inf")]*N for _ in range(N)]
d2=[[float("inf")]*N for _ in range(N)]

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

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

d=war(d)

for i in range(N):
    for j in range(N):
        if d[i][j]<=L:
            d2[i][j]=1
for i in range(N):
    d2[i][i]=0
d2=war(d2)
#print(d)
#print(d2)
Q=int(input())
for i in range(Q):
    s,t=map(int,input().split())
    print(d2[s-1][t-1]-1 if d2[s-1][t-1]!=float("inf") else -1)

Submission status: Time Limit Exceeded

Solution code:
N,M,L=map(int,input().split())
G=[[10**15 for i in range(N)] for i in range(N)]
for i in range(M):
    a,b,c=map(int,input().split())
    G[a-1][b-1]=c
    G[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])
H=[[10**15 for i in range(N)] for i in range(N)]
for i in range(N):
    for j in range(N):
        if G[i][j]<=L:
            H[i][j]=1
for k in range(N):
    for i in range(N):
        for j in range(N):
            H[i][j]=min(H[i][j],H[i][k]+H[k][j])
Q=int(input())
for line in range(Q):
    s,t=map(int,input().split())
    s-=1;t-=1
    if H[s][t]==10**15:
        print(-1)
    else:
        print(H[s][t]-1)

Submission status: Time Limit Exceeded

Solution code:

N,M,L = 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)]

INF = 1<<30
dp = [[INF]*N for _ in range(N)]

for a,b,c in ABC:
    dp[a-1][b-1] = c
    dp[b-1][a-1] = c
    
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])
        
d = [[INF]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if dp[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])
        
for s,t in st:
    if d[s-1][t-1]!=INF:
        print(d[s-1][t-1]-1)
    else:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
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 _ in range(M):
    a, b, c = map(int, input().split())
    dist[a-1][b-1] = dist[b-1][a-1] = c

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

old_dist = [row[:] for row in dist]
for i in range(N):
    for j in range(N):
        if old_dist[i][j] <= L:
            dist[i][j] = 1
        else:
            dist[i][j] = float('inf')
        if i == j:
            dist[i][j] = 0
            
for k in range(N):
    for i in range(N):
        for j in range(N):
            dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

Q = int(input())
for _ in range(Q):
    start, end= map(int, input().split())
    if dist[start-1][end-1] != float('inf'):
    	print(dist[start-1][end-1] - 1)
    else:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
def floyd_warshall(dist, graph):
    for i in range(N):
        dist[i][i] = 0
    
    for i in range(N):
        for j in range(i):
            dist[i][j] = dist[j][i] = graph[i][j]
        
    for k in range(N):
        for i in range(N):
            for j in range(N):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
    
N, M, L = map(int, input().split())
graph = [[float('inf')] * N for _ in range(N)]

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

dist = [[float('inf')] * N for _ in range(N)]
floyd_warshall(dist, graph)

graph1 = [[float('inf')] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if dist[i][j] <= L:
            graph1[i][j] = 1
            
dist1 = [[float('inf')] * N for _ in range(N)]
floyd_warshall(dist1, graph1)

Q = int(input())
for _ in range(Q):
    start, end= map(int, input().split())
    if dist1[start-1][end-1] != float('inf'):
    	print(dist1[start-1][end-1] - 1)
    else:
        print(-1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, M, L= map(int,input().split())
cost = [[float('inf')]*N for i in range(N)]

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

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

for k in range(N):
    for i in range(N):
        for j in range(N):
            cost[i][j] = min(cost[i][k] + cost[k][j], cost[i][j])
for i in range(N):
    for j in range(N):
        if cost[i][j] > L:
            cost[i][j] = float('inf')
        else:
            cost[i][j] = 1

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

Q = int(input())
ans = []
for i in range(Q):
    s,t = map(int,input().split())
    ans.append(cost[s-1][t-1]-1)
for i in ans:
    if i == float('inf'):
        print(-1)
    else:
        print(i)

Submission status: Time Limit Exceeded

Solution code:
import sys
from copy import deepcopy

def main():
    INF = 10**18
    input = sys.stdin.readline
    N, M, L = [int(x) for x in input().split()]
    d = [set() for _ in range(N+1)]
    cs = [[INF] * (N+1) for _ in range(N+1)]
    for _ in range(M):
        A, B, C = [int(x) for x in input().split()]
        d[A].add(B)
        d[B].add(A)
        cs[A][B] = C
        cs[B][A] = C

    #ds = [[INF] * (N+1) for in range(N+1)]:
    ds = deepcopy(cs)
    bs = [[INF] * (N+1) for _ in range(N+1)]
    for k in range(1, N+1):
        for i in range(1, N+1):
            for j in range(1, N+1):
                ds[i][j] = min(ds[i][j], ds[i][k] + ds[k][j])
                if ds[i][j] <= L:
                    bs[i][j] = 1

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

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def travel_by_car():
    import sys
    from heapq import heappush, heappop
    
    INF = float('inf')
    f = sys.stdin
    
    def dijkstra(i, N, L):
        h = []
        color = ['white'] * N
        d = [[INF, 0, 0] for _ in range(N)]
        d[i] = [0, -L, i]
        heappush(h, d[i])
        
        while h:
            supply, rem, u = heappop(h)
            color[u] = 'black'
            
            for cost, v in adj[u]:
                if color[v] != 'black' and cost <= L:
                    if -rem < cost:
                        new_dv = [supply + 1, -(L - cost), v]
                    else:
                        new_dv = [supply, rem + cost, v]
                    
                    if new_dv < d[v]:
                        d[v] = new_dv
                        heappush(h, new_dv)
                    
        return d
    
    N, M, L = map(int, f.readline().split())
    
    adj = [[] for _ in range(N)]
    for _ in range(M):
        A, B, C = map(int, f.readline().split())
        adj[A-1].append((C, B-1))
        adj[B-1].append((C, A-1))
    
    dist = [None] * N
    for i in range(N):    
        dist[i] = dijkstra(i, N, L)

    Q = int(f.readline())

    ST = [[int(i) - 1 for i in f.readline().split()] for _ in range(Q)]

    for s, t in ST:
        ans = dist[s][t][0]
        if ans == INF:
            ans = -1
        print(ans)
        
        
def main():
  travel_by_car()


if __name__ == '__main__':
  main()