Problem p02889 (python)

Fixed submission:
import numpy as np
from scipy.sparse.csgraph import floyd_warshall

n,m,l=map(int,input().split())
ans=[[0]*n for i in range(n)]
for i in range(m):
    a,b,c=[int(j) for j in input().split()]
    ans[a-1][b-1]=c
    ans[b-1][a-1]=c
q=int(input())
st=[]
for i in range(q):
    st.append([int(j)-1 for j in input().split()])

ans=floyd_warshall(ans)

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

ans=floyd_warshall(ans)

for i,j in st:
    if ans[i][j]==float("inf"):
        print(-1)
    else:
        print(int(ans[i][j])-1)

Buggy submission (verdict: Runtime Error):
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall

n,m,l=map(int,input().split())
ll=[]
for i in range(m):
    a,b,c=[int(j) for j in input().split()]
    if c<=l:
        ll.append((a,b,c))
q=int(input())
st=[]
for i in range(q):
    st.append([int(j)-1 for j in input().split()])
if ll==[]:
    print(-1)
    exit()
edge=np.array(ll,dtype=np.int64).T
graph=csr_matrix(((edge[2],edge[:2]-1)),(n,n))

#False->無向グラフ True->有効グラフ
ans=floyd_warshall(graph,directed=False)

graph2=np.full((n,n),np.inf)

np.fill_diagonal(graph2,0)

graph2[ans<=l]=1


ans2=floyd_warshall(graph2,directed=False)


for i,j in st:
    if int(ans2[i][j])==float("inf"):
        print(-1)
    else:
        print(int(ans2[i][j])-1)

Buggy submission (verdict: Runtime Error):
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall

n,m,l=map(int,input().split())
ll=[]
for i in range(m):
    a,b,c=[int(j) for j in input().split()]
    if c<=l:
        ll.append((a,b,c))
if ll==[]:
    print(-1)
    exit()
edge=np.array(ll,dtype=np.int64).T
graph=csr_matrix(((edge[2],edge[:2]-1)),(n,n))

#False->無向グラフ True->有効グラフ
ans=floyd_warshall(graph,directed=False)
v=[]
s=set(list(range(1,n+1)))
for i in range(n):
    for j in range(n):
        if ans[i][j]<=l:
            if i+1 in s:
                s.remove(i+1)
            if j+1 in s:
                s.remove(j+1)
            v.append((i+1,j+1,1))
for i in s:
    v.append((1,i,float("inf")))
edge=np.array(v,dtype=np.int64).T
graph=csr_matrix(((edge[2],edge[:2]-1)),(n,n))
ans2=floyd_warshall(graph,directed=False)

q=int(input())
st=[]
for i in range(q):
    st.append([int(j)-1 for j in input().split()])

for i,j in st:
    if int(ans2[i][j])==float("inf"):
        print(-1)
    else:
        print(int(ans2[i][j])-1)

Buggy submission (verdict: Runtime Error):
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall

n,m,l=map(int,input().split())
ll=[]
for i in range(m):
    a,b,c=[int(j) for j in input().split()]
    if c<=l:
        ll.append((a,b,c))
if ll==[]:
    print(-1)
    exit()
edge=np.array(ll,dtype=np.int64).T
graph=csr_matrix(((edge[2],edge[:2]-1)),(n,n))

#False->無向グラフ True->有効グラフ
ans=floyd_warshall(graph,directed=False)
v=[]
for i in range(n):
    for j in range(n):
        if ans[i][j]<=l:
            v.append((i+1,j+1,1))

edge=np.array(v,dtype=np.int64).T
graph=csr_matrix(((edge[2],edge[:2]-1)),(n,n))
ans2=floyd_warshall(graph,directed=False)

q=int(input())
st=[]
for i in range(q):
    st.append([int(j)-1 for j in input().split()])

for i,j in st:
    if int(ans2[i][j])==float("inf"):
        print(-1)
    else:
        print(int(ans2[i][j])-1)