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:

inf = 10**6
n,m,l = map(int,input().split())

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

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)
P = [[0] * n for _ in range(n)]

for i in range(n):
    for j in range(n):
        if i == j:
            P[i][j] = 0
        elif G[i][j] <= l:
            P[i][j] = 1
        else:
            P[i][j] = inf

p = warshall_floyd(P)
q = int(input())

for _ in range(q):
    s,t = map(int,input().split())
    ans = p[s-1][t-1]-1
    print(ans if ans <= 10**5 else -1)

Submission status: Wrong Answer

Solution code:
INF = float("inf")
n, m, l = list(map(int, input().split()))
edge1 = [[0 if i == j else INF for i in range(n)] for j in range(n)]

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

# for row in edge1:
#     print(row)

q = int(input())
queries = []

for _ in range(q):
    s, t = map(int, input().split())
    if s < t:
        queries.append((s - 1, t - 1))
    else:
        queries.append((t - 1, s - 1))

# shortest paths by Warshall Floyd
for k in range(n):  # through
    for i in range(n):  # from
        for j in range(n):  # to
            edge1[i][j] = min(edge1[i][j], edge1[i][k] + edge1[k][j])

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

# shortest costs by Warshall Floyd
for k in range(n):  # through
    for i in range(n):  # from
        for j in range(n):  # to
            edge2[i][j] = min(edge2[i][j], edge2[i][k] + edge2[k][j])

# for row in edge2:
#     print(row)

for (s, t) in queries:
    # print(s, t)
    # print(edge2[s][t])
    print(edge2[s][t] - 1 if edge2[s][t] < INF else -1)

Submission status: Wrong Answer

Solution code:
n, m, l = map(int, input().split())
 
edge_lst = [[] * (n+1) for _ in range(n+1)]
for i in range(m):
    a,b,c = map(int ,input().split())
    if c <= l:
        edge_lst[a].append((b,c))
        edge_lst[b].append((a,c))
        
d_matrix = [[(-1, 10**20)] * (n+1) for _ in range(n+1)]
for i in range(n):
    d = [(-1, 10**20)] * (n+1)
    used = [False] * (n+1)
    d[i+1] = (l, 0)
    
    while True:
        v = -1
        for j in range (n):
            if not used[j+1]:
                if v == -1:
                    v = j+1
                else:
                    f, cnt = d[v]
                    if cnt > d[j+1][1]:
                        v = j+1
                    if cnt == d[i+1][1]:
                        if f < d[j+1][0]:
                            v = j+1
        if v == -1:
            break
        used[v] = True
 
        for edge in edge_lst[v]:
            a = v
            b,c = edge
            if d[a][1] != 10**10:
                if d[a][0] < c:
                    cnt = d[a][1] + 1
                    f = l - c
                else:
                    cnt = d[a][1]
                    f = d[a][0] - c
                if d[b][1] == cnt:
                    if d[b][0] < f:
                        d[b] = (f, cnt)
                if d[b][1] > cnt:
                    d[b] = (f, cnt)
    
    d_matrix[i+1] = d
            
 
q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    print(d_matrix[s][t][1]) if d_matrix[s][t][1] < 10**20 else print(-1)

Submission status: Wrong Answer

Solution code:
import copy

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

edge_lst = [[10**10] * (n+1) for _ in range(n+1)]
for i in range(m):
    a,b,c = map(int ,input().split())
    edge_lst[a][b] = c
    edge_lst[b][a] = c
for i in range(n):
    edge_lst[i+1][i+1] = 0

d = copy.deepcopy(edge_lst)
for i in range(n):
    for j in range(n):
        for k in range(n):
            d[i+1][j+1] = min(d[i+1][j+1], d[i+1][k+1] + d[k+1][j+1])

new_edge_lst = [[10**10] * (n+1) for _ in range(n+1)]
for i in range(n):
    for j in range(n):
        if d[i+1][j+1] <= l and i != j:
            new_edge_lst[i+1][j+1] = 1
for i in range(n):
    new_edge_lst[i+1][i+1] = 0

new_d = copy.deepcopy(new_edge_lst)
for i in range(n):
    for j in range(n):
        for k in range(n):
            new_d[i+1][j+1] = min(new_d[i+1][j+1], new_d[i+1][k+1] + new_d[k+1][j+1])

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

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,INF)]*n for _ in range(n)]
    for i in range(n):
        A[i][i]=(0,0)
    for _ in range(m):
        a,b,c=map(int,input().split())
        if(c>L): continue
        a-=1; b-=1
        A[a][b]=(0,c)
        A[b][a]=(0,c)
    # Warshall-Floyd
    from itertools import product
    for k,i,j in product(range(n),repeat=3):
        first=A[i][k]
        second=A[k][j]
        rev_second=A[j][k]
        if(first[1]+rev_second[1]<=L):
            next=(first[0]+second[0],first[1]+second[1])
        else:
            next=(first[0]+second[0]+1,second[1])
        if(A[i][j]>next):
            A[i][j]=next
    # output
    Q=int(input())
    for _ in range(Q):
        s,t=map(int,input().split())
        s-=1; t-=1
        ans=A[s][t][0]
        print(ans if ans!=INF else -1)
resolve()

Submission status: Wrong Answer

Solution code:
n, m, l = map(int, input().split())

edge_lst = [[] * (n+1) for _ in range(n+1)]
for i in range(m):
    a,b,c = map(int ,input().split())
    if c <= l:
        edge_lst[a].append((b,c))
        edge_lst[b].append((a,c))
        
d_matrix = [[(-1, 10**10)] * (n+1) for _ in range(n+1)]
for i in range(n):
    d = [(-1, 10**10)] * (n+1)
    used = [False] * (n+1)
    d[i+1] = (l, 0)
    
    while True:
        v = -1
        for j in range (n):
            if not used[j+1]:
                if v == -1:
                    v = j+1
                else:
                    f, cnt = d[v]
                    if cnt > d[j+1][1]:
                        v = j+1
                    if cnt == d[i+1][1]:
                        if f < d[j+1][0]:
                            v = j+1
        if v == -1:
            break
        used[v] = True

        for edge in edge_lst[v]:
            a = v
            b,c = edge
            if d[a][1] != 10**10:
                if d[a][0] < c:
                    cnt = d[a][1] + 1
                    f = l - c
                else:
                    cnt = d[a][1]
                    f = d[a][0] - c
                if d[b][1] == cnt:
                    if d[b][0] < f:
                        d[b] = (f, cnt)
                if d[b][1] > cnt:
                    d[b] = (f, cnt)
    
    d_matrix[i+1] = d
            

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

Submission status: Wrong Answer

Solution code:
n, m, l = map(int, input().split())

edge_lst = [[] * (n+1) for _ in range(n+1)]
for i in range(m):
    a,b,c = map(int ,input().split())
    if c <= l:
        edge_lst[a].append((b,c))
        edge_lst[b].append((a,c))
        
d_matrix = [[(-1, 10**10)] * (n+1) for _ in range(n+1)]
for i in range(n):
    d = [(-1, 10**10)] * (n+1)
    used = [False] * (n+1)
    d[i+1] = (l, 0)
    
    while True:
        v = -1
        for j in range (n):
            if not used[j+1]:
                if v == -1:
                    v = j+1
                else:
                    f, cnt = d[v]
                    if cnt > d[j+1][1]:
                        v = j+1
                    if cnt == d[i+1][1]:
                        if f < d[j+1][0]:
                            v = j+1    
        used[v] = True
        if v == -1:
            break

        for edge in edge_lst[v]:
            a = v
            b,c = edge
            if d[a][1] != 10**10:
                if d[a][0] < c:
                    cnt = d[a][1] + 1
                    f = l - c
                else:
                    cnt = d[a][1]
                    f = d[a][0] - c
                if d[b][1] == cnt:
                    if d[b][0] < f:
                        d[b] = (f, cnt)
                if d[b][1] > cnt:
                    d[b] = (f, cnt)
    
    d_matrix[i+1] = d
            

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

Submission status: Wrong Answer

Solution code:
n, m, l = map(int, input().split())

edge_lst = [[(0,0)] * (n+1) for _ in range(n+1)]
for i in range(m):
    a,b,c = map(int ,input().split())
    if c <= l:
        edge_lst[a].append((b,c))
        edge_lst[b].append((a,c))
        
d_matrix = [[(-1, 10**10)] * (n+1) for _ in range(n+1)]
for i in range(n):
    d = [(-1, 10**10)] * (n+1)
    used = [False] * (n+1)
    d[i+1] = (l, 0)
    
    while True:
        v = -1
        for j in range (n):
            if not used[j+1]:
                if v == -1:
                    v = j+1
                else:
                    f, cnt = d[v]
                    if cnt > d[j+1][1]:
                        v = j+1
                    if cnt == d[i+1][1]:
                        if f < d[j+1][0]:
                            v = j+1    
        used[v] = True
        if v == -1:
            break

        for edge in edge_lst[v]:
            a = v
            b,c = edge
            if d[a][1] != 10**10:
                if d[a][0] < c:
                    cnt = d[a][1] + 1
                    f = l - c
                else:
                    cnt = d[a][1]
                    f = d[a][0] - c
                if d[b][1] == cnt:
                    if d[b][0] < f:
                        d[b] = (f, cnt)
                if d[b][1] > cnt:
                    d[b] = (f, cnt)
    
    d_matrix[i+1] = d
            

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

Submission status: Wrong Answer

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

edge_lst = []
for abc in abc_lst:
    a,b,c = abc
    if c <= l:
        edge_lst.append((a,b,c))
        edge_lst.append((b,a,c))
        
d_matrix = [[(-1, 10**10)] * (n+1) for _ in range(n+1)]
for i in range(n):
    d = [(-1, 10**10)] * (n+1)
    d[i+1] = (l, 0)
    while True:
        update = False
        for abc in edge_lst:
            a, b, c = abc
            if d[a][1] != 10**10:
                if d[a][0] < c:
                    cnt = d[a][1] + 1
                    f = l - c
                else:
                    cnt = d[a][1]
                    f = d[a][0] - c
                if d[b][1] == cnt:
                    if d[b][0] > f:
                        d[b] = (f, cnt)
                        update = True
                if d[b][1] > cnt:
                    d[b] = (f, cnt)
                    update = True
        if not update:
            break
        d_matrix[i+1] = d

for q in q_lst:
    s, t = q
    print(d_matrix[s][t][1]) if d_matrix[s][t][1] != 10**10 else print(-1)

Submission status: Wrong Answer

Solution code:
def froyd(n, d):
    for k in range(n):
        for s in range(n):
            # ds = d[s]
            for t in range(n):
                d[s][t] = min(d[s][t], d[s][k] + d[k][t])


def main():
    N, M, L = [int(x) for x in input().split()]
    INF = float("inf")
    A = [[] for _ in range(N)]
    B = [[] for _ in range(N)]
    for i in range(N):
        A[i] = [INF] * N
        A[i][i] = 0
        B[i] = [INF] * N
        B[i][i] = 0
    for i in range(M):
        a, b, c = [int(x) for x in input().split()]
        A[a - 1][b - 1] = c
        A[b - 1][a - 1] = c
    froyd(N, A)
    # print(A)
    for s in range(N):
        for t in range(N):
            B[s][t] = 0 if s == t else 1 if A[s][t] <= L else INF
    # print(B)
    froyd(N, B)
    # print(B)

    Q = int(input())
    for _ in range(Q):
        s, t = [int(x) for x in input().split()]
        print(B[s - 1][t - 1] - 1)


main()

Submission status: Wrong Answer

Solution code:
from scipy.sparse import*
f=csgraph.johnson
n,m,l,*t=map(int,open(0).read().split())
m*=3
print(*f(f(csr_matrix((t[2:m:3],(t[:m:3],t[1:m:3])),[n+1]*2),0)<=l)[t[m+1::2],t[m+2::2]].clip(0,n)%n-1)

Submission status: Wrong Answer

Solution code:
N,M,L = map(int, input().split())
A = [0]*M
B = [0]*M
C = [0]*M
for i in range(M):
    A[i], B[i], C[i] = map(int, input().split())
    
Q = int(input())
qlist = [[int(q) for q in input().split()] for _ in range(Q)]

INF = 10**13
d = [[INF]*N for _ in range(N)]
for i in range(M):
    d[A[i]-1][B[i]-1] = C[i]
    d[B[i]-1][A[i]-1] = C[i]

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

# Warshall-Floyd法
# DP
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])

d2 = [[INF]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            d2[i][j] = 0
        elif d[i][j] <= L:
            d2[i][j] = 1
            
for i in range(N):
    for j in range(N):
        for k in range(N):
            d2[i][j] = min(d2[i][j], d2[i][k]+d2[k][j])
                
for i in range(Q):
    s = qlist[i][0]-1
    t = qlist[i][1]-1
    ans = d2[s][t]
    if ans >= INF:
        print(-1)
    else:
        print(ans-1)

Submission status: Wrong Answer

Solution code:
N,M,L = map(int, input().split())
A = [0]*M
B = [0]*M
C = [0]*M
for i in range(M):
    A[i], B[i], C[i] = map(int, input().split())
    
Q = int(input())
qlist = [[int(q) for q in input().split()] for _ in range(Q)]

INF = 10**15
d = [[INF]*N for _ in range(N)]
for i in range(M):
    d[A[i]-1][B[i]-1] = C[i]
    d[B[i]-1][A[i]-1] = C[i]
        

# Warshall-Floyd法
# DP
for k in range(N):
    for i in range(N):
        for j in range(N):
            if i != j:
                d[i][j] = min(d[i][j], d[i][k]+d[k][j])

d2 = [[INF]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if d[i][j] <= L:
            d2[i][j] = 1
            
for i in range(N):
    for j in range(N):
        for k in range(N):
            if i != j:
                d2[i][j] = min(d2[i][j], d2[i][k]+d2[k][j])
                
for i in range(Q):
    s = qlist[i][0]-1
    t = qlist[i][1]-1
    ans = d2[s][t]
    if ans == INF:
        print(-1)
    else:
        print(ans-1)

Submission status: Wrong Answer

Solution code:
from scipy.sparse import*
f=csgraph.johnson
n,m,l,*t=map(int,open(0).read().split())
m*=3
print(*f(f(csr_matrix((t[2:m:3],(t[:m:3],t[1:m:3])),[n+1]*2),0)<=l)[t[m+1::2],t[m+2::2]]%n-1,sep='\n')

Submission status: Wrong Answer

Solution code:
S = raw_input()
S = S.split()
n,m,l = int(S[0]),int(S[1]),int(S[2])
MAX = int(1e15)
G = []
for i in xrange(n):
    temp=[]
    for j in xrange(n):
        temp.append(MAX)
    G.append(temp)
for i in xrange(n):
    G[i][i]=0

for i in xrange(m):
    S = raw_input()
    S = S.split()
    a,b,c = int(S[0]),int(S[1]),int(S[2])
    a-=1
    b-=1
    G[a][b]=c
    G[b][a]=c
for i in xrange(n):
    for j in xrange(n):
        for k in xrange(n):
            val = G[i][k]+G[k][j]
            G[i][j] = min(G[i][j],val)
dd = G
for i in xrange(n):
    for j in xrange(n):
        if(dd[i][j]<=l):
            G[i][j]=1
        else :
            G[i][j]=MAX
        if(i==j):
            G[i][j]=0

for i in xrange(n):
    for j in xrange(n):
        for k in xrange(n):
            val = G[i][k]+G[k][j]
            G[i][j] = min(G[i][j],val)
#print(G)
q = input()
while(q):
    q-=1
    S = raw_input()
    S =S.split()
    s,t = int(S[0]),int(S[1])
    s-=1
    t-=1
    p = G[s][t]
    if(p>=MAX):
        print -1
    else:
        print p-1

Submission status: Wrong Answer

Solution code:
from scipy.sparse import*
f=csgraph.johnson
n,m,l,*t=map(int,open(0).read().split())
m*=3
d=f(f(csr_matrix((t[2:m:3],(t[:m:3],t[1:m:3])),[n+1]*2),0)<=l)
print(*d[t[m+1::2],t[m+2::2]]-1,sep='\n')

Submission status: Wrong Answer

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

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

distance = [[float('inf')]*N for _ in range(N)]
for i in range(N):
    distance[i][i] = 0
for _ in range(M):
    a, b, c = map(int, input().split())
    distance[a-1][b-1] = c
    distance[b-1][a-1] = c

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

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

Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    ans = stoke[s-1][t-1]-1
    if ans is float('inf'):
        ans = -1
    print(ans)

Submission status: Wrong Answer

Solution code:
from scipy.sparse import*
f=csgraph.johnson
n,m,l,*t=map(int,open(0).read().split())
m*=3
d=f(f(csr_matrix((t[2:m:3],(t[:m:3],t[1:m:3])),[n+1]*2),0)<=l)
d[d>n]=0
print(*d[t[m+1::2],t[m+2::2]]-1)

Submission status: Wrong Answer

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

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

distance = [[float('inf')]*N for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    distance[a-1][b-1] = c
    distance[b-1][a-1] = c

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

stoke = [[1 if distance[i][j] <= L else float('inf') 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):
            stoke[i][j] = min(stoke[i][j], stoke[i][k]+stoke[k][j])

Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    ans = stoke[s-1][t-1]-1
    if ans is float('inf'):
        ans = -1
    print(ans)

Submission status: Wrong Answer

Solution code:
# import os
# import sys
# sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
# from lib import pytime

# start = pytime.pytime('start')

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

town = []
cost = []
query = []

for _ in range(n):
    town.append([])
    cost.append([])

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

q = int(input())

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

# print(town)
# print(cost)
# print(query)

def travel(source,destination,li,litre,cnt):
    flg[source] = 1
    if source == destination:
        tmp.append(cnt)
    else:
        for i in range(len(town[source])):
            d = town[source][i]
            dc = cost[source][i]
            # print('source{}'.format(source))
            # print('des{}'.format(destination))
            # print('d{}'.format(d))
            # print('litre{}'.format(litre))
            # print('cnt{}'.format(cnt))
            # print('town{}'.format(town[source]))
            # print('flg{}'.format(flg))
            # print('tmp{}'.format(tmp))
            if dc >= li or flg[d] == 1:
                # print('x')
                pass
            elif dc > litre:
                # print('y')
                travel(d,destination,li,li - dc,cnt + 1)
            elif dc <= litre:
                # print('z')
                travel(d,destination,li,litre - dc,cnt)
    return

for i in range(q):
    flg = [0 for _ in range(n)]
    tmp = []
    travel(query[i][0],query[i][1],l,l,0)
    if tmp:
        print(min(tmp))
    else:
        print(-1)

# pytime.pytime('end',start)

Submission status: Wrong Answer

Solution code:
n, m, l = map(int, input().split())
INF = float("inf")

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

d = [[INF for i in range(n)] for j in range(n)]
for i in range(m):
    a, b, c = map(int, input().split())
    d[a-1][b-1] = c
d = warshall_floyd(d, n)

d2 = [[INF for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(i+1, n):
        if d[i][j] <= l: d2[i][j] = 1
d2 = warshall_floyd(d2, n)

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

Submission status: Wrong Answer

Solution code:
N, M, L = map(int, input().split())
A, B, C = [], [], []
for _ in range(M):
    a, b, c = map(int, input().split())
    if c > L:
        continue
    A.append(a)
    B.append(b)
    C.append(c)
Q = int(input())
S, T = [], []
for _ in range(Q):
    s, t = map(int, input().split())
    S.append(s)
    T.append(t)


def dijkstra(s):
    resolved = {s}
    unresolved = set(range(1, N+1)) - resolved
    min_distance = {s: 0}
    while True:
        resolved_tmp = set()
        for a, b, c in zip(A, B, C):
            if a in resolved and b in unresolved:
                try:
                    if min_distance[b] > min_distance[a] + c:
                        min_distance[b] = min_distance[a] + c
                except:
                    min_distance[b] = min_distance[a] + c
                    resolved_tmp.add(b)
            elif b in resolved and a in unresolved:
                try:
                    if min_distance[a] > min_distance[b] + c:
                        min_distance[a] = min_distance[b] + c
                except:
                    min_distance[a] = min_distance[b] + c
                    resolved_tmp.add(a)
        if resolved_tmp == set():
            break
        resolved = resolved ^ resolved_tmp
        unresolved = unresolved - resolved_tmp

    return min_distance
            

min_distance = {}
for s in set(S):
    min_distance[s] = dijkstra(s)

C = [1 for _ in C]
for s in min_distance.keys():
    for t in min_distance[s].keys():
        if min_distance[s][t] <= L:
            A.append(s)
            B.append(t)
            C.append(1)

min_distance = {}
for s in set(S):
    min_distance[s] = dijkstra(s)


# answer
for s, t in zip(S, T):
    if t not in min_distance[s].keys():
        print(-1)
    else:
        print(min_distance[s][t] - 1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
#import math
#import time
#import random  # randome is not available at Codeforces
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
#eps=10**(-10)
#ts=time.time()
#sys.setrecursionlimit(10**6)
input=lambda: sys.stdin.readline().rstrip()

show_flg=False
show_flg=True

def wf(d):
    n=len(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


n,m,l=MI()
d=[[inf]*n for _ in range(n)]
for i in range(m):
    a,b,c=LI_()
    d[a][b]=c+1
    d[b][a]=c+1

d=wf(d)
g=[[inf]*n for i in range(n)]

for i in range(n):
    for j in range(i+1,n):
        if d[i][j]<=l:
            g[i][j]=1
            g[j][i]=1
g=wf(g)

q=I()
for _ in range(q):
    s,t=LI_()
    print(g[s][t]-1)

Submission status: Wrong Answer

Solution code:
def warshall_floyd(d):
    for k in range(len(d)):
        for i in range(len(d)):
            for j in range(len(d)):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
    return d


INF = 10**9 + 1

if __name__ == "__main__":
    N, M, L = map(int, input().split())
    D = [[INF] * N for _ in range(N)]
    for i in range(N):
        D[i][i] = 0
    for i in range(M):
        tmp = list(map(int, input().split()))
        D[tmp[0]-1][tmp[1]-1] = tmp[2]
        D[tmp[1]-1][tmp[0]-1] = tmp[2]
    Q = int(input())
    st = list()
    for i in range(Q):
        st.append(list(map(int, input().split())))

    D = warshall_floyd(D)

    for i in range(N):
        for j in range(N):
            if i == j:
                D[i][j] = 0
            elif D[i][j] <= L:
                D[i][j] = 1
            else:
                D[i][j] = INF

    D = warshall_floyd(D)

    for i in range(Q):
        ans = D[st[i][0]-1][st[i][1]-1] - 1
        print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import *

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

d = [[] for _ in range(N)]
for a, b, c in ABC:
    d[a-1].append((b-1, c))
    d[b-1].append((a-1, c))

# print(d)

# スタートをiにしてdijkstra * N
a_to_b = []
for i in range(N):
    # (ここまでの燃料補給回数, 使った燃料(L-残り燃料))
    i_to_b = [(10**24, 10**24) for _ in range(N)]
    i_to_b[i] = (0, 0)

    visited = [0]*N
    
    q = [(i_to_b[i], i)]
    while q:
        # print(q)
        (_, _), ti = heappop(q)
        tb, tm = i_to_b[ti]

        for ni, nm in d[ti]:
            if visited[ni] == 2:
                continue
            if tm+nm <= L:
                n_bm = (tb, tm+nm)
            elif nm <= L:
                n_bm = (tb+1, nm)
            else:
                n_bm = (10**24, 10**24)
            # print(i, (ni, ), (tb, tm), n_bm, i_to_b[ni])
            i_to_b[ni] = min(i_to_b[ni], n_bm)
            if visited[ni] == 0:
                heappush(q, (i_to_b[ni], ni))
                visited[ni] = 1
            # print(i_to_b)
        visited[ti] = 2
    a_to_b.append(i_to_b)
    # print(i_to_b)

# print(a_to_b)

for s, t in ST:
    r = a_to_b[s-1][t-1][0]
    print(-1 if r == 10**24 else r)

Submission status: Wrong Answer

Solution code:
import sys
from scipy.sparse.csgraph import floyd_warshall as f
def main():
  n,m,l,*t=map(int,sys.stdin.buffer.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
  d=d.astype('i1').astype('U')
  print('\n'.join(d[i]for i in zip(*[iter(t[m*3+1:])]*2)))
main()

Submission status: Wrong Answer

Solution code:
from collections import deque

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

d = [[] for _ in range(N)]
for a, b, c in ABC:
    d[a-1].append((b-1, c))
    d[b-1].append((a-1, c))

# print(d)

# スタートをiにしてdijkstra * N
a_to_b = []
for i in range(N):
    # (ここまでの燃料補給回数, L-残り燃料)
    i_to_b = [(10**24, 10**24) for _ in range(N)]
    i_to_b[i] = (0, 0)
    visited = [0]*N
    visited[i] = 1
    q = deque([i])
    while q:
        # print(q)
        ti = q.popleft()
        tb, tm = i_to_b[ti]

        for ni, nm in d[ti]:
            if visited[ni]:
                continue
            if tm+nm <= L:
                n_bm = (tb, tm+nm)
            elif nm <= L:
                n_bm = (tb+1, nm)
            else:
                n_bm = (10**24, 10**24)
            # print((i, ni), (tb, tm), n_bm, i_to_b[ni])
            visited[ni] = 1
            q.append(ni)
            i_to_b[ni] = min(i_to_b[ni], n_bm)
    a_to_b.append(i_to_b)
    # print(i_to_b)

# print(a_to_b)

for s, t in ST:
    r = a_to_b[s-1][t-1][0]
    print(-1 if r == 10**24 else r)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

d = [[] for _ in range(N)]
for a, b, c in ABC:
    d[a-1].append((b-1, c))
    d[b-1].append((a-1, c))

# print(d)

# スタートをiにしてdijkstra * N
a_to_b = []
for i in range(N):
    # (ここまでの燃料補給回数, L-残り燃料)
    i_to_b = [(10**24, 10**24) for _ in range(N)]
    i_to_b[i] = (0, 0)
    visited = [0]*N
    visited[i] = 1
    q = deque([i])
    while q:
        # print(q)
        ti = q.popleft()
        tb, tm = i_to_b[ti]

        for ni, nm in d[ti]:
            if visited[ni]:
                continue
            if tm+nm <= L:
                n_bm = (tb, tm+nm)
            else:
                n_bm = (tb+1, nm)
            # print((i, ni), (tb, tm), n_bm, i_to_b[ni])
            visited[ni] = 1
            q.append(ni)
            i_to_b[ni] = min(i_to_b[ni], n_bm)
    a_to_b.append(i_to_b)
    # print(i_to_b)

# print(a_to_b)

for s, t in ST:
    r = a_to_b[s-1][t-1][0]
    print(-1 if r == 10**24 else r)

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(int(d[i]))for i in zip(*[iter(t[m*3+1:])]*2)))
main()