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):
H, W = map(int, input().split())

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

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


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

Solution (Python):

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
mod=10**9+7

def main():
    H,W=MI()
    c=[[] for _ in range(10)]
    A=[[] for _ in range(H)]
    
    for i in range(10):
        c[i]=LI()
    for i in range(H):
        A[i]=LI()
        
    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
    
    c=warshall_floyd(c)
    
    ans=0
    for i in range(H):
        for j in range(W):
            if A[i][j]==-1:
                continue
            else:
                ans+=c[A[i][j]][1]
                
    print(ans)
            
    
    

main()

Solution (Python):



H,W = map(int, input().split())

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


for k in range(10):
    for i in range(10):
        for j in range(10):
            convert[i][j] = min(convert[i][j], convert[i][k] + convert[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 += convert[A[i][j]][1]

print(ans)

Solution (Python):
def main():
    H,W = list(map(int,input().split()))
    magic = [list(map(int,input().split())) for i in range(10)]
    wall = [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):
                magic[i][j] = min(magic[i][j],magic[i][k]+magic[k][j])
    
    ans = 0
    for h in range(H):
        for w in range(W):
            if wall[h][w] != -1:
                ans += magic[wall[h][w]][1]
    print(ans)
            
if __name__ == '__main__':
    main()

Solution (Python):
import queue

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

change_list=[]
for i in range(10):
    if i==1:
        change_list.append(0)
    else:
        MIN=c[i][1]
        sub=queue.Queue()
        for g in range(10):
            if i!=g and g!=1 and c[i][g]<c[i][1]:
                sub.put((g,c[i][g]))
        while not sub.empty():
            now,_sum=sub.get()
            MIN=min(_sum+c[now][1],MIN)
            for l in range(10):
                if now!=l and l!=1 and _sum+c[now][l]<MIN:
                    sub.put((l,_sum+c[now][l]))
        change_list.append(MIN)

ans=0
for g in range(H):
    a=list(map(int,input().split()))
    for l in range(W):
        if a[l]!=-1:
            ans+=change_list[a[l]]

print(ans)

Solution (Python):
def warshall_floyd(N,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())
C = []
for i in range(10):
	C.append([int(x) for x in input().split()])
C = warshall_floyd(10,C)
A = []
for i in range(H):
	A.append([int(x) for x in input().split()])

ans = 0
for i in range(H):
	for j in range(W):
		a = A[i][j]
		if a<0:
			continue
		ans += C[a][1]
print(ans)

Solution (Python):
def warshall_floyd(d, n):
    
    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())
c = [list(map(int, input().split())) for _ in range(10)]
a = [list(map(int, input().split())) for _ in range(H)]
n = 10

d = warshall_floyd(c, n)

ans = 0
for i in range(H):
    for j in range(W):
        s = a[i][j]
        if s == 1 or s == -1:
            continue
        ans += d[s][1]
print(ans)

Solution (Python):









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

v = 10

h, w = list(map(int, input().split()))

d = [list(map(int, input().split())) for i in range(10)]
wall = [list(map(int, input().split())) for i in range(h)]

warshall_floyd(d, v)

ans =0
for i in range(h):
    for j in range(w):
        num = wall[i][j]
        if num == -1:
            pass
        else:
            
            ans += d[num][1]

print(ans)

Solution (Python):

h,w = map(int,input().split())
inf = 10**9
wf = []
for i in range(10):
  wf.append(list(map(int,input().split())))
for k in range(10):
  for i in range(10):
    for j in range(10):
      wf[i][j] = min(wf[i][j],wf[i][k]+wf[k][j])
z1 = []
for i in range(10):
  z1.append(wf[i][1])
cnt = [0 for _ in range(10)] 
for _ in range(h):
  tmp = list(map(int,input().split()))
  for y in range(w):
    if tmp[y] != -1:
      cnt[tmp[y]] += 1
ans = 0
for i in range(10):
  ans += z1[i] * cnt[i]



print(ans)

Solution (Python):
import copy

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

num_dict = {}
for i in range(-1,10):
    new = {i:0}
    num_dict.update(new)

for i in range(h):
    A = list(map(int, input().split()))
    for a in A:
        num_dict[a] += 1

DP = copy.deepcopy(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
for m in range(0, 10):
    ans += DP[m][1] * num_dict[m]

print(ans)

Solution (Python):
from copy import deepcopy

def make_multi_list(initial, degree):
    ans = [initial for _ in range(degree[-1])]
    for d in reversed(degree[:-1]):
        ans = [deepcopy(ans) for _ in range(d)]
    return ans


H, W = map(int, input().split())
C = []
INF = float('inf')
dist = make_multi_list(INF, [10, 10])

for i in range(10):
    C.append(list(map(int, input().split())))


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

ans = 0
for i in range(H):
    for n in map(int, input().split()):
        if n == -1:
            continue
        ans += dist[n][1]

print(ans)

Solution (Python):
H, W = map(int,input().split())
dp = [[None]*10 for _ in range(10)]
for i in range(10):
    ls = list(map(int,input().split()))
    for j in range(10):
        dp[i][j] = ls[j]
from collections import defaultdict
d = defaultdict(int)
for i in range(H):
    for a in map(int,input().split()):
        d[a] += 1
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
for i in range(10):
    if i == 1:
        continue
    ans += dp[i][1]*d[i]
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 row in A:
    for a in row:
        if a==-1: continue
        ans += C[a][1]
print(ans)

Solution (Python):
h, _ = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(10)]
A = [list(map(int, input().split())) for _ in range(h)]

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])

ans = 0
for aa in A:
    for a in aa:
        if a != -1:
            ans += d[a][1]

print(ans)

Solution (Python):
class WarshallFloyd:
    def __init__(self,n):
        self.n = n
        self.inf = float('inf')
        self.d = [[self.inf for _ in range(n)] for _ in range(n)]
        for i in range(self.n):
            self.d[i][i] = 0
    def register(self,v1,v2,l,multiple=True):
        self.d[v1][v2] = l
        if multiple:
            self.d[v2][v1] = l
    def solve(self):
        for i in range(self.n):
            for j in range(self.n):
                for k in range(self.n):
                    self.d[j][k] = min(self.d[j][k],self.d[j][i]+self.d[i][k])
        return self.d
H,W = map(int,input().split())
C = []
A = []
WF = WarshallFloyd(10)
for i in range(10):
    C.append(list(map(int,input().split())))
for i in range(10):
    for j in range(10):
        WF.register(i,j,C[i][j],multiple=False)
C = WF.solve()
for i in range(H):
    A.append(list(map(int,input().split())))
ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] == -1:
            pass
        else:
            ans += C[A[i][j]][1]
print(ans)

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

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

numbers=[0 for i in range(0,10)]
for i in range(0,H):
    for j in range(0,W):
        if A[i][j]!=-1:
            numbers[A[i][j]]+=1

print(sum(numbers[i]*C[i][1] for i in range(0,10)))

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline



INF = 1e10
H, W = list(map(int, input().split()))

dp = [[INF] * 10 for _ in range(10)]

for i in range(10):
    for j, v in enumerate([int(_) for _ in input().split()]):
        dp[i][j] = v

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
for h in range(H):
    for w in [int(_) for _ in input().split()]:
        if w == -1: continue
        ans += dp[w][1]
print(ans)

Solution (Python):

def search(start: int, visited: list, power: list, min_power: int, min_powers: int) -> int:
    minimum = 10**4
    if min_powers[start] != -1:
        return min_powers[start] + min_power
    if visited[1] == True:
        
        return min_power
    for i, v in enumerate(visited):
        if not v:
            visited[i] = True
            min_power += power[start][i]

            
            minimum = min(minimum, search(i,visited,power,min_power,min_powers))

            visited[i] = False
            min_power -= power[start][i]
            
    return minimum


H, W = map(int, input().split())

power = [[] for _ in range(10)]

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

world = [[] for _ in range(H)]

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

min_powers = [-1] * 10
visited = [False] * 10


for i in range(10):
    min_power = 0
    visited[i] = True
    min_powers[i] = search(i,visited,power,min_power,min_powers)
    visited[i] = False
    
for i in range(10):
    pass
    
ans = 0
for wor in world:
    for w in wor:
        if w != -1:
            ans += min_powers[w]

print(ans)

Solution (Python):
H,W=map(int,input().split())
c=[list(map(int,input().split())) for i in range(10)]
Gd=[]
for i in range(H):
  Gd=Gd+list(map(int,input().split()))
dp = [[[10000]*10 for i in range(10)] for t in range(10)]
dp[0] = c[:]
for t in range(1,10):
  for i in range(10):
    for j in range(10):
      for k in range(10):
        dp[t][i][j]=min(dp[t][i][j],dp[t-1][i][k]+c[k][j])
ret = 0
for i in Gd:
  if i>=0:
    ret+=dp[-1][i][1]
print(ret)

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


def dfs(now, mp, rest):
    if now == 1:
        return mp
    ret = [
        dfs(p, mp + cost[now][p], rest[:i] + rest[i+1:]) for i, p in enumerate(rest)
    ]
    return min(ret)


r = list(range(10))
magic = [dfs(i, 0, r[:i] + r[i+1:]) for i in range(10)]

ans = 0
for _ in range(h):
    x = list(map(int, input().split()))
    for a_ij in x:
        if a_ij == -1:
            pass
        else:
            ans += magic[a_ij]
print(ans)

Solution (Python):
H, W = map(int, input().split())
C = [[0 for _ in range(10)] for _ in range(10)]
for i in range(10):
    for j, c in enumerate(input().split()):
        C[i][j] = int(c)
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 A in input().split():
        A = int(A)
        if A >= 0:
            ans += C[A][1]

print(ans)

Solution (Python):
from collections import deque

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

total = 0
memo = [-1] * 10
for A in AA:
    for a in A:
        if a == -1:
            continue
        if memo[a] != -1:
            total += memo[a]
        else:
            best = CC[a][1]
            q = deque()
            q.append((a, 0))
            used = {a}
            while q:
                node, cost = q.popleft()
                for to in range(10):
                    new_cost = cost + CC[node][to]
                    if to == node or new_cost >= best:
                        continue
                    if to == 1:
                        if new_cost < best:
                            best = new_cost
                    else:
                        q.append((to, new_cost))
                        used.add(to)
            memo[a] = best
            total += memo[a]
print(total)

Solution (Python):
import heapq as hq
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)]

edges = [[] for _ in range(10)]
for i in range(10):
    for j in range(10):
        edges[j].append((c[i][j],i))

dis = [10**10] * 10
dis[1] = 0
q = []
hq.heappush(q, (0,1))

while q:
    dis1,p = hq.heappop(q)
    for dis2,p2 in edges[p]:
        if dis[p2] > dis[p] + dis2:
            dis[p2] = dis[p] + dis2
            hq.heappush(q, (dis[p2], p2))

ans = 0

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

print(ans)

Solution (Python):
import sys
input = sys.stdin.buffer.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)]

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])

answer = 0
for a in A:
    for aa in a:
        if aa != -1:
            answer += C[aa][1]

print(answer)

Solution (Python):
import sys
input = sys.stdin.buffer.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)]

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])

answer = 0
for a in A:
    for aa in a:
        if aa != -1:
            answer += C[aa][1]

print(answer)

Solution (Python):
def warshall_floyd(d):
    n = len(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())
c = []
for _ in range(10):
    c.append(list(map(int, input().split())))

d = warshall_floyd(c)
ans = 0
for _ in range(H):
    line = map(int, input().split())
    for v in line:
        if v != -1:
            ans += d[v][1]


print(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)]

N  = 10
d = c
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])

ans = 0
for aa in A:
    for a in aa:
        if a!=-1:
           ans += d[a][1]
           
print(ans)

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


sys.setrecursionlimit(1000000)
MOD = int(1e09) + 7
INF = int(1e30)


def main():
    
    H, W = Scanner.map_int()
    C = Scanner.int_list_list(10)
    A = Scanner.int_list_list(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 h in range(H):
        for w in range(W):
            if A[h][w] == -1:
                continue
            ans += C[A[h][w]][1]
    print(ans)
    return


if __name__ == "__main__":
    main()

Solution (Python):
H,W=map(int,input().split())
grid=[None]*10
for i in range(10):
  grid[i]=list(map(int,input().split()))

for k in range(10):
  for i in range(10):
    for j in range(10):
      if grid[i][k]+grid[k][j]<grid[i][j]:
        grid[i][j]=grid[i][k]+grid[k][j]
        
ans=0
for i in range(H):
  A=list(map(int,input().split()))
  for a in A:
    if a==-1:
      continue
    ans+=grid[a][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 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)

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

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

from scipy.sparse.csgraph import csgraph_from_dense,dijkstra

G = csgraph_from_dense(c, null_value=0)

l = []
for i in range(10):
    d = dijkstra(G, indices=i)
    l.append(d[1])
ans = 0
for _ in range(H):
    A = list(map(int, input().split()))
    for a in A:
        if a != -1 and a != 1:
            ans += l[a]

print(int(ans))

Solution (Python):
import heapq

INF = 10**9

def solve():
  
  H,W = map(int, input().split())
  graph = [list(map(int, input().split())) for _ in range(10)]
  search_table = [[i != 1 and i != j for i in range(10)] for j in range(10)]
  min_distance_n_to_1 = [INF]*10
  min_distance_n_to_1[1] = 0
  
  
  queue = [(min_distance_n_to_1[v], v) for v in range(10)]
  heapq.heapify(queue)

  while queue:
    _, dom = heapq.heappop(queue)
    
    for i in range(10):
      
      
      
      if min_distance_n_to_1[i] > min_distance_n_to_1[dom] + graph[i][dom]:
        min_distance_n_to_1[i] = min_distance_n_to_1[dom] + graph[i][dom]
        heapq.heappush(queue, (min_distance_n_to_1[i], i))

  
  ret = sum(sum(min_distance_n_to_1[int(Aij)] if int(Aij) != -1 else 0 for Aij in input().split()) for _ in range(H))
  print(ret)
  
solve()

Solution (Python):
def warshall_floyd(d,n):
    
    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())
ls = [0]*10
cost = [[10**5]*10 for i in range(10)]
dist = [0]*10
for i in range(10):
  inf = [int(c) for c in input().split()]
  for j in range(10):
    cost[i][j] = inf[j]
dist = warshall_floyd(cost,10)
cnt = [0]*(12)
for i in range(H):
  inf = [int(c) for c in input().split()]
  for j in range(W):
    cnt[inf[j]] += 1
ans = 0
for i in range(10):
  ans += dist[i][1]*cnt[i]
print(ans)

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)]
a = [list(map(int, input().split())) for _ in range(H)]

wf = floyd_warshall(c)
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 += wf[a[i][j]][1]

print(int(ans))

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

from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, csgraph_from_dense





input = sys.stdin.readline


def main():
    H, W = [int(x) for x in input().split()]
    edge = [[int(x) for x in input().split()] for _ in range(10)]
    A = [[int(x) for x in input().split()] for _ in range(H)]

    G = csgraph_from_dense(edge, null_value=0)
    d = floyd_warshall(G)

    ans = 0
    for a in A:
        for v in a:
            if v == -1:
                continue
            ans += d[v][1]

    print(int(ans))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
h,w = map(int,readline().split())
lst1 = [list(map(int,readline().split())) for i in range(10)]


from scipy.sparse.csgraph import shortest_path,floyd_warshall
from scipy.sparse import csr_matrix
lc = csr_matrix(lst1)
cost_lst = shortest_path(lc,method="FW")


ans = 0
for i in range(h):
    lst2 = list(map(int,readline().split()))
    for j in lst2:
        if j != -1:
            ans += cost_lst[j][1]

print(int(ans))

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

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

wall = []
for _ in range(h):
  wa = list(map(int, input().split()))
  wall.append(list(wa))
  
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])

total_cost = 0
for i in range(h):
  for j in range(w):
    num = wall[i][j]
    if num != -1:
      total_cost += c[num][1]
print(total_cost)

Solution (Python):
h,w=map(int,input().split())
cost=[list(map(int,input().split())) for i in range(10)]
wall=[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):
      cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j])

tot_cost=0
for i in range(h):
  for j in range(w):
    num=wall[i][j]
    if num!=-1:
      tot_cost+=cost[num][1]
print(tot_cost)

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

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)]

d=floyd_warshall(c)
ans=0
for i in range(h):
 for j in range(w):
  num=a[i][j]
  if num!=1 and num!=-1:
   ans+=d[num][1]
print(int(ans))

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

costs=[10**10]*10
costs[1]=0

used=[]
used.append(1)
now=1

while True:
    for i in range(0,10):
        if i in used:
            continue
        if costs[i] > c[i][now]+costs[now]:
            costs[i] = c[i][now]+costs[now]

    argmin=-1
    costmin=10**5
    for i in range(0,10):
        if i in used:
            continue
        if costs[i] < costmin:
            costmin=costs[i]
            argmin=i
    if argmin==-1:
        break
    now=argmin
    used.append(now)

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

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


def warshall_floyd(d,n):
    
    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
    
d=warshall_floyd(field,10)

ans=0
for i in wall:
    for j in i:
        if j!=-1 and j!=1:
            ans+=d[j][1]
print(ans)

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


def warshall_floyd(d,n):
    
    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
    
warshall_floyd(field,10)

ans=0
for i in wall:
    for j in i:
        if j!=-1 and j!=1:
            ans+=field[j][1]
print(ans)

Solution (Python):




from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix, lil_matrix
import numpy as np
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


H, W = read_ints()
C = csr_matrix(read_matrix(10))
A = read_matrix(H)


D = dijkstra(C, directed=True)

ans = 0
for a in A:
    for aa in a:
        if abs(aa) == 1:
            continue
        ans += D[aa][1]

print(int(ans))

Solution (Python):
def main():
    cost = [[1e10 for j in range(10)]for i in range(10)]
    h,w = map(int,input().split())
    for i in range(10):
        tmp = list(map(int,input().split()))
        for j in range(10):
            if tmp[j] != -1:
                cost[i][j]=tmp[j]
    
    a = list()
    for i in range(h):
        tmp = list(map(int,input().split()))
        a.append(tmp)
    


    for k in range(10):
        for i in range(10):
            for j in range(10):
                cost[i][j] = min(cost[i][k]+cost[k][j],cost[i][j])
    
    ans = 0
    for i in range(h):
        for j in range(w):
            if a[i][j] != -1:
                ans += cost[a[i][j]][1]

    print(ans)
    return

if __name__ == "__main__":
    main()

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) 

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

H, W = LI()
c = []
for i in range(10):
    c.append(LI())

A = []
for i in range(H):
    A = A + LI()

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 a in A:
    if a != -1:
        ans += c[a][1]

print(ans)

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

def warshall_floyd():
  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])

warshall_floyd()

ans = 0
for i in range(h):
  for j in range(w):
    if abs(aa[i][j]) != 1:
      ans += d[aa[i][j]][1]      
    
print(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)]

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 = 0
for i in range(H):
	for j in range(W):
		num = A[i][j]
		if num == -1:
			continue
		else:
			cost += C[num][1]

print(cost)