Problem:


船旅  

問題

JOI国には，n 島の島があり，各島には 1 から n までの番号が付けられている．現在，JOI国では各島の間を結ぶ航路網の整備が進んでいる．

あなたは，船舶の切符を扱っているチケットセンターに勤務している．JOI国には船舶を使って，できる限り安価に，島と島の間を旅行したいと考えている人が沢山おり，彼らは，出発地と目的地を注文票に記入して，あなたのところに送ってくる．

あなたの仕事は，客から注文票を受け取ったらすぐに，いくつかの船舶を乗り継いで，出発地と目的地を結ぶ航路の中で，もっとも安価な運賃を計算し，客に伝えることである．
ただし，旅程によっては，船舶を使って旅行することが出来ない場合もある．そのときは『船舶を使って旅行することが出来ない』と客に伝える必要がある．また，JOI国では，島と島の間を結ぶ新しい船舶が，次々と運航を開始しており，あなたには，その情報が随時伝えられる．客に返事をする際には，最新の情報に留意しなければならない．

入力として，客の注文票や新たに運航を開始した船舶の運航情報が与えられたときに，客への返事を求めるプログラムを作成せよ．

なお，入力例１と出力例１に対する実行状況を，図１として図示している．

入力

入力は複数のデータセットからなる．各データセットは以下の形式で与えられる．

入力の 1 行目には2つの整数 n, k (1 ≤ n ≤ 100, 1 ≤ k ≤ 5000) が書かれている．これは，島の数が n 島で，入力が k + 1 行からなることを表す．
i + 1 行目 (1 ≤ i ≤ k) には， 3 個または 4 個の整数が空白を区切りとして書かれている．

    
 最初の数字が 0 のとき，この行は客の注文票を表す．

      この行には3個の整数 0, a, b (1 ≤ a ≤ n, 1 ≤ b ≤ n, a &ne; b) が空白を区切りとして書かれている．
      これは，客が，島 a を出発地とし島 b を目的地とするような注文票を送ってきたことを表す．

    
 最初の数字が 1 のとき，この行は新たに運航を開始した船舶の運航情報を表す．

      この行には 4 個の整数 1, c, d, e (1 ≤ c ≤ n, 1 ≤ d ≤ n, c &ne; d, 1 ≤ e ≤ 1000000) が書かれている．

      これは島 c と島 d を往復する船舶が新たに運航を開始し，この船舶の島 c から島 d への運賃と，島 d から島 c への運賃が，共に e であることを表す．

      この行以降の注文票に対しては，この船舶も考慮して返事をしなければならない．

最初の段階では，船舶は一隻も運航していないものとする．入力のうち，船舶の運航情報を表す行は 1000 行以下である．また，島と島の間に，複数の船舶が運航することがあることに注意せよ． 

n, k がともに 0 のとき入力の終了を示す. データセットの数は 5 を超えない．

出力

データセットごとに以下の形式で出力する．

入力のうち，注文票を表す行の数を m とおく．
 各データセットの出力は m 行からなり， i 行目(1 ≤ i ≤ m)には， i 番目の注文票に対する返事を表す整数を書く．
すなわち， i 番目の注文票の出発地と目的地の間を，いくつかの船舶を乗り継いで旅行することが可能ならば，その運賃の合計の最小値を書く．旅行することが不可能ならば，-1 を出力する．

入出力例

入力例

3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0

出力例

-1
15
12
5955
21431
9298
16392
24774
8840

上記問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0


Sample output:
-1
15
12
5955
21431
9298
16392
24774
8840

Submission status: Accepted

Solution code:
from heapq import*
import sys
r=sys.stdin.readline
def g(n,E,S,G):
 F=[1e7]*-~n;F[S]=0;H=[(0,S)]
 while H:
  c,u=heappop(H)
  if u==G:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:F[v]=t;heappush(H,(t,v))
 return-1
def s(n,k,a):
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r();p=map(int,f[2:].split())
  if'0'==f[0]:a+=f'{g(n,E,*p)}\n'
  else:c,d,e=p;E[c]+=[(e,d)];E[d]+=[(e,c)]
 print(a[:-1])
for e in iter(r,'0 0\n'):s(*map(int,e.split()),'')

Submission status: Accepted

Solution code:
from heapq import*
import sys
r=sys.stdin.readline
def g(n,E,S,G):
 F=[1e7]*-~n;F[S]=0;H=[(0,S)]
 while H:
  c,u=heappop(H)
  if u==G:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:F[v]=t;heappush(H,(t,v))
 return-1
def s(n,k):
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r();p=map(int,f[2:].split())
  if'0'==f[0]:print(g(n,E,*p))
  else:c,d,e=p;E[c]+=[(e,d)];E[d]+=[(e,c)]
[s(*map(int,e.split()))for e in iter(r,'0 0\n')]

Submission status: Accepted

Solution code:
import heapq,sys
r=sys.stdin.readline
def g(n,E,S,G):
 F=[1e7]*-~n;F[S]=0;H=[(0,S)]
 while H:
  c,u=heapq.heappop(H)
  if u==G:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:F[v]=t;heapq.heappush(H,(t,v))
 return-1
def s(n,k):
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r();p=map(int,f[2:].split())
  if'0'==f[0]:print(g(n,E,*p))
  else:c,d,e=p;E[c]+=[(e,d)];E[d]+=[(e,c)]
for e in iter(r,'0 0\n'):s(*map(int,e.split()))

Submission status: Accepted

Solution code:
from heapq import*
import sys
r=sys.stdin.readline
def g(n,E,S,G):
 F=[1e7]*-~n;F[S]=0;H=[[0,S]]
 while H:
  c,u=heappop(H)
  if u==G:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:F[v]=t;heappush(H,[t,v])
 return-1
def s(n,k):
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r();p=map(int,f[2:].split())
  if'0'==f[0]:print(g(n,E,*p))
  else:c,d,e=p;E[c]+=[(e,d)];E[d]+=[(e,c)]
for e in iter(r,'0 0\n'):s(*map(int,e.split()))

Submission status: Accepted

Solution code:
from heapq import*
import sys
r=sys.stdin.readline
def g(n,E,S,G):
 F=[1e7]*-~n;F[S]=0;H=[[0,S]]
 while H:
  c,u=heappop(H)
  if u==G:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:F[v]=t;heappush(H,[t,v])
 return-1
def s(n,k):
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r();p=map(int,f[2:-1].split())
  if'0'==f[0]:print(g(n,E,*p))
  else:c,d,e=p;E[c]+=[(e,d)];E[d]+=[(e,c)]
for e in iter(r,'0 0\n'):s(*map(int,e.split()))

Submission status: Accepted

Solution code:
from heapq import*
import sys
r=sys.stdin.readline
def g(n,E,S,G):
 F=[1e7]*-~n;F[S]=0;H=[(0,S)]
 while H:
  c,u=heappop(H)
  if u==G:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:F[v]=t;heappush(H,(t,v))
 return-1
def s(n,k):
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r();p=map(int,f[2:-1].split())
  if'0'==f[0]:print(g(n,E,*p))
  else:c,d,e=p;E[c]+=[(e,d)];E[d]+=[(e,c)]
for e in iter(r,'0 0\n'):s(*map(int,e.split()))

Submission status: Accepted

Solution code:
from heapq import*
import sys
r=sys.stdin.readline
def g(n,E,S,G):
 F=[1e7]*-~n;F[S],H=0,[(0,S)]
 while H:
  c,u=heappop(H)
  if u==G:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:F[v]=t;heappush(H,(t,v))
 return-1
def s(n,k):
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r();p=map(int,f[2:].split())
  if'0'==f[0]:print(g(n,E,*p))
  else:c,d,e=p;E[c]+=[(e,d)];E[d]+=[(e,c)]
for e in iter(r,'0 0\n'):s(*map(int,e.split()))

Submission status: Accepted

Solution code:
from heapq import*
import sys
r=sys.stdin.readline
def g(n,E,S,G):
 F=[1e7]*-~n;F[S],H=0,[(0,S)]
 while H:
  c,u=heappop(H)
  if u==G:return print(c)
  for f,v in E[u]:
   t=c+f
   if t<F[v]:F[v]=t;heappush(H,(t,v))
 return print(-1)
def s(n,k):
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r();p=map(int,f[2:].split())
  if'0'==f[0]:g(n,E,*p)
  else:c,d,e=p;E[c]+=[(e,d)];E[d]+=[(e,c)]
for e in iter(r,'0 0\n'):s(*map(int,e.split()))

Submission status: Accepted

Solution code:
from heapq import*
import sys
r=sys.stdin.readline
def g(n,E,S,G):
 F=[1e7]*-~n;F[S],H=0,[(0,S)]
 while H:
  c,u=heappop(H)
  if u==G:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:F[v]=t;heappush(H,(t,v))
 return-1
def s(n,k):
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r();p=map(int,f[2:].split())
  if'0'==f[0]:print(g(n,E,*p))
  else:c,d,e=p;E[c],E[d]=E[c]+[(e,d)],E[d]+[(e,c)]
for e in iter(r,'0 0\n'):s(*map(int,e.split()))

Submission status: Accepted

Solution code:
from heapq import*
import sys
r=sys.stdin.readline
def g(n,E,S,G):
 F=[1e7]*-~n;F[S],H=0,[(0,S)]
 while H:
  c,u=heappop(H)
  if u==G:return c
  for f,v in E[u]:
   if c+f<F[v]:F[v]=c+f;heappush(H,(F[v],v))
 return-1
def s(n,k):
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r();p=map(int,f[2:].split())
  if'0'==f[0]:print(g(n,E,*p))
  else:c,d,e=p;E[c]+=[(e,d)];E[d]+=[(e,c)]
for e in iter(r,'0 0\n'):s(*map(int,e.split()))

Submission status: Accepted

Solution code:
def main():
 from heapq import heappush, heappop
 import sys
 r=sys.stdin.readline
 def g(n,E,S,G):
  F=[1e7]*-~n;F[S],H=0,[(0,S)]
  while H:
   c,u=heappop(H)
   if u==G:return c
   for f,v in E[u]:
    t=c+f
    if t<F[v]:F[v]=t;heappush(H,(t,v))
  return-1
 def s(n,k):
  E=[[]for _ in[0]*-~n]
  for _ in[0]*k:
   f=r();p=map(int,f[2:].split())
   if'0'==f[0]:print(g(n,E,*p))
   else:c,d,e=p;E[c]+=[(e,d)];E[d]+=[(e,c)]
 for e in iter(r,'0 0\n'):s(*map(int,e.split()))
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
 from heapq import heappush, heappop
 import sys
 r=sys.stdin.readline
 def g(n,E,S,G):
  F=[1e7]*-~n;F[S],H=0,[(0,S)]
  while H:
   c,u=heappop(H)
   if u==G:return c
   for f,v in E[u]:
    t=c+f
    if t<F[v]:F[v]=t;heappush(H,(t,v))
  return-1
 def s(n,k):
  E=[[]for _ in[0]*-~n]
  for _ in[0]*k:
   f=r();p=map(int,f[2:].split())
   if'0'==f[0]:print(g(n,E,*p))
   else:c,d,e=p;E[c]+=[(e,d)];E[d]+=[(e,c)]
 for e in iter(r,'0 0\n'):s(*map(int,e.split()))
if __name__ == '__main__': main()

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
import sys
r=sys.stdin.readline
def g(n,E,S,G):
 F=[1e7]*-~n
 F[S],H=0,[(0,S)]
 while H:
  c,u=heappop(H)
  if u==G:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:
    F[v]=t
    heappush(H,(t,v))
 return-1
def s(n,k):
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r();p=map(int,f[2:].split())
  if'0'==f[0]:
   print(g(n,E,*p))
  else:
   c,d,e=p
   E[c]+=[(e,d)]
   E[d]+=[(e,c)]
for e in iter(r,'0 0\n'):
 s(*map(int,e.split()))

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
import sys
r=sys.stdin.readline
def s(n,k):
 def g(S,G):
  F=[1e7]*-~n;F[S],H=0,[(0,S)]
  while H:
   c,u=heappop(H)
   if u==G:return c
   for f,v in E[u]:
    t=c+f
    if t<F[v]:F[v]=t;heappush(H,(t,v))
  return-1
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r();p=map(int,f[2:].split())
  if'0'==f[0]:print(g(*p))
  else:c,d,e=p;E[c]+=[(e,d)];E[d]+=[(e,c)]
for e in iter(r,'0 0\n'):s(*map(int,e.split()))

Submission status: Accepted

Solution code:
I=float('inf')
for e in iter(input,'0 0'):
 n,k=map(int,e.split())
 F=[[I]*-~n for _ in[0]*-~n]
 for i in range(1,n+1):F[i][i]=0
 for _ in[0]*k:
  f=input();g=map(int,f[2:].split())
  if'0'==f[0]:a,b=g;A=F[a][b];print([A,-1][A==I])
  else:
   c,d,e=g
   if e<F[c][d]:
    for i in range(2,n+1):
     for j in range(1,i):
      F[i][j]=min(F[i][j],F[i][c]+e+F[d][j],F[i][d]+e+F[c][j])
    for j in range(2,n+1):
     for i in range(1,j):
      F[i][j]=F[j][i]

Submission status: Accepted

Solution code:
I=float('inf')
for e in iter(input,'0 0'):
 n,k=map(int,e.split())
 F=[[I]*-~n for _ in[0]*-~n]
 for i in range(1,n+1):F[i][i]=0
 for _ in[0]*k:
  f=input();g=map(int,f[2:].split())
  if'0'==f[0]:a,b=g;A=F[a][b];print([A,-1][A==I])
  else:
   c,d,e=g
   if e<F[c][d]:
    for i in range(2,n+1):
     for j in range(1,i):
      F[i][j]=F[j][i]=min(F[i][j],F[i][c]+e+F[d][j],F[i][d]+e+F[c][j])

Submission status: Accepted

Solution code:
import sys
r=sys.stdin.readline
I=float('inf')
for e in iter(r,'0 0\n'):
 n,k=map(int,e.split())
 F=[[I]*-~n for _ in[0]*-~n]
 for i in range(1,n+1):F[i][i]=0
 for _ in[0]*k:
  f=r();g=map(int,f[2:].split())
  if'0'==f[0]:a,b=g;A=F[a][b];print([A,-1][A==I])
  else:
   c,d,e=g
   if e<F[c][d]:
    for i in range(2,n+1):
     for j in range(1,i):
      F[i][j]=F[j][i]=min(F[i][j],F[i][c]+e+F[d][j],F[i][d]+e+F[c][j])

Submission status: Accepted

Solution code:
def s():
 import sys
 r=sys.stdin.readline
 I=float('inf')
 for e in iter(r,'0 0\n'):
  n,k=map(int,e.split())
  F=[[I]*-~n for _ in[0]*-~n]
  for i in range(1,n+1):F[i][i]=0
  for _ in[0]*k:
   f=r();g=map(int,f[2:].split())
   if'0'==f[0]:a,b=g;A=F[a][b];print([A,-1][A==I])
   else:
    c,d,e=g
    if e<F[c][d]:
     for i in range(2,n+1):
      for j in range(1,i):
       F[i][j]=F[j][i]=min(F[i][j],F[i][c]+e+F[d][j],F[i][d]+e+F[c][j])
if'__main__'==__name__:s()

Submission status: Accepted

Solution code:
def s():
 import sys
 r=sys.stdin.readline
 I=float('inf')
 for e in iter(r,'0 0\n'):
  n,k=map(int,e.split())
  F=[[I]*-~n for _ in[0]*-~n]
  for i in range(1,n+1):F[i][i]=0
  for _ in[0]*k:
   f=r();g=map(int,f[2:].split())
   if'0'==f[0]:a,b=g;A=F[a][b];print([A,-1][A==I])
   else:
    c,d,e=g
    if e<F[c][d]:
     for i in range(2,n+1):
      for j in range(1,i):
       F[i][j]=F[j][i]=min(F[i][j],F[i][c]+e+F[d][j],F[i][d]+e+F[c][j])
s()

Submission status: Accepted

Solution code:
while 1:
    INF = 10**9
    N, K = map(int, input().split())
    if N == K == 0:
        break
    E = [[INF]*N for i in range(N)]
    for i in range(N):
        E[i][i] = 0
    ans = []
    for i in range(K):
        q, *c = map(int, input().split())
        if q == 0:
            a, b = c
            cost = E[a-1][b-1]
            if INF <= cost:
                ans.append(-1)
            else:
                ans.append(cost)
        else:
            c, d, e = c
            if e < E[c-1][d-1]:
                E[c-1][d-1] = E[d-1][c-1] = e
                Ec = E[c-1]; Ed = E[d-1]
                for i in range(N):
                    Ei = E[i]
                    Eic = Ei[c-1] + e
                    Eid = Ei[d-1] + e
                    for j in range(i+1, N):
                        Ei[j] = E[j][i] = min(Ei[j], Eic + Ed[j], Eid + Ec[j])
    print(*ans, sep='\n')

Submission status: Accepted

Solution code:
# AOJ 0526: Boat Travel
# Python3 2018.7.1 bal4u

INF = 0x7fffffff
while True:
	n, k = map(int, input().split())
	if n == 0: break
	fee = [[INF for j in range(101)] for i in range(101)]
	for i in range(n+1): fee[i][i] = 0
	for i in range(k):
		v = list(map(int, input().split()))
		a, b = v[1], v[2]
		if v[0] == 1:
			e = v[3]
			if fee[a][b] > e:
				fee[a][b], fee[b][a] = e, e
				for c in range(1, n+1):
					for d in range(c+1, n+1):
						t = fee[c][a] + e + fee[b][d]
						if t < fee[c][d]: fee[c][d], fee[d][c] = t, t
						t = fee[c][b] + e + fee[a][d]
						if t < fee[c][d]: fee[c][d], fee[d][c] = t, t;
		else:
			e = fee[a][b]
			if e >= INF: e = -1
			print(e)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from math import *
import heapq

def disktra(g,s):
    n=len(g)
    dist = [inf]*n
    dist[s] = 0
    hq=[]
    heapq.heappush(hq,(dist[s],s))
    while hq:
        cost,u =heapq.heappop(hq)
        if dist[u] < cost:
            continue
        for v,c in g[u]:
            if dist[v] <= dist[u] +c:
                continue
            dist[v] = dist[u] + c
            heapq.heappush(hq,(dist[v],v))
    return dist
while True:
    n, k = map(int, input().split())
    if n == 0 and k == 0:
        break
    G = [[]*n for i in range(n)]
    used = [0]*n

    E = [[float('inf')]*n for i in range(n)]
    for i in range(k):
        inp = tuple(map(int, input().split()))
        if inp[0] == 1:
            c, d, e = inp[1:]
            c -= 1
            d -= 1
            G[c].append((d,e))
            G[d].append((c,e))
            # E[c][d] = min(E[c][d], e)
            # E[d][c] = min(E[d][c], e)

        else:
            # for i in G:
            #     print(i)
            a, b = inp[1:]
            a-=1
            b-=1
            d=disktra(G,a)
            print(-1 if d[b]==inf else d[b])

        # dis = [float('inf')]*n
        # dis[a] = 0
        # mv = a
        # for v in G[a]:
        #     m = float('inf')
        #     if not used[v]:
        #         if m >dis[v]:
        #             m = dis[v]
        #             mv = v
        # used[mv]=1
        #     # print(dis)
        # for i in G[mv]:
        #     dis[i] = min(dis[i],dis[mv]+E[mv][i])
        # print(dis)
        # print(-1 if dis[b] == float('inf') else dis[b])


# while True:
#     n, k = map(int, input().split())
#     if n == 0 and k == 0:
#         break
#     G = [[]*n  for i in range(n)]
#     used = [0]*n
#     dis = [float('inf')]*n
#     E = [[float('inf')]*n for i in range(n)]
#     dis[0]=0
#     for i in range(k):
#         inp = tuple(map(int, input().split()))
#         if inp[0] == 1:
#             c, d, e = inp[1:]
#             c -= 1
#             d -= 1
#             G[c].append(d)
#             G[d].append(c)
#             E[c][d] = min(E[c][d], e)
#             E[d][c] = min(E[d][c], e)

#         else:
#             a, b = inp[1:]
#             a-=1;b-=1
#             mv=a
#             for v in G[a]:
#                 m=float('inf')
#                 if not used[v]:
#                     if m>dis[v]:
#                         m=dis[v]
#                         mv=v
#             for i in G[mv]:
#                 dis[i]=min(dis[i],dis[mv]+E[mv][i])
#             # print(dis)
                # print(-1 if dis[b]==float('inf') else dis[b])

Submission status: Runtime Error

Solution code:
inf = 10**6+1

def dijkstra(a,b):
    global inf
    global n
    global cost

    d = [inf]*n
    d[a] = 0
    prev= [-1]*n
    q = [x for x in range(n)]

    while(len(q) != 0):
        pmin = inf
        for idx in q:
            if pmin>d[idx]:
                pmin = d[idx]
        q.remove(d.index(pmin))
        u = d.index(pmin)
        for v in range(n):
            if cost[u][v] != inf:
                if d[v] > d[u] + cost[u][v]:
                    d[v] = d[u] + cost[u][v]
                    prev[v] = u

    return d[b]


while True:
    n,k = map(int, input().split())
    if (n|k)==0: break

    cost = [[inf]*n for i in range (n)]
    for inline in range(k):
        query = list(map(int, input().split()))
        if query[0]:
            cost[query[1]-1][query[2]-1] = min(cost[query[1]-1][query[2]-1], query[3])
            cost[query[2]-1][query[1]-1] = cost[query[1]-1][query[2]-1]
        else:
            mon = dijkstra(query[1]-1,query[2]-1)
            print(mon if mon != inf else -1)

Submission status: Runtime Error

Solution code:
INF = 1000000000

def solve(s,g,E,n):
  D = [0 if i == s else INF for i in range(n)]
  U = [0 for i in range(n)]
  V = [s]
  while V:
    v = V.pop(0)
    if v == g:
      break
    for e in E[v]:
      if and D[v] + e[1] < D[e[0]]:
        D[e[0]] = D[v] + e[1]
        V.append(e[0])
    V = sorted(V,key=lambda x:D[x])
  if D[g] >= INF:
    print(-1)
  else:
    print(D[g])
  
  
while True:
  try:
    n,k = list(map(int,input().split()))
    if n == 0 and k == 0:
      break
    E = [[] for i in range(n)]
    for i in range(k):
      lst = list(map(int,input().split()))
      a = min(lst[1],lst[2])
      b = max(lst[1],lst[2])
      if lst[0] == 0:
        solve(a - 1, b - 1, E, n)
      elif lst[0] == 1:
        E[a - 1].append([b - 1, lst[3]])
        E[b - 1].append([a - 1, lst[3]])
  except EOFError:
    break

Submission status: Runtime Error

Solution code:
from heapq import*
def g(s,g):
 F=[1e7]*-~n;F[s]=0
 H=[(0,s)]
 while H:
  c,u=heappop(H)
  if u==g:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:F[v]=t;heappush(H,(t,v))
 return-1
def s():
 for e in iter(input,'0 0'):
  n,k=map(int,e.split())
  E=[[]for _ in[0]*-~n]
  for _ in[0]*k:
   f=[*map(int,input().split())]
   if f[0]:
    c,d,e=f[1:]
    E[c]+=[(e,d)];E[d]+=[(e,c)]
   else:print(g(*f[1:]))
if'__main__'==__name__:s()

Submission status: Runtime Error

Solution code:
import sys
from heapq import*
s=sys.stdin.readline
def g(s,g):
 F=[1e7]*-~n;F[s]=0
 H=[(0,s)]
 while H:
  c,u=heappop(H)
  if u==g:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:F[v]=t;heappush(H,(t,v))
 return-1
for e in iter(s,'0 0'):
 n,k=map(int,e.split())
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=[*map(int,s().split())]
  if f[0]:
   c,d,e=f[1:]
   E[c]+=[(e,d)];E[d]+=[(e,c)]
  else:print(g(*f[1:]))

Submission status: Runtime Error

Solution code:
import sys
from heapq import*
def g(s,g):
 F=[1e7]*-~n;F[s]=0
 H=[(0,s)]
 while H:
  c,u=heappop(H)
  if u==g:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:
    F[v]=t
    heappush(H,(t,v))
 return-1
for e in iter(input,'0 0'):
 n,k=map(int,e.split())
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=input()
  if f[0]:
   c,d,e=map(int,f.split())
   E[c]+=[(e,d)];E[d]+=[(e,c)]
  else:print(g(map(int,f[2:])))

Submission status: Runtime Error

Solution code:
import sys
from heapq import*
def g(s,g):
 F=[1e7]*-~n;F[s]=0
 H=[(0,s)]
 while H:
  c,u=heappop(H)
  if u==g:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:
    F[v]=t
    heappush(H,(t,v))
 return-1
for e in iter(input,'0 0'):
 n,k=map(int,e.split())
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=input()
  if f[0]:
   c,d,e=map(int,f[2;].split())
   E[c]+=[(e,d)];E[d]+=[(e,c)]
  else:print(g(map(int,f[2:].split())))

Submission status: Runtime Error

Solution code:
import sys
from heapq import*
def g(s,g):
 F=[1e7]*-~n;F[s]=0
 H=[(0,s)]
 while H:
  c,u=heappop(H)
  if u==g:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:
    F[v]=t
    heappush(H,(t,v))
 return-1
for e in iter(input,'0 0'):
 n,k=map(int,e.split())
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=input()
  if f[0]:
   c,d,e=map(int,f[2:].split())
   E[c]+=[(e,d)];E[d]+=[(e,c)]
  else:print(g(map(int,f[2:].split())))

Submission status: Runtime Error

Solution code:
import sys
from heapq import*
def g(s,g):
 F=[1e7]*-~n;F[s]=0
 H=[(0,s)]
 while H:
  c,u=heappop(H)
  if u==g:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:
    F[v]=t
    heappush(H,(t,v))
 return-1
for e in iter(input,'0 0'):
 n,k=map(int,e.split())
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=input()
  if f[0]:
   c,d,e=map(int,f[2:].split())
   E[c]+=[(e,d)];E[d]+=[(e,c)]
  else:print(g(*map(int,f[2:].split())))

Submission status: Runtime Error

Solution code:
import sys
from heapq import*
def g(s,g):
 F=[1e7]*-~n;F[s]=0
 H=[(0,s)]
 while H:
  c,u=heappop(H)
  if u==g:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:
    F[v]=t
    heappush(H,(t,v))
 return-1
for e in iter(input,'0 0'):
 n,k=map(int,e.split())
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=input()
  if'0'==f[0]:
   c,d,e=map(int,f[2:].split())
   E[c]+=[(e,d)];E[d]+=[(e,c)]
  else:print(g(*map(int,f[2:].split())))

Submission status: Runtime Error

Solution code:
import sys
from heapq import*
r=sys.stdin.readline
def g(n,E,S,G):
 F=[1e7]*-~n;F[S]=0
 H=[(0,S)]
 while H:
  c,u=heappop(H)
  if u==G:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:
    F[v]=t
    heappush(H,(t,v))
 return-1
def s(n,k):
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r()
  if'0'==f[0]:print(g(n,E,*map(int,f[2:].split())))
  else:
   c,d,e=map(int,f[2:].split())
   E[c]+=[(e,d)];E[d]+=[(e,c)]
if'__main__'==__name__:s()
 for e in iter(r,'0 0\n'):s(*map(int,e.split()))

Submission status: Runtime Error

Solution code:
import sys
r=sys.stdin.readline
def g(n,E,S,G):
 from heapq import*
 F=[1e7]*-~n;F[S]=0
 H=[(0,S)]
 while H:
  c,u=heappop(H)
  if u==G:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:
    F[v]=t
    heappush(H,(t,v))
 return-1
def s(n,k):
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r()
  if'0'==f[0]:print(g(n,E,*map(int,f[2:].split())))
  else:
   c,d,e=map(int,f[2:].split())
   E[c]+=[(e,d)];E[d]+=[(e,c)]
if'__main__'==__name__:
 for e in iter(r,'0 0\n'):s(*map(int,e.split()))

Submission status: Runtime Error

Solution code:
from heapq import*
import sys
r=sys.stdin.readline
def g(n,E,S,G):
 F=[1e7]*-~n;F[S]=0;H=[(0,S)]
 while H:
  c,u=heappop(H)
  if u==G:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:F[v]=t;heappush(H,(t,v))
 return-1
def s(n,k):
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r()
  if'0'==f[0]:print(g(n,E,*map(int,f[2:].split())))
  else:c=[*map(int,f[2:].split())];E[c]+=[[e,d]];E[d]+=[[e,c]]
for e in iter(r,'0 0\n'):s(*map(int,e.split()))

Submission status: Runtime Error

Solution code:
from heapq import*
import sys
r=sys.stdin.readline
def g(n,E,S,G):
 F=[1e7]*-~n;F[S],H=0,[(0,S)]
 while H:
  c,u=heappop(H)
  if u==G:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:F[v]=t;heappush(H,(t,v))
 return-1
def s(n,k):
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=r();p=map(int,f[2:].split())
  if'0'==f[0]:print(g(n,E,*p))
  else:c,d,e=p;E[c],E[d]=E[c]+[(e,d)],E[d],[(e,c)]
for e in iter(r,'0 0\n'):s(*map(int,e.split()))

Submission status: Runtime Error

Solution code:
def main():
 from heapq import*
 import sys
 r=sys.stdin.readline
 def g(n,E,S,G):
  F=[1e7]*-~n;F[S],H=0,[(0,S)]
  while H:
   c,u=heappop(H)
   if u==G:return c
   for f,v in E[u]:
    t=c+f
    if t<F[v]:F[v]=t;heappush(H,(t,v))
  return-1
 def s(n,k):
  E=[[]for _ in[0]*-~n]
  for _ in[0]*k:
   f=r();p=map(int,f[2:].split())
   if'0'==f[0]:print(g(n,E,*p))
   else:c,d,e=p;E[c]+=[(e,d)];E[d]+=[(e,c)]
 for e in iter(r,'0 0\n'):s(*map(int,e.split()))
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
r=sys.stdin.readline
I=float('inf')
for e in iter(r,'0 0'):
 n,k=map(int,e.split())
 F=[[I]*-~n for _ in[0]*-~n]
 for i in range(1,n+1):F[i][i]=0
 for _ in[0]*k:
  f=r();g=map(int,f[2:].split())
  if'0'==f[0]:a,b=g;A=F[a][b];print([A,-1][A==I])
  else:
   c,d,e=g
   if e<F[c][d]:
    for i in range(2,n+1):
     for j in range(1,i):
      F[i][j]=F[j][i]=min(F[i][j],F[i][c]+e+F[d][j],F[i][d]+e+F[c][j])