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 heapq import heappush, heappop
import sys
input = sys.stdin.readline

N, M, L = map(int, input().split())
inf = 10**20
d = [[inf if i!=j else 0 for i in range(N)] for j in range(N)]

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

charged_dict = [[(inf,inf)]*N for _ in range(N)]
for j in range(N):
    charged_dict[j][j] = (0, 0)
    q = [(0, 0, j)]
    while q:
        charged, used, u = heappop(q)
        for v, next_cost in enumerate(d[u]):
            if next_cost == inf:
                continue
            c = (charged+1, next_cost) if used+next_cost>L else (charged, used+next_cost)
            if charged_dict[j][v] > c:
                charged_dict[j][v] = c
                heappush(q, c+(v,))

Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    ans = charged_dict[s-1][t-1][0]
    print(ans if ans!=inf else -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
import sys
input = sys.stdin.readline

N, M, L = map(int, input().split())
inf = 10**20
d = [[inf]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i==j:
            d[i][j] = 0

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

charged_dict = [[(inf,inf)]*N for _ in range(N)]
for j in range(N):
    charged_dict[j][j] = (0, 0)
    q = [(0, -L, j)]
    while q:
        charged, fuel, u = heappop(q)
        for v, next_cost in enumerate(d[u]):
            if next_cost == inf:
                continue
            c = (charged+1, -(L-next_cost)) if -fuel-next_cost<0 else (charged, -(-fuel-next_cost))
            if charged_dict[j][v] > c:
                charged_dict[j][v] = c
                heappush(q, c+(v,))

Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    ans = charged_dict[s-1][t-1][0]
    print(ans if ans!=inf else -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop

N, M, L = map(int, input().split())
inf = 10**20
d = [[inf]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i==j:
            d[i][j] = 0

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

charged_dict = [[(inf,inf)]*N for _ in range(N)]
for j in range(N):
    charged_dict[j][j] = (0, 0)
    q = [(0, -L, j)]
    while q:
        charged, fuel, u = heappop(q)
        for v, next_cost in enumerate(d[u]):
            if next_cost == inf:
                continue
            c = (charged+1, -(L-next_cost)) if -fuel-next_cost<0 else (charged, -(-fuel-next_cost))
            if charged_dict[j][v] > c:
                charged_dict[j][v] = c
                heappush(q, c+(v,))

Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    ans = charged_dict[s-1][t-1][0]
    print(ans if ans!=inf else -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop

N, M, L = map(int, input().split())
inf = 10**20
d = [[inf]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i==j:
            d[i][j] = 0

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

charged_dict = []
for j in range(N):
    dist = [(inf,inf)]*N
    dist[j] = (0, 0)
    q = [(0, -L, j)]
    while q:
        charged, fuel, u = heappop(q)
        for v, next_cost in enumerate(d[u]):
            if next_cost == inf:
                continue
            c = (charged+1, -(L-next_cost)) if -fuel-next_cost<0 else (charged, -(-fuel-next_cost))
            if dist[v] > c:
                dist[v] = c
                heappush(q, c+(v,))
    charged_dict.append(dist)

Q = int(input())
l = []
for _ in range(Q):
    s, t = map(int, input().split())
    ans = charged_dict[s-1][t-1][0]
    print(ans if ans!=inf else -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop

N, M, L = map(int, input().split())
inf = 10**20
d = [[inf]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i==j:
            d[i][j] = 0

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

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

charged_dict = []

for j in range(N):
    dist = [(inf,inf)]*N
    dist[j] = (0, 0)
    q = [(0, -L, 0, j)]
    while q:
        charged, fuel, cost, u = heappop(q)
        for v, next_cost in enumerate(d[u]):
            if next_cost == inf:
                continue
            c = (charged+1, -(L-next_cost)) if -fuel-next_cost<0 else (charged, -(-fuel-next_cost))
            if dist[v] > c:
                dist[v] = c

                if -fuel-next_cost<0:
                    heappush(q, (charged+1, -(L-next_cost), next_cost+cost, v))
                else:
                    heappush(q, (charged, -(-fuel-next_cost), next_cost+cost, v))
    charged_dict.append(dist)

for s, t in l:
    ans = charged_dict[s-1][t-1][0]
    print(ans if ans!=inf else -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import defaultdict

N, M, L = map(int, input().split())
inf = 10**20
c = [[inf]*N for _ in range(N)]
d = defaultdict(list)

for i in range(N):
    for j in range(N):
        if i==j:
            c[i][j]=0

for _ in range(M):
    A, B, C = map(int, input().split())
    c[A-1][B-1] = C
    c[B-1][A-1] = C
    d[A-1].append(B-1)
    d[B-1].append(A-1)

Q = int(input())
l = [tuple(map(int, input().split())) for _ in range(Q)]
charged_dict = [[inf]*N for _ in range(N)]

for j in range(N):
    q = []
    heappush(q, (0, -L, 0, j))
    while q:
        charged, fuel, cost, pos = heappop(q)

        if charged_dict[j][pos] < charged:
            continue

        charged_dict[j][pos] = charged

        for i in d[pos]:
            next_cost = c[pos][i]
            if -fuel-next_cost<0:
                heappush(q, (charged+1, -(L-next_cost), next_cost+cost, i))
            else:
                heappush(q, (charged, -(-fuel-next_cost), next_cost+cost, i))

for s, t in l:
    ans = charged_dict[s-1][t-1]
    print(ans if ans!=inf else -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import defaultdict

N, M, L = map(int, input().split())
inf = 10**20
c = [[inf]*N for _ in range(N)]
d = defaultdict(list)

for _ in range(M):
    A, B, C = map(int, input().split())
    c[A-1][B-1] = C
    c[B-1][A-1] = C
    d[A-1].append(B-1)
    d[B-1].append(A-1)

Q = int(input())
l = [tuple(map(int, input().split())) for _ in range(Q)]
charged_dict = [[inf]*N for _ in range(N)]

for j in range(N):
    q = []
    heappush(q, (0, -L, 0, j))
    while q:
        charged, fuel, cost, pos = heappop(q)

        if charged_dict[j][pos] < charged:
            continue

        charged_dict[j][pos] = charged

        for i in d[pos]:
            next_cost = c[pos][i]
            if -fuel-next_cost<0:
                heappush(q, (charged+1, -(L-next_cost), next_cost+cost, i))
            else:
                heappush(q, (charged, -(-fuel-next_cost), next_cost+cost, i))

for s, t in l:
    ans = charged_dict[s-1][t-1]
    print(ans if ans!=inf else -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import defaultdict

N, M, L = map(int, input().split())
inf = 10**20
c = [[inf]*N for _ in range(N)]
d = defaultdict(list)

for _ in range(M):
    A, B, C = map(int, input().split())
    c[A-1][B-1] = C
    c[B-1][A-1] = C
    d[A-1].append(B-1)
    d[B-1].append(A-1)

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

charged_dict = [[inf]*N for _ in range(N)]

for j in range(N):
    q = []
    heappush(q, (0, -L, 0, j))
    while q:
        charged, fuel, cost, pos = heappop(q)

        if charged_dict[j][pos] < charged:
            continue

        charged_dict[j][pos] = charged

        for i in d[pos]:
            next_cost = c[pos][i]
            if -fuel-next_cost<0:
                heappush(q, (charged+1, -(L-next_cost), next_cost+cost, i))
            else:
                heappush(q, (charged, -(-fuel-next_cost), next_cost+cost, i))

for s, t in l:
    ans = charged_dict[s-1][t-1]
    print(ans if ans!=inf else -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import defaultdict

N, M, L = map(int, input().split())
inf = 10**20
c = [[inf]*N for _ in range(N)]
d = defaultdict(list)

for _ in range(M):
    A, B, C = map(int, input().split())
    c[A-1][B-1] = C
    c[B-1][A-1] = C
    d[A-1].append(B-1)
    d[B-1].append(A-1)

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

charged_dict = [[inf]*N for _ in range(N)]

for j in range(N):
    visited = [False]*N
    q = []
    heappush(q, (0, -L, 0, j))
    while q:
        charged, fuel, cost, pos = heappop(q)

        charged_dict[j][pos] = charged
        visited[pos] = True
        for i in d[pos]:
            if visited[i]:continue

            next_cost = c[pos][i]
            if -fuel-next_cost<0:
                heappush(q, (charged+1, -(L-next_cost), next_cost+cost, i))
            else:
                heappush(q, (charged, -(-fuel-next_cost), next_cost+cost, i))

for s, t in l:
    ans = charged_dict[s-1][t-1]
    print(ans if ans!=inf else -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import defaultdict

N, M, L = map(int, input().split())
inf = 10**20
c = [[inf]*N for _ in range(N)]
d = defaultdict(list)

for _ in range(M):
    A, B, C = map(int, input().split())
    c[A-1][B-1] = C
    c[B-1][A-1] = C
    d[A-1].append(B-1)
    d[B-1].append(A-1)

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

charged_dict = [[inf]*N for _ in range(N)]

for j in range(N):
    visited = [False]*N
    q = []
    heappush(q, (0, -L, 0, j))
    while q:
        charged, fuel, cost, pos = heappop(q)
        print(charged, -fuel)

        charged_dict[j][pos] = charged
        visited[pos] = True
        for i in d[pos]:
            if visited[i]:continue

            next_cost = c[pos][i]
            if -fuel-next_cost<0:
                heappush(q, (charged+1, -(L-next_cost), next_cost+cost, i))
            else:
                heappush(q, (charged, -(-fuel-next_cost), next_cost+cost, i))

for s, t in l:
    ans = charged_dict[s-1][t-1]
    print(ans if ans!=inf else -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import defaultdict

N, M, L = map(int, input().split())
inf = 10**20
c = [[inf]*N for _ in range(N)]
d = defaultdict(list)

for _ in range(M):
    A, B, C = map(int, input().split())
    c[A-1][B-1] = C
    c[B-1][A-1] = C
    d[A-1].append(B-1)
    d[B-1].append(A-1)

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

charged_dict = [[inf]*N for _ in range(N)]

for j in range(N):
    visited = [False]*N
    q = []
    heappush(q, (0, -L, 0, j))
    while q:
        charged, fuel, cost, pos = heappop(q)

        charged_dict[j][pos] = charged
        visited[pos] = True
        for i in d[pos]:
            if visited[i]:continue

            next_cost = c[pos][i]
            if -fuel-next_cost<0:
                heappush(q, (charged+1, -(L-next_cost), next_cost+cost, i))
            else:
                heappush(q, (charged, -(-fuel-next_cost), next_cost+cost, i))

for s, t in l:
    ans = min(charged_dict[s-1][t-1], charged_dict[t-1][s-1])
    print(ans if ans!=inf else -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import defaultdict

N, M, L = map(int, input().split())
inf = 10**20
c = [[inf]*N for _ in range(N)]
d = defaultdict(list)

for _ in range(M):
    A, B, C = map(int, input().split())
    c[A-1][B-1] = C
    c[B-1][A-1] = C
    d[A-1].append(B-1)
    d[B-1].append(A-1)

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

charged_dict = [[inf]*N for _ in range(N)]

for j in range(N):
    visited = [False]*N
    q = []
    heappush(q, (0, j, L, 0))
    while q:
        cost, pos, fuel, charged = heappop(q)
        charged_dict[j][pos] = charged
        visited[pos] = True
        for i in d[pos]:
            if visited[i]:continue

            next_cost = c[pos][i]
            if fuel-next_cost<0:
                heappush(q, (next_cost+cost, i, L-next_cost, charged+1))
            else:
                heappush(q, (next_cost+cost, i, fuel-next_cost, charged))

for s, t in l:
    ans = min(charged_dict[s-1][t-1], charged_dict[t-1][s-1])
    print(ans if ans!=inf else -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import defaultdict

N, M, L = map(int, input().split())
inf = 10**20
c = [[inf]*N for _ in range(N)]
d = defaultdict(list)

for _ in range(M):
    A, B, C = map(int, input().split())
    c[A-1][B-1] = C
    c[B-1][A-1] = C
    d[A-1].append(B-1)
    d[B-1].append(A-1)

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

charged_dict = [[0]*N for _ in range(N)]

for j in range(N):
    visited = [False]*N
    q = []
    heappush(q, (0, j, L, 0))
    while q:
        cost, pos, fuel, charged = heappop(q)
        charged_dict[j][pos] = charged
        visited[pos] = True
        for i in d[pos]:
            if visited[i]:continue

            next_cost = c[pos][i]
            if fuel-next_cost<0:
                heappush(q, (next_cost+cost, i, L-next_cost, charged+1))
            else:
                heappush(q, (next_cost+cost, i, fuel-next_cost, charged))

for s, t in l:
    print(charged_dict[s-1][t-1])

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import defaultdict

N, M, L = map(int, input().split())
inf = 10**20
c = [[inf]*N for _ in range(N)]
d = defaultdict(list)

for _ in range(M):
    A, B, C = map(int, input().split())
    c[A-1][B-1] = C
    c[B-1][A-1] = C
    d[A-1].append(B-1)
    d[B-1].append(A-1)

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

for s, t in l:
    visited = [False]*N
    q = []
    heappush(q, (0, s-1, L, 0))
    while q:
        cost, pos, fuel, charged = heappop(q)
        visited[pos] = True
        if pos==t-1:
            break
        for i in d[pos]:
            if visited[i]:continue

            next_cost = c[pos][i]
            if fuel-next_cost<0:
                heappush(q, (next_cost+cost, i, L-next_cost, charged+1))
            else:
                heappush(q, (next_cost+cost, i, fuel-next_cost, charged))
    print(charged if pos==t-1 else -1)

Submission status: Wrong Answer

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 i == j: continue
            for k in range(len(rd[j])):
                d = rd[j][k]
                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
                    ZO[d][i] = nzo
                    NP[d][i] = 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: Wrong Answer

Solution code:
N, M, L = map(int,input().split())
A = []
B = []
C = []

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:
        A.append(a)
        B.append(b)
        C.append(c)

        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):
        for j in range(i+1,N+1):
            for k in range(len(rd[j])):
                d = rd[j][k]
                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
                    ZO[d][i] = nzo
                    NP[d][i] = 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: Wrong Answer

Solution code:
n,m,l = map(int,input().split())
abe = [list(map(int,input().split())) for i in range(m)]
dist = [[10**18 for i in range(n)] for i in range(n)]
for a,b,e in abe:
  dist[a-1][b-1] = e
  dist[b-1][a-1] = e
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])
for i in range(n):
  for j in range(n):
    if dist[i][j] <= l:
      dist[i][j] = 1
    else:
      dist[i][j] = 10**18
for arr in dist:
  print(*arr)
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 i in range(q):
  s,t = map(int,input().split())
  x = dist[s-1][t-1]-1
  if x == 10**18-1:
    print(-1)
  else:
    print(x)

Submission status: Wrong Answer

Solution code:
n,m,l = map(int,input().split())
abe = [list(map(int,input().split())) for i in range(m)]
dist = [[10**18 for i in range(n)] for i in range(n)]
for a,b,e in abe:
  dist[a-1][b-1] = e
  dist[b-1][a-1] = e
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])
for i in range(n):
  for j in range(n):
    if dist[i][j] <= l:
      dist[i][j] = 1
    else:
      dist[i][j] = 10**18
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 i in range(q):
  s,t = map(int,input().split())
  x = dist[s-1][t-1]-1
  if x == 10**18:
    print(-1)
  else:
    print(dist[s-1][t-1]-1)

Submission status: Wrong Answer

Solution code:
n,m,l = map(int,input().split())
abe = [list(map(int,input().split())) for i in range(m)]
dist = [[10**18 for i in range(n)] for i in range(n)]
for a,b,e in abe:
  dist[a-1][b-1] = e
  dist[b-1][a-1] = e
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])
for i in range(n):
  for j in range(n):
    if dist[i][j] <= l:
      dist[i][j] = 1
    else:
      dist[i][j] = 10**18
for arr in dist:
  print(*arr)
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 i in range(q):
  s,t = map(int,input().split())
  x = dist[s-1][t-1]-1
  if x == 10**18:
    print(-1)
  else:
    print(dist[s-1][t-1]-1)

Submission status: Wrong Answer

Solution code:
n,m,l = map(int,input().split())
abe = [list(map(int,input().split())) for i in range(m)]
dist = [[10**18 for i in range(n)] for i in range(n)]
for a,b,e in abe:
  dist[a-1][b-1] = e
  dist[b-1][a-1] = e
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])
for i in range(n):
  for j in range(n):
    if dist[i][j] <= l:
      dist[i][j] = 1
for arr in dist:
  print(*arr)
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 i in range(q):
  s,t = map(int,input().split())
  print(dist[s-1][t-1]-1)

Submission status: Wrong Answer

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

N,M,L = map(int,input().split())
G = [[] for i in range(N)]

for i in range(M):
	a,b,c = map(lambda x:int(x)-1,input().split())
	if L<c:
		continue
	G[a].append([b,c+1])
	G[b].append([a,c+1])

Q = int(input())
s = [0]*Q
t = [0]*Q
for i in range(Q):
	s[i],t[i] = map(lambda x:int(x)-1,input().split())

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

def dfs(p,x,energy,cnt):

	global ANS
	if ANS[s_cur][x]==-1:
		ANS[s_cur][x]=cnt
	else:
		ANS[s_cur][x] = min(ANS[s_cur][x],cnt)

	for i,c in G[x]:
		if i==p:
			continue
		if energy-c<0:
			energy = L
			cnt += 1
		dfs(x,i,energy-c, cnt)

for i in range(N):
	s_cur = i
	dfs(-1,s_cur, L,0)


for s_cur, t_cur in zip(s,t):
	print(ANS[s_cur][t_cur])

Submission status: Wrong Answer

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

N,M,L = map(int,input().split())
G = [[] for i in range(N)]

for i in range(M):
	a,b,c = map(lambda x:int(x)-1,input().split())
	if L<c:
		continue
	G[a].append([b,c+1])
	G[b].append([a,c+1])

Q = int(input())
s = [0]*Q
t = [0]*Q
for i in range(Q):
	s[i],t[i] = map(lambda x:int(x)-1,input().split())


def dfs(p,x,energy,cnt):
	if x==t_cur:
		global ans
		if ans==-1:
			ans=cnt
		else:
			ans = min(ans,cnt)
		return
	for i,c in G[x]:
		if i==p:
			continue
		if energy-c<0:
			energy = L
			cnt += 1
		dfs(x,i,energy-c, cnt)



for s_cur, t_cur in zip(s,t):
	ans = -1
	dfs(-1, s_cur, L, 0)
	print(ans)

Submission status: Wrong Answer

Solution code:
import sys


INF = 1 << 32 - 1
S = sys.stdin.readlines()
N, M, L = map(int, S[0].split())

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

for i in range(1, M + 1):
    A, B, C = map(int, S[i].split())
    dp[A - 1][B - 1] = C
    dp[B - 1][A - 1] = C
for i in range(N):
    dp[i][i] = 0

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])


dp2 = [[INF] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if dp[i][j] <= L:
            dp2[i][j] = 1
# for i in range(N):
#     dp2[i][i] = 0

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


Q = int(S[M + 1])
for i in range(M + 2, M + 2 + Q):
    s, t = map(int, S[i].split())
    refuel = dp2[s - 1][t - 1] - 1
    if refuel >= INF:
        print(-1)
    else:
        print(refuel)

Submission status: Wrong Answer

Solution code:
import sys


INF = 10 ** 6
S = sys.stdin.readlines()
N, M, L = map(int, S[0].split())

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

for i in range(1, M + 1):
    A, B, C = map(int, S[i].split())
    dp[A - 1][B - 1] = C
    dp[B - 1][A - 1] = C
for i in range(N):
    dp[i][i] = 0

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])


dp2 = [[INF] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if dp[i][j] <= L:
            dp2[i][j] = 1
# for i in range(N):
#     dp2[i][i] = 0

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


Q = int(S[M + 1])
for i in range(M + 2, M + 2 + Q):
    s, t = map(int, S[i].split())
    refuel = dp2[s - 1][t - 1] - 1
    if refuel >= INF:
        print(-1)
    else:
        print(refuel)

Submission status: Wrong Answer

Solution code:
import sys


INF = 10 ** 6
S = sys.stdin.readlines()
N, M, L = map(int, S[0].split())

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

for i in range(1, M + 1):
    A, B, C = map(int, S[i].split())
    dp[A - 1][B - 1] = C
    dp[B - 1][A - 1] = C
for i in range(N):
    dp[i][i] = 0

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])


dp2 = [[INF] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if dp[i][j] <= L:
            dp2[i][j] = 1
for i in range(N):
    dp2[i][i] = 0

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


Q = int(S[M + 1])
for i in range(M + 2, M + 2 + Q):
    s, t = map(int, S[i].split())
    refuel = dp2[s - 1][t - 1] - 1
    if refuel >= INF:
        print(-1)
    else:
        print(refuel)

Submission status: Wrong Answer

Solution code:
import sys


INF = 10 ** 6
S = sys.stdin.readlines()
N, M, L = map(int, S[0].split())

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

for i in range(1, M + 1):
    A, B, C = map(int, S[i].split())
    dp[A - 1][B - 1] = C
    dp[B - 1][A - 1] = C
for i in range(N):
    dp[i][i] = 0

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])


dp2 = [[INF] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if dp[i][j] <= L:
            dp2[i][j] = 1
for i in range(N):
    dp2[i][i] = 0

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


Q = int(S[M + 1])
for i in range(M + 2, M + 2 + Q):
    s, t = map(int, S[i].split())
    refuel = dp2[s - 1][t - 1] - 1
    if refuel > 10 ** 3:
        print(-1)
    else:
        print(refuel)

Submission status: Wrong Answer

Solution code:
import sys


INF = 10 ** 5
S = sys.stdin.readlines()
N, M, L = map(int, S[0].split())

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

for i in range(1, M + 1):
    A, B, C = map(int, S[i].split())
    dp[A - 1][B - 1] = C
    dp[B - 1][A - 1] = C
for i in range(N):
    dp[i][i] = 0

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])


dp2 = [[INF] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if dp[i][j] <= L:
            dp2[i][j] = 1
for i in range(N):
    dp2[i][i] = 0

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


Q = int(S[M + 1])
for i in range(M + 2, M + 2 + Q):
    s, t = map(int, S[i].split())
    refuel = dp2[s - 1][t - 1] - 1
    if refuel > N * 10:
        print(-1)
    else:
        print(refuel)

Submission status: Wrong Answer

Solution code:
import sys


INF = 10 ** 5
S = sys.stdin.readlines()
N, M, L = map(int, S[0].split())

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

for i in range(1, M + 1):
    A, B, C = map(int, S[i].split())
    dp[A - 1][B - 1] = C
    dp[B - 1][A - 1] = C
for i in range(N):
    dp[i][i] = 0

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])


dp2 = [[INF] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if dp[i][j] <= L:
            dp2[i][j] = 1
for i in range(N):
    dp2[i][i] = 0

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


Q = int(S[M + 1])
for i in range(M + 2, M + 2 + Q):
    s, t = map(int, S[i].split())
    refuel = dp2[s - 1][t - 1] - 1
    if refuel > N:
        print(-1)
    else:
        print(refuel)

Submission status: Wrong Answer

Solution code:
import sys


INF = 10 ** 3
S = sys.stdin.readlines()
N, M, L = map(int, S[0].split())

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

for i in range(1, M + 1):
    A, B, C = map(int, S[i].split())
    dp[A - 1][B - 1] = C
    dp[B - 1][A - 1] = C
for i in range(N):
    dp[i][i] = 0

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])


dp2 = [[INF] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if dp[i][j] <= L:
            dp2[i][j] = 1


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


Q = int(S[M + 1])
for i in range(M + 2, M + 2 + Q):
    s, t = map(int, S[i].split())
    refuel = dp2[s - 1][t - 1] - 1
    if refuel > N:
        print(-1)
    else:
        print(refuel)