Problem p02889 (python)

Fixed submission:
# coding: utf-8
from scipy.sparse.csgraph import floyd_warshall

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

for i in range(M):
    f,t,c = map(int, input().split())
    if c <= L:
        RG[f-1][t-1] = c
        RG[t-1][f-1] = c

Rd = floyd_warshall(RG, directed=False)

FG=[[0 for i in range(N)] for j in range(N)]
for i in range(N):
    for j in range(i+1, N):
        if Rd[i][j] <= L:
            FG[i][j] = 1
            FG[j][i] = 1

Fd = floyd_warshall(FG, directed=False)

Q=int(input())
for i in range(Q):
    s, t = map(int, input().split())
    v = Fd[s-1][t-1]
    if s == t:
        print (str(0))
    if v == float('inf'):
        print (str(-1))
    else:
        print (str(int(v-1)))

Buggy submission (verdict: Runtime Error):
# coding: utf-8
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import lil_matrix

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

for i in range(M):
    f,t,c = map(int, input().split())
    if c <= L:
        RG[(f-1,t-1)] = c
        RG[(t-1,f-1)] = c

Rd = floyd_warshall(RG, directed=False)

FG=lil_matrix((N,N))
for i in range(N):
    for j in range(i+1, N):
        if Rd[i][j] <= L:
            FG[(i,j)] = 1
            FG[(j,i)] = 1

Fd = floyd_warshall(FG, directed=False)

Q=int(input())
for i in range(Q):
    s, t = map(int, input().split())
    v = Fd[s-1][t-1]
    if s == t:
        print (str(0))
    if v == float('inf'):
        print (str(-1))
    else:
        print (str(int(v-1)))

Buggy submission (verdict: Runtime Error):
# coding: utf-8
from scipy.sparse.csgraph import floyd_warshall,csgraph_from_dense

MAX=float('inf')
N,M,L=map(int, raw_input().split())
Roads = [[ 0 if i == j else MAX for j in range(N)] for i in range(N)]

for i in range(M):
    f,t,c = map(int, raw_input().split())
    Roads[f-1][t-1] = c
    Roads[t-1][f-1] = c

RG = csgraph_from_dense(Roads, null_value=MAX)
Rd = floyd_warshall(RG)

Fuels = [[ 1 if Rd[i][j] <=L else MAX for j in range(N)] for i in range(N)]

FG = csgraph_from_dense(Fuels, null_value=MAX)
Fd = floyd_warshall(FG)

Q=int(raw_input())
Queries = []
for i in range(Q):
    s, t = map(int, raw_input().split())
    v = Fd[s-1][t-1]
    if s == t:
        print (str(0))
    if v == float('inf'):
        print (str(-1))
    else:
        print (str(int(v-1)))

Buggy submission (verdict: Runtime Error):
# coding: utf-8
from scipy.sparse.csgraph import floyd_warshall,csgraph_from_dense

MAX=float('inf')
N,M,L=map(int, input().split())
Roads = [[ 0 if i == j else MAX for j in range(N)] for i in range(N)]

for i in range(M):
    f,t,c = map(int, input().split())
    Roads[f-1][t-1] = c
    Roads[t-1][f-1] = c

RG = csgraph_from_dense(Roads, null_value=MAX)
Rd = floyd_warshall(RG)

Fuels = [[ 1 if Rd[i][j] <=L else MAX for j in range(N)] for i in range(N)]

FG = csgraph_from_dense(Fuels, null_value=MAX)
Fd = floyd_warshall(FG)

Q=int(input())
Queries = []
for i in range(Q):
    s, t = map(int, input().split())
    v = Fd[s-1][t-1]
    if v == float('inf'):
        print (str(-1))
    else:
        print (str(int(v -1)))

Buggy submission (verdict: Runtime Error):
# coding: utf-8
from scipy.sparse.csgraph import floyd_warshall,csgraph_from_dense

MAX=pow(10,9) * 300
N,M,L=map(int, input().split())
Roads = [[ 0 if i == j else MAX for j in range(N)] for i in range(N)]

for i in range(M):
    f,t,c = map(int, input().split())
    Roads[f-1][t-1] = c
    Roads[t-1][f-1] = c

RG = csgraph_from_dense(Roads, null_value=MAX)
Rd = floyd_warshall(RG)

Fuels = [[ 1 if Rd[i][j] <=L else MAX for j in range(N)] for i in range(N)]

FG = csgraph_from_dense(Fuels, null_value=MAX)
Fd = floyd_warshall(FG)

Q=int(input())
Queries = []
for i in range(Q):
    s, t = map(int, input().split())
    v = Fd[s-1][t-1]
    if v == float('inf'):
        print (str(-1))
    else:
        print (str(int(v -1)))