Problem:
 Score : 400 points    Problem Statement Joisino the magical girl has decided to turn every single digit that exists on this world into 1 .  Rewriting a digit i with j  (0≤i,j≤9) costs c_{i,j} MP (Magic Points).  She is now standing before a wall. The wall is divided into HW squares in H rows and W columns, and at least one square contains a digit between 0 and 9 (inclusive).  You are given A_{i,j} that describes the square at the i -th row from the top and j -th column from the left, as follows:   If A_{i,j}≠-1 , the square contains a digit A_{i,j} .  If A_{i,j}=-1 , the square does not contain a digit.   Find the minimum total amount of MP required to turn every digit on this wall into 1 in the end.      Constraints  1≤H,W≤200  1≤c_{i,j}≤10^3  (i≠j)  c_{i,j}=0  (i=j)  -1≤A_{i,j}≤9  All input values are integers.  There is at least one digit on the wall.         Input Input is given from Standard Input in the following format:  H  W  c_{0,0}  ...  c_{0,9}  :  c_{9,0}  ...  c_{9,9}  A_{1,1}  ...  A_{1,W}  :  A_{H,1}  ...  A_{H,W}       Output Print the minimum total amount of MP required to turn every digit on the wall into 1 in the end.        Sample Input 1 2 4
0 9 9 9 9 9 9 9 9 9
9 0 9 9 9 9 9 9 9 9
9 9 0 9 9 9 9 9 9 9
9 9 9 0 9 9 9 9 9 9
9 9 9 9 0 9 9 9 9 2
9 9 9 9 9 0 9 9 9 9
9 9 9 9 9 9 0 9 9 9
9 9 9 9 9 9 9 0 9 9
9 9 9 9 2 9 9 9 0 9
9 2 9 9 9 9 9 9 9 0
-1 -1 -1 -1
8 1 1 8      Sample Output 1 12  To turn a single 8 into 1 , it is optimal to first turn 8 into 4 , then turn 4 into 9 , and finally turn 9 into 1 , costing 6 MP.  The wall contains two 8 s, so the minimum total MP required is 6×2=12 .       Sample Input 2 5 5
0 999 999 999 999 999 999 999 999 999
999 0 999 999 999 999 999 999 999 999
999 999 0 999 999 999 999 999 999 999
999 999 999 0 999 999 999 999 999 999
999 999 999 999 0 999 999 999 999 999
999 999 999 999 999 0 999 999 999 999
999 999 999 999 999 999 0 999 999 999
999 999 999 999 999 999 999 0 999 999
999 999 999 999 999 999 999 999 0 999
999 999 999 999 999 999 999 999 999 0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1      Sample Output 2 0  Note that she may not need to change any digit.       Sample Input 3 3 5
0 4 3 6 2 7 2 5 3 3
4 0 5 3 7 5 3 7 2 7
5 7 0 7 2 9 3 2 9 1
3 6 2 0 2 4 6 4 2 3
3 5 7 4 0 6 9 7 6 7
9 8 5 2 2 0 4 7 6 5
5 4 6 3 2 3 0 5 4 3
3 6 2 3 4 2 4 0 8 9
4 6 5 4 3 5 3 2 0 8
2 1 3 4 5 7 8 6 4 0
3 5 2 6 1
2 5 3 2 1
6 9 2 5 6      Sample Output 3 47

Solution (Python):
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

h,w = inpl()
wall = []
mask = [] 
res = 0
for i in range(10):
    mask.append(inpl())
for y in range(h):
    wall.append(inpl())
for k in range(10):
    for i in range(10):
        for j in range(10):
            mask[i][j] = min(mask[i][j], mask[i][k] + mask[k][j])
for i in range(h):
    for j in range(w):
        tmp = wall[i][j]
        if abs(tmp) == 1:
            continue
        res += mask[tmp][1]
print(res)

Solution (Python):
inf = 1000
h,w = map(int,input().split())

dp = []
for i in range(10):
    c = list(map(int,input().split()))
    dp.append(c)

for k in range(10):
    for i in range(10):
        for j in range(10):
            dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j])

ans = 0
A = [list(map(int, input().split())) for _ in range(h)]

for i in range(h):
    for j in range(w):
        if A[i][j] not in [-1,1]:
            ans += dp[A[i][j]][1]

print(ans)

Solution (Python):
def resolve():
    H,_ = map(int,input().split())
    DP = [[2 ** 63 - 1 for _ in range(10)] for _ in range(10)]
    for i in range(10):
        DP[i] = list(map(int,input().split()))

    for k in range(10):
        for i in range(10):
            for j in range(10):
                DP[i][j] = min(DP[i][j],DP[i][k]+DP[k][j])

    import collections
    import itertools
    A = [list(map(int,input().split())) for _ in range(H)]
    c = collections.Counter(itertools.chain.from_iterable(A))
    ans = 0
    for i in range(10):
        ans += c[i] * DP[i][1] 
    print(ans)
resolve()

Solution (Python):
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)

def main():
    h, w = map(int, input().strip().split())
    c = [[0] * 10 for _ in range(10)]
    for i in range(10):
        c[i] = list(map(int, input().strip().split()))
    a = [[0] * w for _ in range(h)]
    for i in range(h):
        a[i] = list(map(int, input().strip().split()))

    ary = list(c)
    for k in range(10):
        for i in range(10):
            for j in range(10):
                ary[i][j] = min(ary[i][j], ary[i][k] + ary[k][j])
    ans = 0
    for i in range(h):
        for j in range(w):
            if a[i][j] not in (-1, 1):
                
                ans += ary[a[i][j]][1]
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
from scipy.sparse.csgraph import floyd_warshall
import sys
input = sys.stdin.readline

H, W = map(int, input().split())
c = [list(map(int, input().split())) for _ in range(10)]
a = [list(map(int, input().split())) for _ in range(H)]

cost = floyd_warshall(c)
ans = 0

for i in a:
    for j in i:
        if j != -1:
            ans += cost[j][1]

print(int(ans))

Solution (Python):
H,W=map(int,input().split())

co=[]
c=[0]*10
for i in range(10):
   temp=list(map(int,input().split()))
   co.append(temp)


for k in range(10):
    for i in range(10):
        for j in range(10):
            temp=[]
            if co[i][j] > co[i][k] + co[k][j]:
                co[i][j] = co[i][k] + co[k][j]

for i in range(10):
    c[i]=co[i][1]


ans=0
for i in range(H):
    A=list(map(int,input().split()))
    for j in range(W):
        if A[j]!=-1 and A[j]!=1:
            ans+=c[A[j]]
print(ans)

Solution (Python):
H,W=map(int,input().split())

co=[]
c=[0]*10
for i in range(10):
   temp=list(map(int,input().split()))
   co.append(temp)

f=True

while f:
    f=False
    for i in range(10):
        for j in range(10):
            temp=[]
            for k in range(10):
                if co[i][k]<co[i][j] and co[k][j]<co[i][j]:
                    temp.append(co[i][k]+co[k][j])
            if len(temp) >= 1:
                if co[i][j] > min(temp):
                    co[i][j] = min(temp)
                    f = True




for i in range(10):
    c[i]=co[i][1]


ans=0
for i in range(H):
    A=list(map(int,input().split()))
    for j in range(W):
        if A[j]!=-1 and A[j]!=1:
            ans+=c[A[j]]
print(ans)

Solution (Python):
h,w=map(int, input().split()) 
c=[list(map(int, input().split()))  for i in range(10)]
a=[list(map(int, input().split()))  for i in range(h)]



for k in range(10):
    for i in range(10):
        for j in range(10):
            c[i][j]=min(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 or a[i][j]==1:
            continue
        else:
            ans+=c[a[i][j]][1]
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(200000)

N, M = map(int, input().split())

convert=[]
for i in range(10):
    convert.append(list(map(int, input().split())))

paint=set([])
dicts={ i:0 for i in range(10)}
for i in range(N):
    input_temp = list(map(int, input().split()))
    temp=[]
    
    for j in input_temp:
        if j != -1:
            dicts[j] += 1
            temp.append(j)
    paint = paint | set(temp)


search_index = set([0,1,2,3,4,5,6,7,8,9])
def dfs(now_index, direct_distance, now_value, before_index, goal_index):
    
    if direct_distance < now_value:
        return direct_distance
    if now_index == goal_index:
        return now_value
    
    mins=[]
    temp_search_index = search_index - set([now_index, before_index])
    for i in temp_search_index:
        mins.append(dfs(i, 
                        direct_distance, 
                        now_value+convert[now_index][i], 
                        now_index, 
                        goal_index)
                   )
    return min(mins)

sums=0
for start in paint:
    if start != 1:
        sums += dicts[start] * dfs(start, convert[start][1], 0, -1, 1)

print(sums)

Solution (Python):
h, w = map(int, input().split( ))
c = []
for i in range(10):
    inpt = list(map(int, input().split( )))
    c.append(inpt)
a = []
for i in range(h):
    inpt = list(map(int, input().split( )))
    a.append(inpt)

dp = [[c[i][j] for j in range(10)] for i in range(10)]


for i in range(10):
    for j in range(10):
        for k in range(10):

            dp[j][k] = min(dp[j][i]+dp[i][k], dp[j][k])

ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j] !=-1:
            ans += dp[a[i][j]][1]

print(ans)

Solution (Python):
h,w=map(int,input().split())
C=[list(map(int,input().split())) for i in range(10)]
A=[list(map(int,input().split())) for j in range(h)]

for k in range(10):
    for i in range(10):
        for j in range(10):
            C[i][j]=min(C[i][j],C[i][k]+C[k][j])

cost=[C[i][1] for i in range(10)]+[0]
cnt=0
for i in range(h):
    for j in range(w):
        cnt +=cost[A[i][j]]
print(cnt)

Solution (Python):
h,w=map(int,input().split())
C=[list(map(int,input().split())) for i in range(10)]
A=[list(map(int,input().split())) for j in range(h)]
for k in range(10):
    for i in range(10):
        for j in range(10):
            C[i][j]=min(C[i][j],C[i][k]+C[k][j])
cost=[C[i][1] for i in range(10)]
cnt=0
for i in range(h):
    for j in range(w):
        add=A[i][j]
        if add!=-1:
            cnt +=cost[add]
print(cnt)

Solution (Python):
import copy
H, W = map(int, input().split())
c = [list(map(int, input().split())) for i in range(10)]
A = [list(map(int, input().split())) for i in range(H)]



cost = [-1]*10
cost[1] = 0

dp = [[-1]*10 for i in range(10)]
for i in range(10):
    dp[i][i] = 0


def search_cost(i, j, reached):
    
    
    if cost[i] != -1:
        return cost[i]
    tmp_list = copy.copy(reached)
    tmp_list.append(i)
    ret = c[i][j]

    for k in range(10):
        if k == i or k == 1 or k in reached:
            continue
        else:
            tmp = c[i][k] + search_cost(k, j, tmp_list)
            ret = min(tmp, ret)
            if ret <= 2:
                break
            

    
    
    
    return ret


for i in range(10):
    if i == 1:
        continue
    else:
        if c[i][1] <= 2:
            dp[i][1] = c[i][1]
            cost[i] = c[i][1]
        else:
            cost[i] = search_cost(i, 1, [])
ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] != -1:
            ans += cost[A[i][j]]
print(ans)

Solution (Python):

from collections import Counter
from collections import deque
h, w = map(int, input().split())

cost = [[] for _ in range(10)]
for i in range(10):
    row = map(int, input().split())
    for ind, v in enumerate(row):
        if ind == i:
            cost[i].append(float('inf'))
        else:
            cost[i].append(v)

nums = []
for _ in range(h):
    arow = list(map(int, input().split()))
    nums += arow
counter = Counter(nums)


def dijkstra(s,n,w,cost):
    
    
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        
        for i in range(n):
            if (not used[i]) and (v == -1):
               v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
               break
        used[v] = True
               
        for j in range(n):
               d[j] = min(d[j],d[v]+cost[v][j])
    return d

total = 0
for num, cnt in counter.items():
    if (num == -1) or (num == 1):
        continue
    else:
        dist = dijkstra(s=num, n=10, w=10*10, cost=cost)
        total += dist[1]*cnt
print(total)

Solution (Python):

import sys

def WarshallFloyd(V: int, cost: "List[List[int]]"):
    """
    全ての頂点対の間の最短距離を求めます。
    ==============
    V: 頂点数
    cost: 隣接行列(辺が存在しない部分はINF)
    --------------
    cost: 最短距離の行列
    """
    INF = 1e18
    for k in range(V):
        for i in range(V):
            for j in range(V):
                if cost[i][k] != INF and cost[k][j] != INF:
                    cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])
    return cost

def solve(H: int, W: int, c: "List[List[int]]", A: "List[List[int]]"):
    cost = WarshallFloyd(len(c), c)
    C = [0] * 10
    for a in A:
        for aa in a:
            if aa != -1:
                C[aa] += 1
    c = 0
    for i in range(10):
        c += cost[i][1] * C[i]
    print(c)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  
    W = int(next(tokens))  
    c = [[int(next(tokens)) for _ in range(9 - 0 + 1)] for _ in range(9 - 0 + 1)]  
    A = [[int(next(tokens)) for _ in range(W)] for _ in range(H)]  
    solve(H, W, c, A)

if __name__ == '__main__':
    main()

Solution (Python):
def resolve():
    h, w = map(int, input().split())
    c = list(list(map(int, input().split())) for _ in range(10))
    a = list(list(map(int, input().split())) for _ in range(h))
    for k in range(10):  
        for i in range(10):  
            for j in range(10):  
                c[i][j] = min(c[i][j], c[i][k] + c[k][j])
    costs = [c[i][1] for i in range(10)]
    costs += [0]  
    ans = 0
    for i in range(h):
        for j in range(w):
            ans += costs[a[i][j]]
    print(ans)


if __name__ == "__main__":
    resolve()

Solution (Python):
from sys import stdin
H, W = [int(_) for _ in stdin.readline().rstrip().split()]
C = [list(map(int, stdin.readline().rstrip().split())) for _ in range(10)]
A = [list(map(int, stdin.readline().rstrip().split())) for _ in range(H)]
D = [0]*10

def warshall_floyd():
    for k in range(10):
        for i in range(10):
            for j in range(10):
                C[i][j] = min(C[i][j], C[i][k] + C[k][j])

warshall_floyd()
for i in range(10):
    D[i] = C[i][1]
ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] >= 0:
            ans += D[A[i][j]]
print(ans)

Solution (Python):
h,w=map(int,input().split())
b=[list(map(int,input().split()))for _ in range(10)]
for i in range(10):
  for j in range(10):
    for k in range(10):b[j][k]=min(b[j][k],b[j][i]+b[i][k])
c=[b[i][1]for i in range(10)]+[0]
a=0
for i in range(h):
  for j in input().split():a+=c[int(j)]
print(a)

Solution (Python):
h, w = map(int, input().split())
c = []
for i in range(10):
    c_tmp = list(map(int, input().split()))
    c.append(c_tmp)
a = []
for i in range(h):
    a_tmp = list(map(int, input().split()))
    a += a_tmp

a2 = [_ for _ in a if _ not in [-1, 1]]

c_min = [c_tmp[1] for c_tmp in c]

while True:
    c_tmp = [_ for _ in c_min]
    for i in [0, 2, 3, 4, 5, 6, 7, 8, 9]:
        for j in [0, 2, 3, 4, 5, 6, 7, 8, 9]:
            if i == j:
                continue
            if c_min[i] >= c[i][j] + c_tmp[j]:
                c_min[i] = c[i][j] + c_tmp[j]

    if c_tmp == c_min:
        break

res = 0
for ai in a2:
    res += c_min[ai]
print(res)

Solution (Python):
h,w=map(int,input().split())
c=[list(map(int,input().split())) for _ in range(10)]
a=[list(map(int,input().split())) for _ in range(h)]
for k in range(10):
  for i in range(10):
    for j in range(10):
      c[i][j]=min(c[i][j],c[i][k]+c[k][j])
s=0
for i in range(h):
  for j in range(w):
    if a[i][j]!=-1:
      s+=c[a[i][j]][1]
print(s)

Solution (Python):
H,W = map(int,input().split())
C = []
for i in range(10):
  c = list(map(int,input().split()))
  C.append(c)
  
A = []
for i in range(H):
  a = list(map(int,input().split()))
  A.append(a)

for i in range(10):
  for j in range(10):
    for k in range(10):
      C[j][k] = min(C[j][k],C[j][i]+C[i][k])

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)

Solution (Python):
import numpy as np
from scipy.sparse.csgraph import csgraph_from_dense,dijkstra,floyd_warshall
H, W = map(int, input().split())

A = []
for _ in range(10):
  A.append(list(map(int, input().split())))
A = np.array(A)
c = []
for _ in range(H):
  c.append(list(map(int, input().split())))
c = np.array(c)
A = floyd_warshall(A)
C = A[0:,1]

ans = 0
for i in c:
  for j in i:
    if j != -1:
      ans += C[j]
print (int(ans))

Solution (Python):
import heapq

h,w = map(int,input().split())
C = [list(map(int,input().split())) for _ in range(10)]
A = [list(map(int,input().split())) for _ in range(h)]

distance = [10**6 for i in range(10)]
visited = [False for i in range(10)]
heap = []
cost = 0

distance[1] = 0
visited[1] = True
for i in range(10):
    heapq.heappush(heap,[C[i][1],i])

while heap:
    minedge = heapq.heappop(heap)
    if visited[minedge[1]]:
        continue
    v = minedge[1]
    distance[v] = minedge[0]
    visited[v] = True
    for i in range(10):
        if not visited[i]:
            heapq.heappush(heap,[C[i][v]+distance[v],i])

for i in range(h):
    for j in range(w):
        if A[i][j] != -1:
            cost += distance[A[i][j]]

print(cost)

Solution (Python):
def main():
    h, w = map(int, input().split())
    c = [list(map(int, input().split())) for _ in range(10)]
    a = [list(map(int, input().split())) for _ in range(h)]

    dp = c.copy()

    for k in range(10):
        for i in range(10):
            for j in range(10):
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])

    magic = 0
    for row in a:
        for num in row:
            if num == -1:
                continue
            magic += dp[num][1]

    print(magic)


if __name__ == "__main__":
    main()

Solution (Python):
H,W = map(int,input().split())
C = []
for i in range(10):
  c = list(map(int,input().split()))
  C.append(c)

A = []
for i in range(H):
  a =  list(map(int,input().split()))
  A.append(a)





















dist = C[:]
for i in range(10):
  for j in range(10):
    for k in range(10):
      dist[j][k] = min(dist[j][k],dist[j][i]+dist[i][k])

ans = 0
for i in range(H):
  for j in range(W):
   if(A[i][j] >= 0): 
     ans += dist[A[i][j]][1]
print(ans)

Solution (Python):
h, w = list(map(int, input().split()))  
power = [list(map(int, input().split())) for i in range(10)]  

for i in range(10):
    for j in range(10):
        for k in range(10):
            power[j][k] = min(power[j][i] + power[i][k], power[j][k])  

ans = 0
for i in range(h):
    wall = list(map(int, input().split()))  
    for item in wall:
        if item < 0:
            continue
        ans += power[item][1]  
print(ans)

Solution (Python):
import itertools
import sys
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
H,W = inputs()
c = [inputs() for _ in range(10)]
HW = [inputs() for _ in range(H)]
A = (0,9,8,7,6,5,4,3,2)
B = list(itertools.permutations(A))
N = [c[i][1] for i in range(10)]
for i in B:
    a = i[0]
    ans = 0
    for j in range(1,9):
        ans +=c[i[j-1]][i[j]]
        if N[a]<=ans:
          break
        N[a] = min(ans+c[i[j]][1],N[a])    
ans = 0
for i in range(H):
    for j in range(W):
        if HW[i][j]!=-1:
            ans += N[HW[i][j]]
print(ans)

Solution (Python):

import sys

INF = 100000000
def solve(H: int, W: int, c: "List[List[int]]", A: "List[List[int]]"):

    V = 10
    
    
    for k in range(V):
        for i in range(V):
            if c[i][k] == INF : continue
            for j in range(V):
                if c[k][j] == INF: continue
                c[i][j] = min(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)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  
    W = int(next(tokens))  
    c = [ [ int(next(tokens)) for _ in range(9-0+1) ] for _ in range(9-0+1) ]  
    A = [ [ int(next(tokens)) for _ in range(W) ] for _ in range(H) ]  
    solve(H, W, c, A)

if __name__ == '__main__':
    main()

Solution (Python):
h,w = map(int,input().split())
c = [list(map(int,input().split())) for l in range(10)]
a = []
res = 0
for _ in range(h):
    a.extend(list(map(int,input().split())))

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]

for sa in a:
    if sa != -1:
        res += c[sa][1]

print(res)

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7



H, W = map(int, input().split())
C = []
for i in range(9+1):
    c = list(map(int, input().split()))
    C.append(c)

A = []
for i in range(H):
    a = list(map(int, input().split()))
    A.append(a)

def warshall(org, V):
    d = org[:]

    for k in range(V):
        for i in range(V):
            for j in range(V):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])

    return d

V = 10
cost = warshall(C, V)


ans = 0
for h in range(H):
    for w in range(W):
        if 0 <= A[h][w]:
            ans += cost[A[h][w]][1]

print(ans)

Solution (Python):
magialist=[]
datalist=[]
h,w=map(int,input().split())
for i in range(10):
    add=list(map(int,input().split()))
    magialist.append(add)

for j in range(h):
    add1=list(map(int,input().split()))
    datalist.extend(add1)
 
storelist=[[[[] for i in range(10)] for i in range(10)] for i in range(10)]
    
for i in range(10):
    for j in range(10):
            a=min(magialist[i][j],magialist[i][0]+magialist[0][j])
            storelist[0][i][j]=a
k=1
while k<=9:
    for i in range(10):
        for j in range(10):
                a=min(storelist[k-1][i][j],storelist[k-1][i][k]+storelist[k-1][k][j])
                storelist[k][i][j]=a
    k+=1

uselist=[]
                                                                                        
for j in range(10):
    uselist.append(storelist[9][j][1])
ans=0
for nums in range(h*w):
        if datalist[nums]==-1:
            continue
        else:
            for j in range(10):
                if datalist[nums]==j:
                    ans+=uselist[j]
print(ans)

Solution (Python):
def calculate_mp(a, b, path, mp_sum):
    if a == b:
        return mp_sum
    cand = [k for k in range(10) if k not in path]
    mp_sum_list = []
    for c in cand:
        mp_sum_list.append(calculate_mp(c, b, path + [c], mp_sum + C[a][c]))
    return min(mp_sum_list)

H, W = [int(i) for i in input().split()]
C = []
for i in range(10):
    c = [int(i) for i in input().split()]
    C.append(c)
D = [0 for i in range(10)]
for i in range(10):
    D[i] = calculate_mp(i, 1, [i], 0)
cnt = 0
for i in range(H):
    A = [int(i) for i in input().split()]
    for a in A:
        if a != -1:
            cnt += D[a]
print(cnt)

Solution (Python):

import copy 

H, W = map(int, input().split())
c = [[int(_) for _ in input().split()] for _ in range(10)]
A = [[int(_) for _ in input().split()] for _ in range(H)]

d = copy.deepcopy(c)

for k in range(10):
    for i in range(10):
        for j in range(10):
            d[i][j] = min(d[i][j], d[i][k]+d[k][j])


ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] == 0 or A[i][j] >= 2:
            ans += d[A[i][j]][1]


print(ans)

Solution (Python):
def main():
    H, W = (int(i) for i in input().split())
    C = [[int(i) for i in input().split()] for j in range(10)]
    A = [[abs(int(i)) for i in input().split()] for j in range(H)]
    determined = set()
    from collections import deque
    notdetermined = deque()
    r = [0] * 10
    for k in range(10):
        r[k] = C[k][1]
    
    from itertools import permutations
    for sel in range(1,10):
        for p in permutations([0,2,3,4,5,6,7,8,9,],sel):
            cur = 0
            k = p[0]
            for i in p[1:]:
                cur += C[k][i]
                k = i
            else:
                cur += C[k][1]
            if cur < r[p[0]]:
                r[p[0]] = cur
    ans = 0
    for row in A:
        for a in row:
            ans += r[a]
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    H, W = (int(i) for i in input().split())
    C = [[int(i) for i in input().split()] for j in range(10)]
    A = [[abs(int(i)) for i in input().split()] for j in range(H)]
    determined = set()
    from collections import deque
    notdetermined = deque()
    r = [0] * 10
    for k in range(10):
        r[k] = C[k][1]
    
    from itertools import permutations
    for sel in range(1,10):
        for p in permutations([0,2,3,4,5,6,7,8,9,],sel):
            cur = 0
            k = p[0]
            for i in p[1:]:
                cur += C[k][i]
                k = i
            else:
                cur += C[k][1]
            if cur < r[p[0]]:
                r[p[0]] = cur
    ans = 0
    for row in A:
        for a in row:
            ans += r[a]
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    H, W = (int(i) for i in input().split())
    C = [[int(i) for i in input().split()] for j in range(10)]
    A = [[abs(int(i)) for i in input().split()] for j in range(H)]
    determined = set()
    from collections import deque
    notdetermined = deque()
    r = [0] * 10
    for k in range(10):
        r[k] = C[k][1]
    
    from itertools import permutations
    for sel in range(1,10):
        for p in permutations([0,2,3,4,5,6,7,8,9,],sel):
            cur = 0
            k = p[0]
            for i in p[1:]:
                cur += C[k][i]
                k = i
            else:
                cur += C[k][1]
            if cur < r[p[0]]:
                r[p[0]] = cur
    ans = 0
    for row in A:
        for a in row:
            ans += r[a]
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix


H,W = map(int,input().split())
C = [list(map(int,input().split())) for _ in range(10)]
A = [list(map(int,input().split())) for _ in range(H)]



csr = csr_matrix(C)
SP = shortest_path(csr)




dicA = {}
for i in range(H):
  for j in range(W):
    if A[i][j] != -1 and A[i][j] != 1:
      if A[i][j] in dicA:
        dicA[A[i][j]] += 1
      else:
        dicA[A[i][j]] = 1


ans = 0
for key,val in dicA.items():
  ans += SP[key][1] * val
print(int(ans))

Solution (Python):
h,w=map(int,input().split())
cost=[]
ans=[0]*10
for _ in range(10):
	x=list(map(int,input().split()))
	cost.append(x)
for _ in range(h):
	x=list(map(int,input().split()))
	for j in range(10):
		ans[j]+=x.count(j)

def warshall_floyd(d):
    
    n=10
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d
p=0
for i in range(10):
	p+=ans[i]*warshall_floyd(cost)[i][1]
print(p)

Solution (Python):
from scipy.sparse.csgraph import floyd_warshall

H, W = map(int, input().split())
C = [list(map(int, input().split())) for _ in range(10)]
grid = [list(map(int, input().split())) for _ in range(H)]

C = floyd_warshall(C)

ans = 0
for i in range(H):
    for j in range(W):
        if grid[i][j] != -1:
            ans += C[grid[i][j], 1]

print(int(ans))

Solution (Python):


h, w = map(int, input().split())
c = [list(map(int, input().split())) for _ in range(10)]
a = [list(map(int, input().split())) for _ in range(h)]

for k in range(10):
    for i in range(10):
        for j in range(10):
            c[i][j] = min(c[i][j], c[i][k] + c[k][j])

ans = 0
for row in a:
    for ele in row:
        if ele == -1:
            continue
        else:
            ans += c[ele][1]

print(ans)

Solution (Python):
def warsha_floyd(d):
    
    for k in range(N):
        for i in range(N):
            for j in range(N):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
                
    return d


H, W = map(int, input().split())  
N = 10
C = [list(map(int, input().split())) for _ in range(N)]  

A = [list(map(int, input().split())) for _ in range(H)]

C = warsha_floyd(C)

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)

Solution (Python):
import heapq

H,W = map(int,input().split())
C = [[int(x) for x in input().split()] for _ in range(10)]
num = [0] * 10
for _ in range(H):
    for x in input().split():
        if int(x) != -1:
            num[int(x)] += 1

que = []
flag = [True]*10
flag[1] = False
cost = [10**10]*10
cost[1] = 0

for i in range(10):
    if flag[i]:
        heapq.heappush(que,[C[i][1],i])

while que:
    a,b = heapq.heappop(que)
    if flag[b]:
        flag[b] = False
        cost[b] = a
        for i in range(10):
            if flag[i]:
                heapq.heappush(que,[C[i][b]+a,i])

ans = 0
for i in range(10):
    ans += num[i]*cost[i]
print(ans)

Solution (Python):
import copy

def main():
    H,W = map(int, input().split())
    c = [[int(i) for i in input().split()] for i in range(10)]
    a = [[int(i) for i in input().split()] for i in range(H)]
    

    
    for k in range(10):
        for i in range(10):
            for j in range(10):
                c[i][j] = min(c[i][j], c[i][k] + c[k][j])
    
    ans = 0
    for i in a:
        for j in i:
            if j > 1 or j == 0:
                ans += c[j][1]
    
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import copy

def main():
    H,W = map(int, input().split())
    c = [[int(i) for i in input().split()] for i in range(10)]
    a = [[int(i) for i in input().split()] for i in range(H)]
    d = copy.deepcopy(c)

    
    for k in range(10):
        for i in range(10):
            for j in range(10):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
    
    ans = 0
    for i in a:
        for j in i:
            if j > 1 or j == 0:
                ans += d[j][1]
    
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
H,W = map(int, input().split())
c, A = [], []
num = [0] * 10
for _ in range(10):
    ci = list(map(int, input().split()))
    ci[0], ci[1] = ci[1], ci[0]
    c.append(ci)
for _ in range(H):
    A += list(map(int, input().split()))
for i in [0] + list(range(2, 10)):
    num[i] += A.count(i)

c = [[0]] + c[:1] + c[2:]
num[0], num[1] = num[1], num[0]

dp = [[c[i][0] for i in range(10)]] + [[10000 for i in range(10)] for j in range(9)]
for i in range(1, 10):
    for j in range(1, 10):
            dp[i][j] = min(c[j][k] + dp[i-1][k] for k in range(1, 10))

print(sum(dp[9][i] * num[i] for i in range(1,10)))

Solution (Python):
h,w=map(int,input().split())
c=[list(map(int,input().split())) for i in range(10)]

for k in range(10):
  for i in range(10):
    for j in range(10):
      c[i][j]=min(c[i][j],c[i][k]+c[k][j])

ans=0

for i in range(h):
  li=list(map(int,input().split()))
  for a in li:
    if a!=-1:
      ans+=c[a][1]
print(ans)

Solution (Python):
H,W=map(int,input().split())
S=[list(map(int, input().split())) for i in range(10)]
l=[None]*10
for i in range(10):
    l[i]=S[i][1]
for i in range(5):
    for j in range(10):
        for k in range(10):
            if l[j]>l[k]+S[j][k]:
                l[j]=l[k]+S[j][k]
l.append(0)
a=0
for i in range(H):
    L=list(map(int,input().split()))
    for j in L:
        a+=l[j]
print(a)