Problem p03546 (python)

Fixed submission:
import math
st=input().split()
H=int(st[0]);W=int(st[1])
c=[[0 for i in range(10)] for i in range(10)]
A=[[0 for i in range(W)]for i in range(H)]
for i in range(10):
    st=input().split()
    for j in range(10):
        c[i][j]=int(st[j])
for i in range(H):
    st=input().split()
    for j in range(W):
        A[i][j]=int(st[j])
def Dijkstra(s):
    d=[10000]*10
    d[s]=0
    Q=set(range(10))
    V=set(range(10))
    while(len(Q)>0):
        u=list(Q)[0]
        for i in Q:
            if d[i]<d[u]:
                u=i
        Q-={u}
        for v in V-{u}:
            if (d[v]>d[u]+c[u][v]):
                d[v]=d[u]+c[u][v]
    return d[1]
result=0
DD=[0]*10
for i in range(10):
    DD[i]=Dijkstra(i)
for i in range(H):
    for j in range(W):
        if A[i][j]!=-1:
            result+=DD[A[i][j]]
print(result)

Buggy submission (verdict: Runtime Error):
H,W=map(int,input().split())
c=[map(int,input().split()) for i in range(10)]
A=[map(int,input().split()) for i in range(H)]
for k in range(10):
  for i in range(10):
    for j in range(10):
      if c[i][j]<c[i][k]+c[k][j]:
        c[i][j]=c[i][k]+c[k][j]
ans=0
for i in range(H):
  for j in range(W):
    if A[i][j]!=-1:
      ans+=c[A[i][j]][1]
print(ans)

Buggy submission (verdict: Runtime Error):
    st=input().split()
    H=int(st[0]);W=int(st[1])
    c=[[0 for i in range(10)] for i in range(10)]
    A=[[0 for i in range(W)]for i in range(H)]
    for i in range(10):
        st=input().split()
        for j in range(10):
            c[i][j]=int(st[j])
    for i in range(H):
        st=input().split()
        for j in range(W):
            A[i][j]=int(st[j])
    import math
    def Dijkstra(s):
        d=[math.inf]*10
        d[s]=0
        Q=list(range(10))
        V=list(range(10))
        while(len(Q)>0):
            u=Q[0]
            for i in Q:
                if d[i]<d[u]:
                    u=i
            Q.remove(u)
            Vtmp=list(range(10))
            Vtmp.remove(u)
            for v in Vtmp:
                if (d[v]>d[u]+c[u][v]):
                    d[v]=d[u]+c[u][v]
        return d[1]
    result=0
    DD=[0]*10
    for i in range(10):
        DD[i]=Dijkstra(i)
    for i in range(H):
        for j in range(W):
            if A[i][j]!=-1:
                result+=DD[A[i][j]]
    print(result)

Buggy submission (verdict: Runtime Error):
st=input().split()
H=int(st[0]);W=int(st[1])
c=[[0 for i in range(10)] for i in range(10)]
A=[[0 for i in range(W)]for i in range(H)]
for i in range(10):
    st=input().split()
    for j in range(10):
        c[i][j]=int(st[j])
for i in range(H):
    st=input().split()
    for j in range(W):
        A[i][j]=int(st[j])
import math
def Dijkstra(s):
    d=[math.inf]*10
    d[s]=0
    Q=set(range(10))
    V=set(range(10))
    while(len(Q)>0):
        u=list(Q)[0]
        for i in Q:
            if d[i]<d[u]:
                u=i
        Q-={u}
        for v in V-{u}:
            if (d[v]>d[u]+c[u][v]):
                d[v]=d[u]+c[u][v]
    return d[1]
result=0
DD=[0]*10
for i in range(10):
    DD[i]=Dijkstra(i)
for i in range(H):
    for j in range(W):
        if A[i][j]!=-1:
            result+=DD[A[i][j]]
print(result)

Buggy submission (verdict: Runtime Error):
st=input().split()
H=int(st[0]);W=int(st[1])
c=[[0 for i in range(10)] for i in range(10)]
A=[[0 for i in range(W)]for i in range(H)]
for i in range(10):
    st=input().split()
    for j in range(10):
        c[i][j]=int(st[j])
for i in range(H):
    st=input().split()
    for j in range(W):
        A[i][j]=int(st[j])
import math
def Dijkstra(s):
#    import pdb;pdb.set_trace()
    d=[math.inf]*10
    d[s]=0
    Q=set(range(10))
    V=set(range(10))
    while(len(Q)>0):
        u=list(Q)[0]
        for i in Q:
            if d[i]<d[u]:
                u=i
        Q-={u}
        for v in V-{u}:
            if (d[v]>d[u]+c[u][v]):
                d[v]=d[u]+c[u][v]
    return d[1]
result=0
DD=[0]*10
for i in range(10):
    DD[i]=Dijkstra(i)
for i in range(H):
    for j in range(W):
        if A[i][j]!=-1:
            result+=DD[A[i][j]]
print(result)