Skip to content

Instantly share code, notes, and snippets.

@niyarin
Created June 17, 2014 03:41
Show Gist options
  • Save niyarin/4b2b320f61128c64d650 to your computer and use it in GitHub Desktop.
Save niyarin/4b2b320f61128c64d650 to your computer and use it in GitHub Desktop.
while True:
to_dic = {}
path_dic = {}
N = input()
if N==0:
break
for i in range(N):
A,B,C = raw_input().split(" ")
if to_dic.has_key(A):
to_dic[A].append(B)
else:
to_dic[A] = [B]
if to_dic.has_key(B):
to_dic[B].append(A)
else:
to_dic[B] = [A]
path_dic[A + B] = int(C)
path_dic[B + A] = int(C)
note = [ [-1 for i in range(len(to_dic) )] for j in range(len(to_dic))]
def Foo(area,money):
global note
if CNT == area:
note[CNT][area] = 0
for t in to_dic[str(area)]:
Foo(int(t),money + path_dic[str(area) + t])
return
if note[CNT][area] == -1:
note[CNT][area] = money
for t in to_dic[str(area)]:
Foo(int(t),money + path_dic[str(area) + t])
return
elif note[CNT][area] > money:
note[CNT][area] = money
for t in to_dic[str(area)]:
Foo(int(t),money + path_dic[str(area) + t])
return
for CNT in range(len(to_dic)):
Foo(CNT,0)
total_list = []
cnt = 0
for n in note:
sum = 0
for s in n:
sum += s
total_list.append([sum,cnt])
cnt += 1
total_list.sort()
print total_list[0][1],total_list[0][0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment