Problem p03608 (python)

Fixed submission:
from collections import *
from itertools import *
import copy
from heapq import *


N,M,R=map(int,input().split())
Rlst=list(map(int,input().split()))
ABC=[list(map(int,input().split())) for i in range(M)]

data=[[] for i in range(N+1)]
nagasa={}
for a,b,c in ABC:
    data[a].append(b)
    data[b].append(a)
    A,B=min(a,b),max(a,b)
    nagasa.update({str(A)+" "+str(B):c})
mokutekiti=[[100000*200+1 for i in range(R)] for m in range(R)]
for r1i in range(R-1):
    for r2i in range(r1i+1,R):
        r1=Rlst[r1i]
        r2=Rlst[r2i]
        visited=set()
        stack=[0]
        heapify(stack)
        dic={0:[r1]}
        while stack:
            m=heappop(stack)
            #if not m in visited:
            #visited.add(m)
            a=dic[m].pop()
            if not a in visited:
                visited.add(a)
                for b in data[a]:
                    if not b in visited or b==r2:
                        A,B=min(a,b),max(a,b)
                        tempm=m+nagasa[str(A)+" "+str(B)]
                        if b==r2:
                            if mokutekiti[r1i][r2i]>tempm:
                                mokutekiti[r1i][r2i]=tempm
                                mokutekiti[r2i][r1i]=tempm
                        else:
                            heappush(stack,tempm)
                            if tempm in dic:
                                dic[tempm].append(b)
                            else:
                                dic.update({tempm:[b]})



value=[]
for lst in permutations([i for i in range(R)],R):
    nagasa=0
    for i in range(R-1):
        nagasa+=mokutekiti[lst[i]][lst[i+1]]
    value.append(nagasa)
print(min(value))

Buggy submission (verdict: Runtime Error):
from collections import *
from itertools import *
import copy

N,M,R=map(int,input().split())
Rlst=list(map(int,input().split()))
ABC=[list(map(int,input().split())) for i in range(M)]

data=[[] for i in range(N+1)]
nagasa={}
for a,b,c in ABC:
    data[a].append(b)
    data[b].append(a)
    A,B=min(a,b),max(a,b)
    nagasa.update({str(A)+" "+str(B):c})
mokutekiti=[[0 for i in range(R)] for m in range(R)]
for r1i in range(R-1):
    for r2i in range(r1i+1,R):
        r1=Rlst[r1i]
        r2=Rlst[r2i]
        stack=deque([[r1,set([r1]),r1]])
        visited=set([r1])
        ikulst=set()
        while stack:
            a,miti,mae=stack.popleft()
            for b in data[a]:
                if b!=mae:
                    if not b in visited:
                        visited.add(b)
                        tempmiti=copy.copy(miti)
                        tempmiti.add(b)
                        if b==r2:
                            ikulst=ikulst|tempmiti
                        else:
                            stack.append([b,tempmiti,a])
                    elif b in ikulst:
                        ikulst=ikulst|miti


        """
        stack=[[r1,0]]
        minpoint=[100000*200+1 for i in range(N+1)]
        while stack:
            a,m=stack.pop()
            for b in data[a]:
                if b in ikulst:
                    A,B=min(a,b),max(a,b)
                    tempm=m+nagasa[str(A)+" "+str(B)]
                    if tempm<minpoint[b]:
                        minpoint[b]=tempm
                        if b==r2:
                            pass
                        else:
                            stack.append([b,tempm])"""
        mokutekiti[r1i][r2i]=minpoint[r2]
        mokutekiti[r2i][r1i]=minpoint[r2]

value=[]
for lst in permutations([i for i in range(R)],R):
    nagasa=0
    for i in range(R-1):
        nagasa+=mokutekiti[lst[i]][lst[i+1]]
    value.append(nagasa)
print(min(value))

Buggy submission (verdict: Runtime Error):
from collections import *
from itertools import *
import copy

N,M,R=map(int,input().split())
Rlst=list(map(int,input().split()))
ABC=[list(map(int,input().split())) for i in range(M)]

data=[[] for i in range(N+1)]
nagasa={}
for a,b,c in ABC:
    data[a].append(b)
    data[b].append(a)
    A,B=min(a,b),max(a,b)
    nagasa.update({str(A)+" "+str(B):c})
mokutekiti=[[0 for i in range(R)] for m in range(R)]
for r1i in range(R-1):
    for r2i in range(r1i+1,R):
        r1=Rlst[r1i]
        r2=Rlst[r2i]
        mitilst=[]
        stack=[[r1,0]]
        minpoint=[100000 for i in range(N+1)]
        while stack:
            a,m=stack.pop()
            for b in data[a]:
                A,B=min(a,b),max(a,b)
                tempm=m+nagasa[str(A)+" "+str(B)]
                if tempm<minpoint[b]:
                    minpoint[b]=tempm
                    if b==r2:
                        mitilst.append(tempm)
                    else:
                        stack.append([b,tempm])
        mitilst=min(mitilst)
        mokutekiti[r1i][r2i]=mitilst
        mokutekiti[r2i][r1i]=mitilst

value=[]
for lst in permutations([i for i in range(R)],R):
    nagasa=0
    for i in range(R-1):
        nagasa+=mokutekiti[lst[i]][lst[i+1]]
    value.append(nagasa)
print(min(value))

Buggy submission (verdict: Runtime Error):
from collections import *
from itertools import *
import copy

N,M,R=map(int,input().split())
Rlst=list(map(int,input().split()))
ABC=[list(map(int,input().split())) for i in range(M)]

data=[[] for i in range(N+1)]
nagasa={}
for a,b,c in ABC:
    data[a].append(b)
    data[b].append(a)
    A,B=min(a,b),max(a,b)
    nagasa.update({str(A)+" "+str(B):c})
mokutekiti=[[0 for i in range(R)] for m in range(R)]
for r1i in range(R-1):
    for r2i in range(r1i+1,R):
        r1=Rlst[r1i]
        r2=Rlst[r2i]
        mitilst=[]
        stack=[[r1,[r1],0]]
        minpoint=[100000 for i in range(N+1)]
        while stack:
            a,miti,m=stack.pop()
            for b in data[a]:
                A,B=min(a,b),max(a,b)
                tempm=m+nagasa[str(A)+" "+str(B)]

                if tempm<minpoint[b]:
                    minpoint[b]=tempm
                    tempmiti=copy.copy(miti)
                    tempmiti.append(b)
                    if b==r2:
                        mitilst.append(tempm)
                    else:
                        stack.append([b,tempmiti,tempm])
        mitilst=min(mitilst)
        mokutekiti[r1i][r2i]=mitilst
        mokutekiti[r2i][r1i]=mitilst

value=[]
for lst in permutations([i for i in range(R)],R):
    nagasa=0
    for i in range(R-1):
        nagasa+=mokutekiti[lst[i]][lst[i+1]]
    value.append(nagasa)
print(min(value))