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):
def floyd_warshall(dist):
    v = len(dist)
    for k in range(v):
        for i in range(v):
            for j in range(v):
                dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j])

h,w = map(int,input().split())
c = [list(map(int,input().split())) for _ in range(10)]
floyd_warshall(c)
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 += c[A[i][j]][1]
print(ans)

Solution (Python):
import numpy as np
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 = []
for i in range(10):
    c.append(list(map(int, input().split())))

csr = csr_matrix(c)

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

cost = floyd_warshall(csr)[:,1]

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

Solution (Python):
import heapq

def dijkstra(s):
    hq = [(0, s)]
    heapq.heapify(hq) 
    cost = [float('inf')] * 10 
    cost[s] = 0 
    while hq:
        c, v = heapq.heappop(hq)
        if c > cost[v]: 
            continue
        for d, u in e[v]:
            tmp = d + cost[v]
            if tmp < cost[u]:
                cost[u] = tmp
                heapq.heappush(hq, (tmp, u))
    return cost

h,w = map(int,input().split())
e = [[] for _ in range(10)]
for i in range(10):
    alist = list(map(int,input().split()))
    for j in range(10):
        if i != j:
            e[i].append((alist[j], j))


l = []
for i in range(h):
    for a in list(map(int,input().split())):
        if a != -1:
            l.append(a)

ans = 0
for i in l:
    ans += dijkstra(i)[1]
print(ans)

Solution (Python):
class Digraph:
    
    def __init__(self,vertex=[]):
        self.vertex=set(vertex)

        self.edge_number=0
        self.vertex_number=len(vertex)

        self.adjacent_out={v:{} for v in vertex} 
        self.adjacent_in={v:{} for v in vertex} 

    
    def add_vertex(self,*adder):
        for v in adder:
            if v not in self.vertex:
                self.adjacent_in[v]={}
                self.adjacent_out[v]={}

                self.vertex_number+=1
                self.vertex.add(v)

    
    def add_edge(self,From,To,weight=1):
        for v in [From,To]:
            if v not in self.vertex:
                self.add_vertex(v)

        if To not in self.adjacent_in[From]:
            self.edge_number+=1

        self.adjacent_out[From][To]=weight
        self.adjacent_in[To][From]=weight

    
    def remove_edge(self,From,To):
        for v in [From,To]:
            if v not in self.vertex:
                self.add_vertex(w)

        if To in self.adjacent_out[From]:
            del self.adjacent_out[From][To]
            del self.adjacent_in[To][From]
            self.edge_number-=1

    
    def remove_vertex(self,*vertexes):
        for  v in vertexes:
            if v in self.vertex:
                self.vertex_number-=1

                for u in self.adjacent_out[v]:
                    del self.adjacent_in[u][v]
                    self.edge_number-=1
                del self.adjacent_out[v]

                for u in self.adjacent_in[v]:
                    del self.adjacent_out[u][v]
                    self.edge_number-=1
                del self.adjacent_in[v]

    
    def add_walk(self,*walk):
        pass

    
    def add_cycle(self,*cycle):
        pass

    
    def __vertex_swap(self,p,q):
        self.vertex.sort()

    
    def vertex_exist(self,v):
        return v in self.vertex

    
    def edge_exist(self,From,To):
        if not(self.vertex_exist(From) and self.vertex_exist(To)):
            return False
        return To in self.adjacent_out[From]

    
    def neighbohood(self,v):
        if not self.vertex_exist(v):
            return []
        return list(self.adjacent[v])

    
    def out_degree(self,v):
        if not self.vertex_exist(v):
            return 0

        return len(self.adjacent_out[v])

    
    def in_degree(self,v):
        if not self.vertex_exist(v):
            return 0

        return len(self.adjacent_in[v])

    
    def degree(self,v):
        if not self.vertex_exist(v):
            return 0

        return self.out_degree(v)-self.in_degree(v)

    
    def vertex_count(self):
        return len(self.vertex)

    
    def edge_count(self):
        return self.edge_number

    
    def connected_component(self,v):
        pass

def Dijkstra_All(D,From,with_path=False):
    """Dijksta法を用いて,単一始点Fromからの距離を求める.

    D:辺の重みが全て非負の有向グラフ
    From:始点
    To:終点
    with_path:最短路も含めて出力するか?

    (出力の結果)
    with_path=True->(距離,最短経路の辿る際の前の頂点)
    with_path=False->距離
    """
    from copy import copy
    from heapq import heappush,heappop

    T={v:float("inf") for v in D.vertex}
    T[From]=0

    if with_path:
        Prev={v:None for v in D.vertex}

    Q=[(0,From)]

    while Q:
        c,u=heappop(Q)

        if T[u]<c:
            continue

        for v in D.adjacent_out[u]:
            if T[v]>T[u]+D.adjacent_out[u][v]:
                T[v]=T[u]+D.adjacent_out[u][v]
                heappush(Q,(T[v],v))

                if with_path:
                    Prev[v]=u

    if with_path:
        return (T,Prev)
    else:
        return  T


def Inverse_Graph(D):
    """有向グラフDの全ての辺の向きを変えたグラフを出力する.

    D:有向グラフ
    """
    from copy import deepcopy

    F=Digraph(D.vertex)

    F.edge_number=D.edge_number
    F.vertex_number=D.vertex_number

    F.adjacent_in=deepcopy(D.adjacent_out)
    F.adjacent_out=deepcopy(D.adjacent_in)

    return F


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

D=Digraph(range(10))
for i in range(10):
    C=list(map(int,input().split()))
    for j in range(10):
        D.add_edge(i,j,C[j])

D=Inverse_Graph(D)
T=Dijkstra_All(D,1)

Count={x:0 for x in range(-1,10)}

for _ in range(H):
    A=list(map(int,input().split()))
    for a in A:
        Count[a]+=1

X=0
for a in range(10):
    X+=Count[a]*T[a]

print(X)

Solution (Python):
class Digraph:
    
    def __init__(self,vertex=[]):
        self.vertex=set(vertex)

        self.edge_number=0
        self.vertex_number=len(vertex)

        self.adjacent_out={v:{} for v in vertex} 
        self.adjacent_in={v:{} for v in vertex} 

    
    def add_vertex(self,*adder):
        for v in adder:
            if v not in self.vertex:
                self.adjacent_in[v]={}
                self.adjacent_out[v]={}

                self.vertex_number+=1
                self.vertex.add(v)

    
    def add_edge(self,From,To,weight=1):
        for v in [From,To]:
            if v not in self.vertex:
                self.add_vertex(v)

        if To not in self.adjacent_in[From]:
            self.edge_number+=1

        self.adjacent_out[From][To]=weight
        self.adjacent_in[To][From]=weight

    
    def remove_edge(self,From,To):
        for v in [From,To]:
            if v not in self.vertex:
                self.add_vertex(w)

        if To in self.adjacent_out[From]:
            del self.adjacent_out[From][To]
            del self.adjacent_in[To][From]
            self.edge_number-=1

    
    def remove_vertex(self,*vertexes):
        for  v in vertexes:
            if v in self.vertex:
                self.vertex_number-=1

                for u in self.adjacent_out[v]:
                    del self.adjacent_in[u][v]
                    self.edge_number-=1
                del self.adjacent_out[v]

                for u in self.adjacent_in[v]:
                    del self.adjacent_out[u][v]
                    self.edge_number-=1
                del self.adjacent_in[v]

    
    def add_walk(self,*walk):
        pass

    
    def add_cycle(self,*cycle):
        pass

    
    def __vertex_swap(self,p,q):
        self.vertex.sort()

    
    def vertex_exist(self,v):
        return v in self.vertex

    
    def edge_exist(self,From,To):
        if not(self.vertex_exist(From) and self.vertex_exist(To)):
            return False
        return To in self.adjacent_out[From]

    
    def neighbohood(self,v):
        if not self.vertex_exist(v):
            return []
        return list(self.adjacent[v])

    
    def out_degree(self,v):
        if not self.vertex_exist(v):
            return 0

        return len(self.adjacent_out[v])

    
    def in_degree(self,v):
        if not self.vertex_exist(v):
            return 0

        return len(self.adjacent_in[v])

    
    def degree(self,v):
        if not self.vertex_exist(v):
            return 0

        return self.out_degree(v)-self.in_degree(v)

    
    def vertex_count(self):
        return len(self.vertex)

    
    def edge_count(self):
        return self.edge_number

    
    def connected_component(self,v):
        pass

def Dijkstra_All(D,From,with_path=False):
    """Dijksta法を用いて,単一始点Fromからの距離を求める.

    D:辺の重みが全て非負の有向グラフ
    From:始点
    To:終点
    with_path:最短路も含めて出力するか?

    (出力の結果)
    with_path=True->(距離,最短経路の辿る際の前の頂点)
    with_path=False->距離
    """
    from copy import copy

    T={v:float("inf") for v in D.vertex}
    T[From]=0

    if with_path:
        Prev={v:None for v in D.vertex}

    Q=copy(D.vertex)

    Flag=False
    while Q:
        u=min(Q,key=lambda u:T[u])
        Q.remove(u)

        for v in D.adjacent_out[u]:
            if T[v]>T[u]+D.adjacent_out[u][v]:
                T[v]=T[u]+D.adjacent_out[u][v]

                if with_path:
                    Prev[v]=u

    if with_path:
        return (T,Prev)
    else:
        return  T


def Inverse_Graph(D):
    """有向グラフDの全ての辺の向きを変えたグラフを出力する.

    D:有向グラフ
    """
    from copy import deepcopy

    F=Digraph(D.vertex)

    F.edge_number=D.edge_number
    F.vertex_number=D.vertex_number

    F.adjacent_in=deepcopy(D.adjacent_out)
    F.adjacent_out=deepcopy(D.adjacent_in)

    return F


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

D=Digraph(range(10))
for i in range(10):
    C=list(map(int,input().split()))
    for j in range(10):
        D.add_edge(i,j,C[j])

D=Inverse_Graph(D)
T=Dijkstra_All(D,1)

Count={x:0 for x in range(-1,10)}

for _ in range(H):
    A=list(map(int,input().split()))
    for a in A:
        Count[a]+=1

X=0
for a in range(10):
    X+=Count[a]*T[a]

print(X)

Solution (Python):
h, w = map(int, input().split())
a = []
c = []
for i in range(10):
    c.append(list(map(int, input().split())))
for i in range(h):
    a.append(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]
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j] >= 0:
            ans += c[a[i][j]][1]

print(ans)

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 k in range(10):
  for i in range(10):
    for j in range(10):
      if C[i][k]+C[k][j] < C[i][j]:
        C[i][j] = C[i][k] + C[k][j]
        
ans = 0
for i in range(H):
  l = A[i]
  for j in range(W):
    n = l[j]
    if (n==0) or (n>1):
      ans += C[n][1]
print(ans)

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

r = range(10)
c = [[int(i) for i in input().split()] for _ in r]

for k in r:
    for i in r:
        for j in r:
            c[i][j] = min(c[i][j], c[i][k] + c[k][j])
else:
    a = [[int(i) for i in input().split()] for _ in range(h)]
    print(sum(c[i][1] for i in sum(a, []) if i != -1))

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

r = range(10)
c = [[int(i) for i in input().split()] for _ in r]
a = [[int(i) for i in input().split()] for _ in range(h)]

for k in r:
    for i in r:
        for j in r:
            c[i][j] = min(c[i][j], c[i][k] + c[k][j])
else:
    print(sum(c[i][1] for i in sum(a, []) if i != -1))

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)]
INF=10**18
for k in range(10):
  for i in range(10):
    if c[i][k] == INF:
      continue
    for j in range(10):
      if c[k][j] == INF:
        continue
      c[i][j] = min(c[i][j], c[i][k] + c[k][j])
ans=0
for i in a:
  for k in i:
    if k!=-1:
      ans+=c[k][1]
print(ans)

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

Solution (Python):

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

Solution (Python):


class Warshall_Floyd:
    def __init__(self, V):
        self.V = V
        self.G = [[float('inf')] * self.V for i in range(self.V)]

    def add_edge(self, fr, to, cost):
        self.G[fr][to] = cost

    def add_multi_edge(self, v1, v2, cost):
        self.G[v1][v2] = cost
        self.G[v2][v1] = cost

    def solve(self):
        for i in range(self.V):
            self.G[i][i] = 0
        for k in range(self.V):
            for i in range(self.V):
                for j in range(self.V):
                    if self.G[i][k] != float("inf") and self.G[k][j] != float('inf'):
                        self.G[i][j] = min(self.G[i][j], self.G[i][k] + self.G[k][j])

    def if_negative(self):
        for i in range(self.V):
            if self.G[i][i] < 0:
                return True
        return False

    def dist(self, fr, to):
        return self.G[fr][to]


G = Warshall_Floyd(10)

H, W = map(int, input().split())
for i in range(10):
    for j, c in enumerate(input().split()):
        G.add_edge(i, j, int(c))
G.solve()


ans = 0
for i in range(H):
    for j, a in enumerate(input().split()):
        if a != "-1":
            ans += G.dist(int(a), 1)

print(ans)

Solution (Python):



h, w = map(int, input().split())
dp = [[int(x) for x in input().split()] for y in range(10)]
a_l = [[int(x) for x in input().split()] for y in range(h)]

for k in range(10):
    for j in range(10):
        for i in range(10):
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
            
ans = 0
for i in range(h):
    for _a in a_l[i]:
        if _a == -1:
            continue
        ans += dp[_a][1]

print(ans)

Solution (Python):
INF = 10 ** 20
H, W = map(int, input().split())
power = 0
graph = []
for i in range(10):
  graph.append(list(map(int, input().split())))

for k in range(10):
  for i in range(10):
    for j in range(10):
      graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])
  
for h in range(H):
  A = list(map(int, input().split()))
  for w in range(W):
    if A[w] == -1:
      continue
    else:
      power += graph[A[w]][1]

print(power)

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

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

d = warshall_floyd(c)
ans = 0
for i in range(H):
    for j in range(W):
        if A[i][j] != -1 and A[i][j] != 1:
            ans += d[A[i][j]][1]
print(ans)

Solution (Python):

import sys

def solve(H: int, W: int, c: "List[List[int]]", A: "List[List[int]]"):
    import numpy as np
    from scipy.sparse.csgraph import floyd_warshall
    from itertools import chain
    mat = floyd_warshall(np.array(c, dtype=np.int64), directed=True)    
    return int(sum(mat[a][1] for a in filter(lambda x: x >= 0, chain.from_iterable(A))))


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)]  
    print(solve(H, W, c, A))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    
    main()

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)]
dp = [float("INF")]*(10)
dp[1] = 0
cnt = 1
while cnt < 10:
    cnt += 1
    for i in range(10):
        for j in range(10):
            dp[i] = min(dp[i],dp[j]+C[i][j])
ans = 0
for i in range(H):
    for j in range(W):
        a = A[i][j]
        if a != -1:
            ans += dp[a]
print(ans)

Solution (Python):
import collections
import itertools

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

a_1D =list(itertools.chain.from_iterable(a))

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

a_cnt = collections.Counter(a_1D)

total = 0
for i,j in a_cnt.items():
    if i == -1:
        pass
    else:
        total += c[i][1]*j

print(total)

Solution (Python):
from collections import defaultdict
dicA = defaultdict(int) 

H,W = map(int,input().split())
C = []
min_step = []
for i in range(10):
  temp = list(map(int,input().split()))
  C.append(temp)
  min_step.append(temp[1])



def dfs(n,k,now): 
  if now > min_step[n]:
    return
  for i in range(10):
    if i == 1: 
      
      min_step[n] = min(min_step[n],now+ C[k][1])
    if i != k:
      if C[k][i]+now < min_step[n]: 
        
        dfs(n,i,C[k][i]+now)

for i in range(10):
  if i == 1:
    continue
  for j in range(10):
    if i!=j:
      dfs(i,j,C[i][j])



for i in range(H):
  temp = list(map(int,input().split()))
  for j in range(W):
    if temp[j] == -1:
      continue
    dicA[temp[j]] +=1 
ans = 0

for x in dicA.keys():
  ans += min_step[x]*dicA[x]
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)]

def warshall_floyd(adj_m):
    
    _dist=adj_m
    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])
    return _dist

dist=warshall_floyd(c)

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

Solution (Python):
from itertools import permutations
from heapq import heappush,heappop

INF=10**12

def dijkstra(G, start = 0, INF=INF):
    
    d = [INF for i in range(len(G))]
    d[start] = 0
    
    que = []
    heappush(que, (0, start))

    while len(que) != 0:
        p = heappop(que)
        v = p[1]
        
        if d[v] < p[0]:
            continue
            
        for u in G[v].keys():
            if d[u] > d[v] + G[v][u]:
                d[u] = d[v] + G[v][u]
                heappush(que, (d[u], u))
    
    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)]

G=[{} for _ in range(10)]
for i in range(10):
  for j in range(10):
    G[i][j] = c[i][j]

dist=[0]*10
for i in range(10):
  d = dijkstra(G,i)
  dist[i] = d[1]

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

print(ans)

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

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


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

ans = 0
for i in range(H):
    for item in wall[i]:
        if item<0:
            continue
        ans += arr[item][1]  
print(ans)

Solution (Python):
h,w=map(int,input().split())
INF=float('inf')

a=[]
for _ in range(10):
  c=list(map(int,input().split()))
  a.append(c)
  
for i in range(10):
  for j in range(10):
    for k in range(10):
      a[j][k]=min(a[j][k],a[j][i]+a[i][k])


ans=0
for x in range(h):
  d=list(map(int,input().split()))
  for e in d:
    if e!=-1:
      ans+=a[e][1]
      
print(ans)

Solution (Python):
import copy
L=[[j for j in range(10)] for i in range(10)]
H,W=map(int,input().split())
costs=list()
costs2=list()
for i in range(10):
  L1=list(map(int,input().split()))
  costs.append(L1[1])
  costs2.append(L1[1])
  L[i]=L1
for i in range(12):
  for j in range(10):
    for k in range(10):
      costs2[j]=min(costs2[j],costs[k]+L[j][k])
  costs=copy.copy(costs2)
costs.append(0)
s=0
for i in range(H):
  A=list(map(int,input().split()))
  s+=sum([costs[i] for i in A])
print(s)

Solution (Python):
import sys
input = sys.stdin.readline


def read():
    H, W = map(int, input().strip().split())
    N = 10
    C = []
    for i in range(N):
        c = list(map(int, input().strip().split()))
        C.append(c)
    A = []
    for i in range(H):
        a = list(map(int, input().strip().split()))
        A.append(a)
    return H, W, N, C, A


def warshall_floyd(N, G, INF=10**7):
    for k in range(N):
        for i in range(N):
            for j in range(N):
                G[i][j] = min(G[i][j], G[i][k] + G[k][j])
    return G


def solve(H, W, N, C, A):
    D = warshall_floyd(N, C)
    ans = 0
    for i in range(H):
        for j in range(W):
            a = A[i][j]
            if a >= 0:
                ans += D[a][1]
    return ans


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Solution (Python):



h,w = map(int, raw_input().split())
cost = []
for _ in range(10): 
	cost.append(map(int,raw_input().split()))


mat = [map(int, raw_input().split()) for ii in range(h)]

h = {(u,v):cost[u][v] for u in range(10) for v in range(10)}
for t in range(10):
	for u in range(10):
		for v in range(10):
			for w in range(10):
				h[(u,v)] = min(h[(u,v)], h[(u,w)] + h[(w,v)])
cumul = 0
for e in mat:
	for el in e:
		if el != -1:
			cumul += h[(el,1)]
print cumul

Solution (Python):
h, w = map(int, input().split())
c = []
for i in range(10):
    c.append(list(map(int, input().split())))
a = []
for i in range(h):
    a.append(list(map(int, input().split())))
cost = [float("inf") for i in range(10)]
flag = [False for i in range(10)]
cost[1] = 0
test = [1]
while test:
    num = test.pop(0)
    flag[num] = True
    for i in range(10):
        cost[i] = min(cost[i], cost[num] + c[i][num])
    temp = [[cost[i], i] for i in range(10)]
    temp.sort()
    for i in range(10):
        if flag[temp[i][1]] == True:
            continue
        else:
            test.append(temp[i][1])
            break
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j] == -1:
            continue
        else:
            ans += cost[a[i][j]]
print(ans)

Solution (Python):

INF = 10**9

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


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

dp = [[(INF if i!=j else 0) for j in range(10)]for i in range(10)]
for k in range(0,10,1):
    for i in range(0,10,1):
        for j in range(0,10,1):
            c[i][j] = min(c[i][j],c[i][k]+c[k][j])


A = [list(map(int,input().split())) for i in range(H)]
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 sys
readline = sys.stdin.readline

H,W = map(int,readline().split())
C = [None] * 10
for i in range(10):
  C[i] = list(map(int,readline().split()))

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

from collections import defaultdict
wall = defaultdict(int)
for i in range(H):
  for w in list(map(int,readline().split())):
    wall[w] += 1
  
ans = 0
for key,value in wall.items():
  if key == -1:
    continue
  ans += value * C[key][1]
  
print(ans)

Solution (Python):
from collections import defaultdict

h,w=map(int,input().split())
c=[]
dic=defaultdict(int)
for _ in range(10): c.append(list(map(int,input().split())))
for _ in range(h):
    temp=list(map(int,input().split()))
    for i in temp: dic[i]+=1
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 item in dic.items():
    if item[0]>=0: ans+=item[1]*c[item[0]][1]
print(ans)

Solution (Python):



def warshall_floyd(dist):
    v_count = len(dist[0])

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

    return dist


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)]
    ans = 0

    results = warshall_floyd(c)

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):



def warshall_floyd(dist):
    v_count = len(dist[0])

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

    return dist


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)]
    ans = 0

    results = warshall_floyd(c)

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from heapq import heappush, heappop
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)]
    h = [(0,1)]
    V = [-1]*10
    while h:
        
        m, n = heappop(h)
        if V[n] > -1:
            continue
        V[n] = m
        for i in range(10):
            if V[i] == -1:
                heappush(h, (m+C[i][n],i))
    
    ans = 0
    for a in A:
        for b in a:
            if b > -1:
                ans += V[b]
    print(ans)
    
if __name__ == '__main__':
    main()