Problem p04003 (python)

Fixed submission:
def main():
  from heapq import heappush,heappop,heapify
  inf=2**31-1
  n,m,*t=map(int,open(0).read().split())
  z=[]
  for a,b,c in zip(t[::3],t[1::3],t[2::3]):
    z.extend([a,b,a+n*c,b+n*c])
  z={i:v for v,i in enumerate(sorted(set(z)))}
  try:
    z[1]
  except:
    print(-1)
    exit()
  edge=[[]for _ in range(len(z))]
  d={}
  l=[]
  for a,b,c in zip(t[::3],t[1::3],t[2::3]):
    if b<a:a,b=b,a
    i,j,x,y=z[a],z[b],z[a+n*c],z[b+n*c]
    if b==n:
      l.append(y)
    edge[i].append((1,x))
    edge[j].append((1,y))
    edge[x].extend([(0,i),(0,y)])
    edge[y].extend([(0,j),(0,x)])
  used=[False]+[True]*~-len(z)
  d=[0]+[inf]*~-len(z)
  edgelist=edge[0]
  heapify(edgelist)
  while edgelist:
    m=heappop(edgelist)
    if not used[m[1]]:
      continue
    d[m[1]]=m[0]
    used[m[1]]=False
    for e in edge[m[1]]:
      if used[e[1]]:
        heappush(edgelist,(e[0]+m[0],e[1]))
  a=min([d[i]for i in l]or[inf])
  print([a,-1][a==inf])
if __name__=='__main__':
  main()

Buggy submission (verdict: Runtime Error):
from collections import*
from heapq import*
def dijkstra(s):
  d=[inf]*n
  used=[True]*n
  d[s]=0
  used[s]=False
  edgelist=[]
  for e in edge[s]:
    heappush(edgelist,e)
  while len(edgelist):
    minedge=heappop(edgelist)
    if not used[minedge[1]]:
      continue
    v=minedge[1]
    d[v]=minedge[0]
    used[v]=False
    for e in edge[v]:
      if used[e[1]]:
        heappush(edgelist,(e[0]+d[v],e[1]))
  return d
inf=10**18
n,m,*t=map(int,open(0).read().split())
e=defaultdict(list)
s=set()
l=set()
f=False
for a,b,c in zip(t[::3],t[1::3],t[2::3]):
  if b<a:a,b=b,a
  if a<2 or b<2:f=True
  x,y=a+-~n*c,b+-~n*c
  if b==n:l|={y}
  e[a].append((1,x))
  e[b].append((1,y))
  e[x].extend([(0,a),(0,y)])
  e[y].extend([(0,b),(0,x)])
  s|=set((a,b,x,y))
if not f:
  print(-1)
  exit()
d={i:v for v,i in enumerate(sorted(s))}
n=len(d)
edge=tuple([]for _ in range(n))
for i in e:
  edge[d[i]].extend([(c,d[j])for c,j in e[i]])
a=dijkstra(0)
a=min(a[d[i]]for i in l)
print([a,-1][a==inf])

Buggy submission (verdict: Runtime Error):
from collections import*
from heapq import*
def dijkstra(s):
  d=[inf]*n
  used=[True]*n
  d[s]=0
  used[s]=False
  edgelist=[]
  for e in edge[s]:
    heappush(edgelist,e)
  while len(edgelist):
    minedge=heappop(edgelist)
    if not used[minedge[1]]:
      continue
    v=minedge[1]
    d[v]=minedge[0]
    used[v]=False
    for e in edge[v]:
      if used[e[1]]:
        heappush(edgelist,(e[0]+d[v],e[1]))
  return d
inf=10**18
n,m,*t=map(int,open(0).read().split())
e=defaultdict(list)
s=[]
l=[]
f=False
for a,b,c in zip(t[::3],t[1::3],t[2::3]):
  if b<a:a,b=b,a
  if a<2 or b<2:f=True
  x,y=a+-~n*c,b+-~n*c
  if b==n:l.append(y)
  e[a].append((1,x))
  e[b].append((1,y))
  e[x].extend([(0,a),(0,y)])
  e[y].extend([(0,b),(0,x)])
  s.extend([a,b,x,y])
if not f:
  print(-1)
  exit()
d={i:v for v,i in enumerate(sorted(set(s)))}
n=len(d)
edge=tuple([]for _ in range(n))
for i in e:
  edge[d[i]].extend([(c,d[j])for c,j in e[i]])
a=dijkstra(0)
a=min(a[d[i]]for i in l)
print([a,-1][a==inf])

Buggy submission (verdict: Runtime Error):
from collections import*
from heapq import*
def dijkstra(s):
  d=[inf]*n
  used=[True]*n
  d[s]=0
  used[s]=False
  edgelist=[]
  for e in edge[s]:
    heappush(edgelist,e)
  while len(edgelist):
    minedge=heappop(edgelist)
    if not used[minedge[1]]:
      continue
    v=minedge[1]
    d[v]=minedge[0]
    used[v]=False
    for e in edge[v]:
      if used[e[1]]:
        heappush(edgelist,(e[0]+d[v],e[1]))
  return d
inf=10**18
n,m,*t=map(int,open(0).read().split())
e=defaultdict(list)
s=[]
l=[]
f=False
for a,b,c in zip(t[::3],t[1::3],t[2::3]):
  if b<a:a,b=b,a
  if a<2 or b<2:f=True
  x,y=a+-~n*c,b+-~n*c
  if b==n:l.append(y)
  e[a].append((1,x))
  e[b].append((1,y))
  e[y].append((0,b))
  e[x].append((0,y))
  s.extend([a,b,x,y])
if not f:
  print(-1)
  exit()
d={i:v for v,i in enumerate(sorted(set(s)))}
n=len(d)
edge=tuple([]for _ in range(n))
for i in e:
  edge[d[i]].extend([(c,d[j])for c,j in e[i]])
a=dijkstra(0)
a=min(a[d[i]]for i in l)
print([a,-1][a==inf])

Buggy submission (verdict: Runtime Error):
from collections import*
from heapq import*
def dijkstra(s):
  d=[inf]*n
  used=[True]*n
  d[s]=0
  used[s]=False
  edgelist=[]
  for e in edge[s]:
    heappush(edgelist,e)
  while len(edgelist):
    minedge=heappop(edgelist)
    if not used[minedge[1]]:
      continue
    v=minedge[1]
    d[v]=minedge[0]
    used[v]=False
    for e in edge[v]:
      if used[e[1]]:
        heappush(edgelist,(e[0]+d[v],e[1]))
  return d
inf=10**18
n,m,*t=map(int,open(0).read().split())
e=defaultdict(list)
s=[]
l=[]
f=False
for a,b,c in zip(t[::3],t[1::3],t[2::3]):
  if b<a:a,b=b,a
  if a<2:f=True
  x,y=a+-~n*c,b+-~n*c
  if b==n:l.append(y)
  e[a].append((1,x))
  e[y].append((0,b))
  e[x].append((0,y))
  s.extend([a,b,x,y])
if not f:
  print(-1)
  exit()
d={i:v for v,i in enumerate(sorted(set(s)))}
n=len(d)
edge=tuple([]for _ in range(n))
for i in e:
  edge[d[i]].extend([(c,d[j])for c,j in e[i]])
a=dijkstra(0)
a=min(a[d[i]]for i in l)
print([a,-1][a==inf])

Buggy submission (verdict: Runtime Error):
from collections import*
from heapq import*
def dijkstra(s):
  d=[inf]*n
  used=[True]*n
  d[s]=0
  used[s]=False
  edgelist=[]
  for e in edge[s]:
    heappush(edgelist,e)
  while len(edgelist):
    minedge=heappop(edgelist)
    if not used[minedge[1]]:
      continue
    v=minedge[1]
    d[v]=minedge[0]
    used[v]=False
    for e in edge[v]:
      if used[e[1]]:
        heappush(edgelist,(e[0]+d[v],e[1]))
  return d
inf=10**18
n,m,*t=map(int,open(0).read().split())
if m<1:
  print(-1)
  exit()
e=defaultdict(list)
s=[]
l=[]
for a,b,c in zip(t[::3],t[1::3],t[2::3]):
  if b<a:a,b=b,a
  if b==n:l.append(b+n*c)
  e[a].append((1,a+n*c))
  e[b+n*c].append((0,b))
  e[a+n*c].append((0,b+n*c))
  s.extend([a,b,a+n*c,b+n*c])
d={i:v for v,i in enumerate(sorted(set(s)))}
n=len(d)
edge=tuple([]for _ in range(n))
for i in e:
  edge[d[i]].extend([(c,d[j])for c,j in e[i]])
a=dijkstra(0)
a=min(a[d[i]]for i in l)
print([a,-1][a==inf])

Buggy submission (verdict: Runtime Error):
from collections import*
from heapq import*
def dijkstra(s):
  d=[float('inf')]*n
  used=[True]*n
  d[s]=0
  used[s]=False
  edgelist=[]
  for e in edge[s]:
    heappush(edgelist,e)
  while len(edgelist):
    minedge=heappop(edgelist)
    if not used[minedge[1]]:
      continue
    v=minedge[1]
    d[v]=minedge[0]
    used[v]=False
    for e in edge[v]:
      if used[e[1]]:
        heappush(edgelist,(e[0]+d[v],e[1]))
  return d
n,m,*t=map(int,open(0).read().split())
if m<1:
  print(-1)
  exit()
e=defaultdict(list)
s=[]
l=[]
for a,b,c in zip(t[::3],t[1::3],t[2::3]):
  if b<a:a,b=b,a
  if b==n:l.append(b+n*c)
  e[a].append((1,a+n*c))
  e[b+n*c].append((0,b))
  e[a+n*c].append((0,b+n*c))
  s.extend([a,b,a+n*c,b+n*c])
d={i:v for v,i in enumerate(sorted(set(s)))}
n=len(d)
edge=tuple([]for _ in range(n))
for i in e:
  edge[d[i]].extend([(c,d[j])for c,j in e[i]])
a=dijkstra(0)
a=min(a[d[i]]for i in l)
print([a,-1][a==float('inf')])