Problem p02889 (python)

Fixed submission:
import sys
import numpy as np
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall, shortest_path

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 _ in range(m):
        a, b, c = map(int, input().split())
        wf[a-1][b-1] = wf[b-1][a-1] = c
    wf = np.array(wf)
    for i in range(n):
        wf[i][i] = 0
    wf = shortest_path(wf, method='FW')
    for i in range(n):
        for j in range(n):
            if wf[i][j] <= l:
                wf[i][j] = 1
            else:
                wf[i][j] = 301
    wf = shortest_path(wf, method='FW')
    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] = int(wf[s-1][t-1])
        else:
            ans[i] = 0
    for i in range(q):
        print(ans[i] - 1)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall, shortest_path
from scipy.sparse import csr_matrix

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 _ in range(m):
        a, b, c = map(int, input().split())
        wf[a-1][b-1] = wf[b-1][a-1] = c
    wf = numpy.array(wf)
    for i in range(n):
        wf[i][i] = 0
    wf = shortest_path(wf, method='FW')
    for i in range(n):
        for j in range(n):
            if wf[i][j] <= l:
                wf[i][j] = 1
            else:
                wf[i][j] = 301
    wf = shortest_path(wf, method='FW')
    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] = int(wf[s-1][t-1])
        else:
            ans[i] = 0
    for i in range(q):
        print(ans[i] - 1)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall

input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    wf = [[float("inf") 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] = wf[b-1][a-1] = c
    for i in range(n):
        wf[i][i] = 0
    wf = csgraph_from_dense(wf, null_value=10**10)
    wf = floyd_warshall(wf, directed=False)
    for i in range(n):
        for j in range(n):
            if wf[i][j] <= l:
                wf[i][j] = 1
            else:
                wf[i][j] = 301
    wf = csgraph_from_dense(wf, null_value=10**10)
    wf = floyd_warshall(wf, directed=False)
    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] = int(wf[s-1][t-1])
        else:
            ans[i] = 0
    for i in range(q):
        print(ans[i] - 1)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall

input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    wf = [[float("inf") 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] = wf[b-1][a-1] = c
    for i in range(n):
        wf[i][i] = 0
    wf = csgraph_from_dense(wf)
    wf = floyd_warshall(wf, directed=False)
    for i in range(n):
        for j in range(n):
            if wf[i][j] <= l:
                wf[i][j] = 1
            else:
                wf[i][j] = 301
    wf = csgraph_from_dense(wf)
    wf = floyd_warshall(wf, directed=False)
    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] = int(wf[s-1][t-1])
        else:
            ans[i] = 0
    for i in range(q):
        print(ans[i] - 1)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall

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
    G = csgraph_from_dense(wf)
    d = floyd_warshall(G)
    for i in range(n):
        for j in range(n):
            if d[i][j] <= l:
                wf[i][j] = 1
            else:
                wf[i][j] = 301
    G = csgraph_from_dense(wf)
    d = floyd_warshall(G)
    q = int(input())
    ans = [None] * q
    for i in range(q):
        s, t = map(int, input().split())
        if d[s-1][t-1] < 301:
            ans[i] = int(d[s-1][t-1])
        else:
            ans[i] = 0
    for i in range(q):
        print(ans[i] - 1)

if __name__ == "__main__":
    main()