Problem p02889 (python)

Fixed submission:
n,m,l=map(int,input().split())
inf=10000000000
g=[[inf for i in range(n)] for j in range(n)]
for i in range(m):
    a,b,c=map(int,input().split())
    g[a-1][b-1]=c
    g[b-1][a-1]=c
for k in range(n):
    for i in range(n):
        for j in range(n):
            g[i][j]=min(g[i][j],g[i][k]+g[k][j])
r=[[inf for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(n):
        if g[i][j]<=l:
            r[i][j]=1
        elif g[i][j]>=inf:
            r[i][j]=inf
for k in range(n):
    for i in range(n):
        for j in range(n):
            r[i][j]=min(r[i][j],r[i][k]+r[k][j])
q=int(input())
for i in range(q):
    s,e=map(int,input().split())
    if r[s-1][e-1]>=inf:
        print(-1)
    else:
        print(r[s-1][e-1]-1)

Buggy submission (verdict: Runtime Error):
#O(ElogV)
import heapq
def dijkstra_heap(s,g):
    #始点sから各頂点への最短距離
    d = [float("inf")] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        if e[0]<=l:
            heapq.heappush(edgelist,[[0,l-e[0]],e[1]])
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        if v==g:
            return minedge[0][0]
        nl = minedge[0][1]
        kai=minedge[0][0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                if e[0]<=l and e[0]<=nl:
                    heapq.heappush(edgelist,[[kai,nl-e[0]],e[1]])
                elif e[0]<=l:
                    heapq.heappush(edgelist,[[kai+1,l],e[1]])
                    
    return -1

################################
n,w,l = map(int,input().split()) #n:頂点数　w:辺の数

edge = [[] for i in range(n)]
#edge[i] : iから出る道の[重み,行先]の配列
for i in range(w):
    x,y,z = map(int,input().split())
    edge[x].append([z,y])
    edge[y].append([z,x])
q=int(input())
for i in range(q):
    sa,ga=map(int,input().split())
    print(dijkstra_heap(sa-1,ga-1))

Buggy submission (verdict: Runtime Error):
#O(ElogV)
import heapq
def dijkstra_heap(s,g):
    #始点sから各頂点への最短距離
    d = [float("inf")] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        if e[0]<=l:
            heapq.heappush(edgelist,[[0,l-e[0]],e[1]])
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        if v==g:
            return minedge[0][0]
        nl = minedge[0][1]
        kai=minedge[0][0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                if e[0]<=l and e[0]<=nl
                    heapq.heappush(edgelist,[[kai,nl-e[0]],e[1]])
                elif e[0]<=l:
                    heapq.heappush(edgelist,[[kai+1,l],e[1]])
                    
    return -1

################################
n,w,l = map(int,input().split()) #n:頂点数　w:辺の数

edge = [[] for i in range(n)]
#edge[i] : iから出る道の[重み,行先]の配列
for i in range(w):
    x,y,z = map(int,input().split())
    edge[x].append([z,y])
    edge[y].append([z,x])
q=int(input())
for i in range(q):
    sa,ga=map(int,input().split())
    print(dijkstra_heap(sa-1,ga-1))