Problem p02889 (python)

Fixed submission:
import sys
input = sys.stdin.readline

def main():
    N, M, L = map(int, input().split())
    dist = [[float('inf')] * N for _ in range(N)]
    for _ in range(M):
        A, B, C = map(int, input().split())
        if C > L:
            continue
        A -= 1
        B -= 1
        dist[A][B] = C
        dist[B][A] = 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])

    e = [[float('inf')] * N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if dist[i][j] <= L:
                e[i][j] = 1
    
    for k in range(N):
        for i in range(N):
            for j in range(N):
                e[i][j] = min(e[i][j], e[i][k] + e[k][j])


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


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input = sys.stdin.readline
    
    def Warshall_Floyd(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 = map(int, input().split())
    f_inf = float('inf')
    dist = [[f_inf]*n for _ in range(n)]
    for i in range(m): dist[i][i] = 0
    for _ in range(m):
        a, b, c = map(int, input().split())
        dist[a-1][b-1] = c
        dist[b-1][a-1] = c

    Warshall_Floyd(dist)

    dist2 = [[f_inf]*n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            if dist[i][j] <= l:
                dist2[i][j] = 1
    
    Warshall_Floyd(dist2)

    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        if dist2[s-1][t-1] == f_inf:
            print(-1)
        else:
          ans = dist2[s-1][t-1]
          print(ans-1)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input = lambda:sys.stdin.readline().rstrip()
    
    def Warshall_Floyd(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 = map(int, input().split())
    f_inf = float('inf')
    dist = [[f_inf]*n for _ in range(n)]
    for i in range(m): dist[i][i] = 0
    for _ in range(m):
        a, b, c = map(int, input().split())
        dist[a-1][b-1] = c
        dist[b-1][a-1] = c

    Warshall_Floyd(dist)

    dist2 = [[f_inf]*n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            if dist[i][j] <= l:
                dist2[i][j] = 1
    
    Warshall_Floyd(dist2)

    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        if dist2[s-1][t-1] == f_inf:
            print(-1)
        else:
          ans = dist2[s-1][t-1]
          print(ans-1)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input = lambda:sys.stdin.readline().rstrip()
    
    def Warshall_Floyd(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 = map(int, input().split())
    f_inf = float('inf')
    dist = [[f_inf]*n for _ in range(n)]

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

    Warshall_Floyd(dist)

    dist2 = [[f_inf]*n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            if dist[i][j] <= l:
                dist2[i][j] = 1
    Warshall_Floyd(dist2)

    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        ans = dist2[s-1][t-1]
        if dist2[s-1][t-1] == f_inf:
            print(-1)
        else:
            print(ans-1)
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
input = lambda:sys.stdin.readline().rstrip()

n, m, l = map(int, input().split())
f_inf = float('inf')
dist = [[f_inf]*n for _ in range(n)]
for i in range(m): dist[i][i] = 0

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

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


dist2 = [[f_inf]*n for _ in range(n)]
for i in range(n):
    for j in range(n):
        if dist[i][j] <= l:
            dist2[i][j] = 1
for k in range(n):
  for i in range(n):
    for j in range(n):
      dist2[i][j] = min(dist2[i][j], dist2[i][k]+dist2[k][j])

q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    ans = dist2[s-1][t-1]
    if dist2[s-1][t-1] == f_inf:
        print(-1)
    else:
        print(ans-1)

Buggy submission (verdict: Runtime Error):
import sys
input = lambda:sys.stdin.readline().rstrip()

def Warshall_Floyd(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 = map(int, input().split())
f_inf = float('inf')
dist = [[f_inf]*n for _ in range(n)]
for i in range(m): dist[i][i] = 0

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

dist = Warshall_Floyd(dist)

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

dist2 = Warshall_Floyd(dist2)

q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    ans = dist2[s-1][t-1]
    if dist2[s-1][t-1] == f_inf:
        print(-1)
    else:
        print(ans-1)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**9)
input = lambda:sys.stdin.readline().rstrip()

def Warshall_Floyd(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 = map(int, input().split())
f_inf = float('inf')
dist = [[f_inf]*n for _ in range(n)]
for i in range(m): dist[i][i] = 0

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

dist = Warshall_Floyd(dist)

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

dist2 = Warshall_Floyd(dist2)

q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    ans = dist2[s-1][t-1]
    if dist2[s-1][t-1] == f_inf:
        print(-1)
    else:
        print(ans-1)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**9)
input = lambda:sys.stdin.readline().rstrip()

def Warshall_Floyd(n, 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 = map(int, input().split())
f_inf = float('inf')
dist = [[f_inf]*n for _ in range(n)]
for i in range(m): dist[i][i] = 0

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

dist = Warshall_Floyd(n, dist)

dist2 = [[f_inf]*n for _ in range(n)]
for i in range(m): dist2[i][i] = 0
for i in range(n):
    for j in range(n):
        if dist[i][j] <= l:
            dist2[i][j] = 1

dist2 = Warshall_Floyd(n, dist2)

q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    ans = dist2[s-1][t-1]
    if dist2[s-1][t-1] == f_inf:
        print(-1)
    else:
        print(ans-1)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**9)
input = lambda:sys.stdin.readline().rstrip()

def Warshall_Floyd(d):
    #dist[i][j]:iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k]+d[k][j])
    return d
############################
n, m, l = map(int, input().split())
f_inf = float('inf')
dist = [[f_inf]*n for _ in range(n)]
for i in range(m): dist[i][i] = 0

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

dist = Warshall_Floyd(dist)

dist2 = [[f_inf]*n for _ in range(n)]
for i in range(m): dist2[i][i] = 0
for i in range(n):
    for j in range(n):
        if dist[i][j] <= l:
            dist2[i][j] = 1

dist2 = Warshall_Floyd(dist2)

q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    ans = dist2[s-1][t-1]
    if dist2[s-1][t-1] == f_inf:
        print(-1)
    else:
        print(ans-1)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**9)
input = lambda:sys.stdin.readline().rstrip()

def Warshall_Floyd(d):
    #dist[i][j]:iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k]+d[k][j])
    return d
############################
n, m, l = map(int, input().split())
f_inf = float('inf')
dist = [[f_inf]*n for _ in range(n)]
for i in range(m): dist[i][i] = 0

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

dist = Warshall_Floyd(dist)

dist2 = [[f_inf]*n for _ in range(n)]
for i in range(m): dist2[i][i] = 0
for i in range(n):
    for j in range(n):
        if dist[i][j] <= l:
            dist2[i][j] = 1

dist2 = Warshall_Floyd(dist2)

q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    ans = dist2[s-1][t-1]-1
    if dist2[s-1][t-1] == f_inf:
      ans = -1
    print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

def Warshall_Floyd(d):
    #dist[i][j]:iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k]+d[k][j])
    return d
n, m, l = map(int, input().split())

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

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

dist = Warshall_Floyd(dist)

dist2 = [[f_inf]*n for _ in range(n)]
for i in range(m): dist2[i][i] = 0
for i in range(n):
    for j in range(n):
        if dist[i][j] <= l:
            dist2[i][j] = 1

dist2 = Warshall_Floyd(dist2)

q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    ans = dist2[s-1][t-1]-1
    if dist2[s-1][t-1] == f_inf:
      ans = -1
    print(ans)

Buggy submission (verdict: Runtime Error):
def Warshall_Floyd(d):
    #dist[i][j]:iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k]+d[k][j])
    return d
############################
n, m, l = map(int, input().split())

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

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

Warshall_Floyd(dist)

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

Warshall_Floyd(dist2)

q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    ans = dist2[s-1][t-1]-1
    if dist2[s-1][t-1] == f_inf:
      ans = -1
    print(ans)

Buggy submission (verdict: Runtime Error):
def Warshall_Floyd(d):
    #dist[i][j]:iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k]+d[k][j])
    return d
############################
n, m, l = map(int, input().split())

f_inf = float('inf')
dist = [[f_inf for _ in range(n)] for _ in range(n)]
for i in range(m):
    dist[i][i] = 0

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

Warshall_Floyd(dist)

dist2 = [[f_inf for _ in range(n)] for _ in range(n)]
for i in range(n):
    for j in range(n):
        if dist[i][j] <= l:
            dist2[i][j] = 1

Warshall_Floyd(dist2)

q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    ans = dist2[s-1][t-1]-1
    if dist2[s-1][t-1] == f_inf:
      ans = -1
    print(ans)

Buggy submission (verdict: Runtime Error):
def Warshall_Floyd(d):
    #dist[i][j]:iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k]+d[k][j])
    return d
############################
n, m, l = map(int, input().split())

f_inf = float('inf')
dist = [[f_inf for _ in range(n)] for _ in range(n)]
for i in range(m): dist[i][i] = 0

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

Warshall_Floyd(dist)

dist2 = [[f_inf for _ in range(n)] for _ in range(n)]
for i in range(n):
    for j in range(n):
        if dist[i][j] <= l:
            dist2[i][j] = 1

Warshall_Floyd(dist2)

q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    ans = dist2[s-1][t-1] 
    if dist2[s-1][t-1] == f_inf:
      ans = -1
      
print(ans)

Buggy submission (verdict: Runtime Error):
def warshall_floyd(dist):
    #d[i][j]: 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])
    return dist

def main():
    n, m, l = map(int, input().split()) #N:頂点数　M:辺の数 L:容量
    dist = [[float("inf") for i in range(n)] for j in range(n)]
    
    for i in range(n):
        dist[i][i] = 0

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

    warshall_floyd(dist)
    dist2 = [[float("inf") for i in range(n)] for j in range(n)]

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

    warshall_floyd(dist2)
    q = int(input())
    for i in range(q):
        s, t = map(int, input().split())
        ans = dist2[s-1][t-1]-1
        if dist2[s-1][t-1] == float("inf"):
            ans = -1
        print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
def warshall_floyd(dist):
    #d[i][j]: 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])
    return dist

n, m, l = map(int, input().split()) #N:頂点数　M:辺の数 L:容量
dist = [[float("inf") for i in range(n)] for j in range(n)]

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

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

warshall_floyd(dist)
dist2 = [[float("inf") for i in range(n)] for j in range(n)]

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

warshall_floyd(dist2)
q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    ans = dist2[s-1][t-1]-1
    if dist2[s-1][t-1] == float("inf"):
        ans = -1
    print(ans)
    
if __name__ == '__main__':
    main()