We need to interpret the problem. We have a grid HxW, each cell either contains a digit (0-9) or is empty (-1). At least one digit exists. We can "rewrite" a digit i with j at cost c_{i,j} MP. The goal: turn every digit on the wall into 1. But note: we can only rewrite digits that exist? And we can't change empty cells because they don't have digits. The operation presumably applies to any digit present. So we need to decide a final digit for each cell that originally had a digit, such that final digit is 1, but we can change it via a series of rewrites? Actually, rewriting a digit i with j costs c_{i,j} and changes the digit from i to j. Since we only care about the final state, we can think of directly replacing the original digit d with 1, but we might do it indirectly: maybe it's cheaper to change from d to some intermediate k then to 1, but the cost of a sequence of rewrites is sum of costs for each step. Since we have the cost matrix c_{i,j} but it's not necessarily symmetric, and we may have triangle inequality not holding, so we need to consider the shortest path cost from i to j via intermediate digits. However, rewriting directly from i to j costs c_{i,j}, but we can also rewrite stepwise: e.g., i -> k -> j. But if we are allowed to perform multiple rewrites, the minimal cost to turn i into j is the shortest path distance from i to j in the complete directed graph with edge weights c_{i,j}. Since we can rewrite any digit any number of times, as long as we end at 1. So the minimal cost for a digit originally i to become 1 is the minimal total cost of a path from i to 1 in this graph. However, note that the problem says: "Rewriting a digit i with j (0≤i,j≤9) costs c_{i,j} MP." It does not say we can only rewrite once. So indeed we can perform multiple rewrites, paying each time. So the minimal cost to change i to 1 is the shortest path distance from i to 1. But is there any constraint like we cannot rewrite a digit that is not present? No.
Thus essentially, for each cell with a digit d, we need to pay the minimal cost to transform d to 1 using the given rewriting costs (where direct rewriting costs may not satisfy triangle inequality). So we need to compute all-pairs shortest path among the 10 digits (0..9) using the given cost matrix. Then for each digit d, answer is dist[d][1]. Sum over all cells with digit.
But is that all? The example: Input 1, output 12. Let's check.
Input 1:
H=2, W=4
Cost matrix:
Row0: 0 9 9 9 9 9 9 9 9 9
Row1: 9 0 9 9 9 9 9 9 9 9
Row2: 9 9 0 9 9 9 9 9 9 9
Row3: 9 9 9 0 9 9 9 9 9 9
Row4: 9 9 9 9 0 9 9 9 9 2
Row5: 9 9 9 9 9 0 9 9 9 9
Row6: 9 9 9 9 9 9 0 9 9 9
Row7: 9 9 9 9 9 9 9 0 9 9
Row8: 9 9 9 9 2 9 9 9 0 9
Row9: 9 2 9 9 9 9 9 9 9 0
Grid:
Row1: -1 -1 -1 -1
Row2: 8 1 1 8
So digits: two 8's. Need to turn them into 1. Compute shortest path from 8 to 1.
Edge weights: c_{8,?} from row8: 0 (self), 9,9,9,2,9,9,9,0? Wait row8: index 8. It says: "9 9 9 9 2 9 9 9 0 9". So c_{8,0}=9, c_{8,1}=9, c_{8,2}=9, c_{8,3}=9, c_{8,4}=2, c_{8,5}=9, c_{8,6}=9, c_{8,7}=9, c_{8,8}=0, c_{8,9}=9.
c_{9,?} from row9: "9 2 9 9 9 9 9 9 9 0". So c_{9,1}=2.
c_{4,9}=2 from row4: "9 9 9 9 0 9 9 9 9 2". So c_{4,9}=2.
c_{?} others are 9 except diagonals 0.
We need shortest from 8 to 1. Direct: c_{8,1}=9. Alternative paths: 8->4->9->1: cost c_{8,4}=2 + c_{4,9}=2 + c_{9,1}=2 = 6. That's less than 9. Also 8->4->? could go 8->4->9->? maybe other. So dist[8][1]=6. But there are two 8's, total cost = 12. Output 12 matches. So indeed we sum the per-digit minimal transformation cost to 1.
But wait: Could there be a scenario where we could rewrite a digit to something else and then later maybe rewrite another digit? But each digit's transformation is independent, because costs are per rewrite and they don't interact. So total minimal MP is sum over digits of minimal cost to become 1.
But what about empty cells? They don't have digits, so ignore.
Thus the problem reduces to:
- Given a 10x10 cost matrix c (with c[i][i]=0, c[i][j] >=1 for i≠j).
- Compute shortest path distances between all pairs (0..9) using Floyd-Warshall or Dijkstra on complete graph of 10 nodes.
- Then read grid A (HxW). For each cell where A_ij != -1, add dist[A_ij][1] to answer.
- Print sum.
Check Example 2: all digits are 1, so cost to turn 1 to 1 is 0, answer 0. Example 3: compute distances and sum.
Thus solution straightforward.
But is there any trick? The problem statement: "Joisino the magical girl has decided to turn every single digit that exists on this world into 1." That might hint that we can also rewrite digits that are not on the wall? But the wall only: she is now standing before a wall. The wall has digits, she wants to turn every digit on this wall into 1. So it's just the digits on the wall.
One nuance: Could there be an advantage to rewrite a digit to something else, then later perhaps that digit might affect other digits? No, they are independent.
Thus solution is easy.
But wait: The problem says "turn every single digit that exists on this world into 1." That seems like a global goal but then says "She is now standing before a wall." Possibly implies that there might be digits elsewhere but we only care about the wall? Actually the problem statement: "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)." So the digits we need to turn are those on the wall. So we just need to change those digits to 1.
Thus solution is sum of minimal costs from each digit to 1.
But wait: Could there be a scenario where we might want to change a digit to something else, then later maybe change another digit, and the intermediate changes might be shared? No, each digit is independent. So sum.
Thus the problem is trivial after computing shortest paths. However, the constraints H,W up to 200, grid up to 40000 cells. But with only 10 digits, shortest path is trivial.
Thus we can implement.
But I recall some AtCoder problem "Joisino's Travel" or something? Actually "Joisino's Magical Girl" maybe? The problem seems from AtCoder ABC 073 D? Not exactly. Let's check: This looks like AtCoder ABC 079 D? Actually there is a problem "Wall" from ABC something? I'm not sure. But it's definitely simple: compute all-pairs shortest paths among digits.
Edge weights given for i and j. Since graph is small, we can run Floyd-Warshall for k from 0 to 9, i, j.
Then sum.
But one more nuance: The cost matrix may not be symmetric? They gave c_{i,j} in input presumably row i, column j. In the example, they are not symmetric: e.g., c_{4,9}=2, but c_{9,4}? row9: "9 2 9 9 9 9 9 9 9 0", so c_{9,4}=9, not symmetric. So directed graph. But we need to turn digit to 1, we can only change the digit by rewriting i to j, paying c_{i,j}. So direction matters: we need to go from original digit to 1, so we consider directed edges: i -> j cost c_{i,j}. We want the cheapest directed path from start to target 1. So Floyd-Warshall on directed graph.
But is it possible that rewriting i to j might also be used as part of a path from i to 1, including intermediate nodes. So yes.
Thus compute shortest path distances d[i][j] = minimal cost to transform digit i into j (via any sequence). Since graph is small, we can just run Floyd-Warshall.
But wait: the problem states: "Rewriting a digit i with j (0≤i,j≤9) costs c_{i,j} MP (Magic Points)." It does not explicitly say we can rewrite multiple times, but it's natural that we can perform the operation multiple times. AtCoder problem "Wall" is similar: you can change digits on a wall, you can change any digit to any other, but you have to pay cost per change, and you want to make all digits 1. So they compute minimal cost to turn each digit to 1 using Floyd-Warshall.
So solution is clear.
Now we just need to code.
But there is also a twist: The grid may have -1 for no digit. So we ignore those.
Complexities: O(10^3) for Floyd-Warshall + O(H*W) for summing, fine.
Now check example 3 manually to ensure answer 47 matches.
Example 3: 3x5 grid, cost matrix given, digits:
Row1: 3 5 2 6 1
Row2: 2 5 3 2 1
Row3: 6 9 2 5 6
We need dist[d][1] for d=0..9.
First compute shortest paths.
Cost matrix (10x10) from input:
Row0: 0 4 3 6 2 7 2 5 3 3
Row1: 4 0 5 3 7 5 3 7 2 7
Row2: 5 7 0 7 2 9 3 2 9 1
Row3: 3 6 2 0 2 4 6 4 2 3
Row4: 3 5 7 4 0 6 9 7 6 7
Row5: 9 8 5 2 2 0 4 7 6 5
Row6: 5 4 6 3 2 3 0 5 4 3
Row7: 3 6 2 3 4 2 4 0 8 9
Row8: 4 6 5 4 3 5 3 2 0 8
Row9: 2 1 3 4 5 7 8 6 4 0
We need distances to 1. Let's compute via Floyd.
But we can check sum: digits in grid:
Row1: 3,5,2,6,1 → costs: dist[3][1], dist[5][1], dist[2][1], dist[6][1], 0
Row2: 2,5,3,2,1 → costs: dist[2][1], dist[5][1], dist[3][1], dist[2][1], 0
Row3: 6,9,2,5,6 → costs: dist[6][1], dist[9][1], dist[2][1], dist[5][1], dist[6][1]
Total = sum of costs for: 3,5,2,6,2,5,3,2,6,9,2,5,6 (and 1's are zero). Count: digits: list: 3,5,2,6,2,5,3,2,6,9,2,5,6. That's 13 digits. And answer 47. So average about 3.6.
Let's compute quickly distances to 1 manually using Floyd. We'll compute all distances, but maybe just compute shortest from each to 1. Since graph is directed.
Set d[i][j] = c[i][j] initially.
We'll run Floyd-Warshall.
Matrix c (i row, j col):
i=0: [0,4,3,6,2,7,2,5,3,3]
i=1: [4,0,5,3,7,5,3,7,2,7]
i=2: [5,7,0,7,2,9,3,2,9,1]
i=3: [3,6,2,0,2,4,6,4,2,3]
i=4: [3,5,7,4,0,6,9,7,6,7]
i=5: [9,8,5,2,2,0,4,7,6,5]
i=6: [5,4,6,3,2,3,0,5,4,3]
i=7: [3,6,2,3,4,2,4,0,8,9]
i=8: [4,6,5,4,3,5,3,2,0,8]
i=9: [2,1,3,4,5,7,8,6,4,0]
Goal: find dist[i][1] for i=0..9.
Let's compute Floyd quickly.
We'll do triple loops: for k in 0..9, for i in 0..9, for j in 0..9, d[i][j] = min(d[i][j], d[i][k]+d[k][j]).
I can do manual passes.
But maybe we can compute dist to 1 via Dijkstra from each node? Since only 10 nodes, we can do Dijkstra from each, but easier to do Floyd.
We'll compute all-pairs.
Initialize d as above.
We'll iteratively update.
I'll try to compute shortest distances focusing on node 1 as target. But Floyd must compute all.
Given small size, we can compute by reasoning: maybe many paths.
Let's list direct costs to 1: d[i][1] initial:
0->1:4
1->1:0
2->1:7
3->1:6
4->1:5
5->1:8
6->1:4
7->1:6
8->1:6
9->1:1 (since c[9][1]=1)
So initial dist to 1: [4,0,7,6,5,8,4,6,6,1].
Now we can consider indirect paths: i -> k -> 1, where k is any node. So we need to see if any path reduces these.
Also consider paths with multiple steps.
We'll do Floyd quickly.
Let's update for k=0: through node 0, we can potentially improve d[i][j] = min(d[i][j], d[i][0]+d[0][j]).
We need d[0][j] row0. d[0][*] = [0,4,3,6,2,7,2,5,3,3].
For each i, we compute new distances to all j. But we can just track for each i->1.
First, before k=0, d[i][1] as above.
After considering k=0, for each i, d[i][1] = min(previous, d[i][0] + d[0][1] = d[i][0] + 4). d[i][0] initial:
0->0:0
1->0:4
2->0:5
3->0:3
4->0:3
5->0:9
6->0:5
7->0:3
8->0:4
9->0:2
So compute potential new:
i=0: min(4, 0+4=4) =4
i=1: min(0, 4+4=8) =0
i=2: min(7, 5+4=9) =7
i=3: min(6, 3+4=7) =6
i=4: min(5, 3+4=7) =5
i=5: min(8, 9+4=13) =8
i=6: min(4, 5+4=9) =4
i=7: min(6, 3+4=7) =6
i=8: min(6, 4+4=8) =6
i=9: min(1, 2+4=6) =1
So no change.
Now k=1: We'll incorporate paths through node 1. We need d[i][1] and d[1][j]. d[1][*] row: [4,0,5,3,7,5,3,7,2,7].
So for i->j, potential new = d[i][1] + d[1][j]. But we need to update all i,j. For i->1, potential via k=1: d[i][1] + d[1][1] = d[i][1]+0, no change. So for target 1, k=1 doesn't change. But other distances may change, which might later affect via other k's.
k=2: row2: d[2][*] = [5,7,0,7,2,9,3,2,9,1]. Through node 2: d[i][2] + d[2][j].
We need to consider d[i][1] new = min(prev, d[i][2] + d[2][1] (which is 7)). But note d[2][1]=7. So if we can go i->2 cost +7.
We need d[i][2] current. Let's keep current d matrix after k=0,1. But since k=0 and k=1 didn't change much, we still have initial for many.
Current d[i][2] (initial, maybe updated by k=1? Let's compute systematically: after k=0,1, we may have updated some entries. But we can consider Floyd fully; to be safe we can just simulate the whole Floyd algorithm step by step with code in mind. But doing manually, maybe easier to compute the final distances to 1 by Dijkstra from each node on the directed graph with given edges. Since graph is small, we can compute manually.
But we should do systematic Floyd because there might be interactions.
Given it's only 10 nodes, we can compute manually if careful. Let's set up a table and iterate k=0..9.
I'll start with initial matrix D0.
D0:
i\j: 0 1 2 3 4 5 6 7 8 9
0: 0 4 3 6 2 7 2 5 3 3
1: 4 0 5 3 7 5 3 7 2 7
2: 5 7 0 7 2 9 3 2 9 1
3: 3 6 2 0 2 4 6 4 2 3
4: 3 5 7 4 0 6 9 7 6 7
5: 9 8 5 2 2 0 4 7 6 5
6: 5 4 6 3 2 3 0 5 4 3
7: 3 6 2 3 4 2 4 0 8 9
8: 4 6 5 4 3 5 3 2 0 8
9: 2 1 3 4 5 7 8 6 4 0
Now iterate k=0: Update D1[i][j] = min(D0[i][j], D0[i][0] + D0[0][j]).
We compute for each i,j.
We already computed for j=1 earlier. But let's compute full for i and j.
We'll keep track of changes. D0[i][0] as above. D0[0][j] row0.
Compute:
For each i (0..9):
i=0: D0[0][j] already min. D0[0][0]+D0[0][j]=0+... equals D0[0][j]; no improvement.
i=1: D0[1][j] initially row1. D0[1][0]=4. New candidate = 4 + D0[0][j].
Compute for each j:
j=0: min(4,4+0=4) no change
j=1: min(0,4+4=8) stays 0
j=2: min(5,4+3=7) stays 5
j=3: min(3,4+6=10) stays 3
j=4: min(7,4+2=6) -> 6! (since 6<7). So D1[1][4]=6.
j=5: min(5,4+7=11) stays 5
j=6: min(3,4+2=6) stays 3
j=7: min(7,4+5=9) stays 7
j=8: min(2,4+3=7) stays 2
j=9: min(7,4+3=7) stays 7 (equal)
So updated D1[1][4]=6.
i=2: D0[2][0]=5. Candidate =5+D0[0][j].
j=0: min(5,5+0=5) no
j=1: min(7,5+4=9) no
j=2: min(0,5+3=8) no
j=3: min(7,5+6=11) no
j=4: min(2,5+2=7) no (2<7)
j=5: min(9,5+7=12) no
j=6: min(3,5+2=7) no
j=7: min(2,5+5=10) no
j=8: min(9,5+3=8) -> 8? Actually min(9,8)=8, so D1[2][8]=8 (improved from 9 to 8)
j=9: min(1,5+3=8) no
i=3: D0[3][0]=3. Candidate =3+D0[0][j].
j=0: min(3,3+0=3) no
j=1: min(6,3+4=7) no
j=2: min(2,3+3=6) no
j=3: min(0,3+6=9) no
j=4: min(2,3+2=5) no
j=5: min(4,3+7=10) no
j=6: min(6,3+2=5) -> 5! So D1[3][6]=5 (was 6)
j=7: min(4,3+5=8) no
j=8: min(2,3+3=6) no
j=9: min(3,3+3=6) no
i=4: D0[4][0]=3. Candidate =3+D0[0][j].
j=0: min(3,3+0=3) no
j=1: min(5,3+4=7) no
j=2: min(7,3+3=6) -> 6! So D1[4][2]=6 (was 7)
j=3: min(4,3+6=9) no
j=4: min(0,3+2=5) no
j=5: min(6,3+7=10) no
j=6: min(9,3+2=5) -> 5! So D1[4][6]=5 (was 9)
j=7: min(7,3+5=8) no
j=8: min(6,3+3=6) no
j=9: min(7,3+3=6) -> 6! So D1[4][9]=6 (was 7)
i=5: D0[5][0]=9. Candidate =9+D0[0][j].
Check each: j=0:9 vs 9; j=1:8 vs 13; j=2:5 vs 12; j=3:2 vs 15; j=4:2 vs 11; j=5:0 vs 16; j=6:4 vs 11; j=7:7 vs 14; j=8:6 vs 12; j=9:5 vs 12. No improvements (since candidate > current).
i=6: D0[6][0]=5. Candidate =5+D0[0][j].
j=0:5 vs 5; j=1:4 vs 9; j=2:6 vs 8; j=3:3 vs 11; j=4:2 vs 7; j=5:3 vs 12; j=6:0 vs 7; j=7:5 vs 10; j=8:4 vs 8; j=9:3 vs 8. No improvement? Check j=3: current 3, candidate 11; j=4:2<7; j=5:3<12; etc. So no.
i=7: D0[7][0]=3. Candidate =3+D0[0][j].
j=0:3 vs 3; j=1:6 vs 7; j=2:2 vs 6; j=3:3 vs 9; j=4:4 vs 5; j=5:2 vs 10; j=6:4 vs 5; j=7:0 vs 8; j=8:8 vs 6? Wait candidate for j=8: 3+3=6, current 8, so improvement! D1[7][8]=6 (was 8)
j=9:9 vs 6? 3+3=6 <9, so D1[7][9]=6 (was 9)
Also j=4: min(4,5)=4 unchanged; j=6: min(4,5)=4 unchanged; etc.
i=8: D0[8][0]=4. Candidate =4+D0[0][j].
j=0:4 vs 4; j=1:6 vs 8; j=2:5 vs 7; j=3:4 vs 10; j=4:3 vs 6; j=5:5 vs 11; j=6:3 vs 6; j=7:2 vs 9; j=8:0 vs 7; j=9:8 vs 7? 4+3=7 <8, so D1[8][9]=7 (was 8)
Also j=? none else.
i=9: D0[9][0]=2. Candidate =2+D0[0][j].
j=0:2 vs 2; j=1:1 vs 6; j=2:3 vs 5; j=3:4 vs 8; j=4:5 vs 4? 2+2=4 <5, so D1[9][4]=4 (was 5)
j=5:7 vs 9; j=6:8 vs 4? 2+2=4 <8, so D1[9][6]=4 (was 8)
j=7:6 vs 7? 2+5=7 >6 no; j=8:4 vs 5? 2+3=5 >4 no; j=9:0 vs 5; no.
So after k=0, we have many updates. Let's record D1 matrix (only changes noted). We'll keep updated rows. We'll produce full D1 after k=0.
But we don't need full; we need final after all k. However, we need to continue with k=1,2,... So we need to maintain the matrix after each iteration.
I'll try to create a table in text.
Initialize D = initial D0.
Then for k=0, we update D in place (Floyd usually does: for k for i for j, D[i][j] = min(D[i][j], D[i][k] + D[k][j]). So we need to use updated D[i][k] and D[k][j] as we go? Typically Floyd uses D from previous iteration, but if we update in-place, it's fine and yields correct shortest paths because we are using D[i][k] and D[k][j] that may have been updated in the same k iteration; but that's okay because they would be paths that go through nodes <k. However, standard implementation uses D as a 2D array and updates in place; the order of i,j loops doesn't matter if we use D[i][k] and D[k][j] from current iteration (i.e., possibly updated earlier in same k). That's acceptable and yields correct all-pairs shortest paths (since the algorithm works even with in-place updates). But careful: If we use in-place, we might use a D[i][k] that includes node k as intermediate? Actually Floyd-Warshall algorithm's correctness relies on using the value of D[i][k] and D[k][j] from previous iteration when k is the only intermediate allowed. In the standard formulation, we have D(k)[i][j] meaning shortest path using intermediate nodes among {1..k}. Then recurrence: D(k)[i][j] = min(D(k-1)[i][j], D(k-1)[i][k] + D(k-1)[k][j]). So we need to use values from previous k-1 iteration. If we update in-place, we risk using D[i][k] that was already updated to D(k)[i][k] which might include k as intermediate, but that's okay because D(k)[i][k] = D(k-1)[i][k] (since the shortest path from i to k using nodes up to k cannot be shorter than using k as intermediate? Actually, D(k)[i][k] = min(D(k-1)[i][k], D(k-1)[i][k] + D(k-1)[k][k]) = D(k-1)[i][k] because D(k-1)[k][k]=0, so it doesn't change). Similarly D(k)[k][j] = D(k-1)[k][j]. So using in-place is safe because D[i][k] and D[k][j] do not change when we set k as intermediate (since adding a zero weight loop doesn't reduce). So in-place works. So we can just do triple loop in order.
Given that, we can continue with our updates, but we need to be careful to use the current (possibly updated) values when computing further. In manual step-by-step, we should update rows sequentially. But it's easier to just write a small program mentally? Since we're doing manually, we can continue by re-calculating after each k fully, using the matrix as it stands at the start of the k iteration. However, if we use in-place updates as we go, we might be using D[i][k] that is already updated. But as argued, it's safe. For manual, we can adopt the approach: at the beginning of each k, we have a matrix D (after all previous k's). Then for each i,j, we consider candidate D[i][k] + D[k][j] using the current D[i][k] and D[k][j] which are already the shortest up to previous k (and not changed for this k because they involve k as endpoint? Actually, D[i][k] might have been updated earlier in this k iteration for some i? In typical Floyd code: 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]). Since the loops for i and j are nested inside k, when we are at a particular i,j, D[i][k] might have been updated earlier in the same k iteration for a different i? But D[i][k] is fixed for given i and k; we don't update D[i][k] in the inner loops until we get to j? Actually D[i][k] is for a specific i, and we may update D[i][k] when j equals something? In the inner loop over j, D[i][k] could be updated when j = k? Because j goes over all columns; when j = k, we might update D[i][k] = min(D[i][k], D[i][k]+D[k][k]) = D[i][k]. So no change. So D[i][k] remains unchanged during the inner j loop. Similarly D[k][j] remains unchanged because when i=k, we might update D[k][j] = min(D[k][j], D[k][k]+D[k][j]) = D[k][j]. So they stay. So in-place is fine. So we can just simulate the triple loop with the order k,i,j.
Thus we can continue after k=0, we have new D (as we computed updates). That's D after finishing k=0.
Now we proceed to k=1, using the D after k=0 (i.e., D1). So we need to know all entries of D1 to compute candidates for k=1. We already noted some updates. Let's write down D1 fully.
We'll list rows 0..9 with updated values.
I'll build a table:
Row 0: unchanged: [0,4,3,6,2,7,2,5,3,3]
Row 1: originally [4,0,5,3,7,5,3,7,2,7]; we updated D1[1][4]=6. So row1: [4,0,5,3,6,5,3,7,2,7] (only col4 changed to 6).
Row 2: originally [5,7,0,7,2,9,3,2,9,1]; we updated D1[2][8]=8. So row2: [5,7,0,7,2,9,3,2,8,1] (col8=8).
Row 3: originally [3,6,2,0,2,4,6,4,2,3]; we updated D1[3][6]=5. So row3: [3,6,2,0,2,4,5,4,2,3] (col6=5).
Row 4: originally [3,5,7,4,0,6,9,7,6,7]; we updated: D1[4][2]=6, D1[4][6]=5, D1[4][9]=6. So row4: [3,5,6,4,0,6,5,7,6,6] (col2=6, col6=5, col9=6).
Row 5: unchanged: [9,8,5,2,2,0,4,7,6,5]
Row 6: unchanged: [5,4,6,3,2,3,0,5,4,3] (we had no changes? We found none. Check: i=6, we said no improvements. So row6 unchanged.)
Row 7: originally [3,6,2,3,4,2,4,0,8,9]; we updated: D1[7][8]=6, D1[7][9]=6. So row7: [3,6,2,3,4,2,4,0,6,6] (col8=6, col9=6).
Row 8: originally [4,6,5,4,3,5,3,2,0,8]; we updated: D1[8][9]=7. So row8: [4,6,5,4,3,5,3,2,0,7] (col9=7).
Row 9: originally [2,1,3,4,5,7,8,6,4,0]; we updated: D1[9][4]=4, D1[9][6]=4. So row9: [2,1,3,4,4,7,4,6,4,0] (col4=4, col6=4).
Now we have D1 after k=0.
Now k=1: We'll use D1. We need to consider for each i, j: candidate = D1[i][1] + D1[1][j]. But careful: D1[1][j] is row1 after updates. Row1: [4,0,5,3,6,5,3,7,2,7]. D1[i][1] is column1 after updates (i.e., the distance from i to 1 after k=0). We'll compute these.
Let's note D1[i][1] values (col1):
i=0:4
i=1:0
i=2:7 (from row2 col1=7? row2 col1 is 7, unchanged)
i=3:6 (row3 col1=6)
i=4:5 (row4 col1=5)
i=5:8 (row5 col1=8)
i=6:4 (row6 col1=4)
i=7:6 (row7 col1=6)
i=8:6 (row8 col1=6)
i=9:1 (row9 col1=1)
Now we will update each row i.
We'll go through i=0..9, j=0..9.
We'll look for improvements: if D1[i][j] > D1[i][1] + D1[1][j], update.
But note D1[i][1] is the current distance from i to 1; D1[1][j] is distance from 1 to j. So candidate = distance i->1->j.
We'll systematically consider each i, but we may not need all entries; we only care about final distances to 1. However, these updates may affect distances to other nodes, which may later (through higher k) affect distances to 1. So we must perform full Floyd. But we can try to compute final dist[i][1] by focusing on all k, but we can incorporate later. Actually, we can run Floyd on the whole matrix in order. Since we are doing manual, we might as well compute final distances by repeatedly relaxing using all nodes.
Given small size, we could also compute shortest distances to 1 by running 10 Dijkstra (or Floyd). Let's just attempt to compute final distances to 1 by performing full Floyd manually with careful steps. It will be a bit of work but doable.
I'll continue Floyd iterations.
After k=1, we produce D2.
But we must also consider that D1[i][1] might be improved during the updates for k=1? Actually, for j=1, candidate = D1[i][1] + D1[1][1] = D1[i][1] + 0, so no change to D[i][1] from k=1. So dist to 1 will not change directly from k=1. But later k's may use other nodes.
Now we process k=1.
We need to go through each i and j.
Let's compute for each i, the row after updating.
We'll define current matrix as D (currently D1). We'll iterate over i from 0 to 9, j from 0 to 9, compute candidate = D[i][1] + D[1][j], compare with D[i][j], update if smaller.
We'll note changes.
First, i=0: D[0][j] = row0 currently: [0,4,3,6,2,7,2,5,3,3]. D[0][1]=4. D[1][j] = row1: [4,0,5,3,6,5,3,7,2,7].
Compute candidate for each j:
j=0: 4+4=8 > 0 -> no.
j=1: 4+0=4 equals current 4 -> no.
j=2: 4+5=9 > 3 -> no.
j=3: 4+3=7 > 6? 7 > 6, no.
j=4: 4+6=10 > 2 -> no.
j=5: 4+5=9 > 7 -> no.
j=6: 4+3=7 > 2 -> no.
j=7: 4+7=11 > 5 -> no.
j=8: 4+2=6 > 3? 6 > 3, no.
j=9: 4+7=11 > 3? 11 > 3, no.
No changes for i=0.
i=1: D[1][1]=0, candidate = 0 + D[1][j] = D[1][j] which is same as current. So no changes.
i=2: D[2] row currently: [5,7,0,7,2,9,3,2,8,1]. D[2][1]=7. Compute candidates:
candidate = 7 + D[1][j].
List D[1][j]: [4,0,5,3,6,5,3,7,2,7]
So candidate per j:
j=0:7+4=11 >5 -> no.
j=1:7+0=7 equals 7 -> same.
j=2:7+5=12 >0 -> no.
j=3:7+3=10 >7 -> no.
j=4:7+6=13 >2 -> no.
j=5:7+5=12 >9 -> no.
j=6:7+3=10 >3 -> no.
j=7:7+7=14 >2 -> no.
j=8:7+2=9 >8? 9 >8 -> no, but 9 > 8, so no improvement.
j=9:7+7=14 >1 -> no.
So no changes for i=2.
i=3: D[3] row: [3,6,2,0,2,4,5,4,2,3]. D[3][1]=6. Candidate =6 + D[1][j]:
j=0:6+4=10 >3? no.
j=1:6+0=6 equals 6 -> same.
j=2:6+5=11 >2 -> no.
j=3:6+3=9 >0 -> no.
j=4:6+6=12 >2 -> no.
j=5:6+5=11 >4 -> no.
j=6:6+3=9 >5 -> no.
j=7:6+7=13 >4 -> no.
j=8:6+2=8 >2? 8 > 2 no.
j=9:6+7=13 >3 -> no.
No changes.
i=4: D[4] row: [3,5,6,4,0,6,5,7,6,6]. D[4][1]=5. Candidate =5 + D[1][j]:
Compute D[1][j] as above.
j=0:5+4=9 >3 no.
j=1:5+0=5 =5 ok.
j=2:5+5=10 >6? 10>6 no.
j=3:5+3=8 >4? 8>4 no.
j=4:5+6=11 >0 no.
j=5:5+5=10 >6? 10>6 no.
j=6:5+3=8 >5? 8>5 no.
j=7:5+7=12 >7? 12>7 no.
j=8:5+2=7 >6? 7>6 -> no? Actually 7 > 6, so no.
j=9:5+7=12 >6 no.
No changes.
i=5: D[5] row: [9,8,5,2,2,0,4,7,6,5]. D[5][1]=8. Candidate =8 + D[1][j]:
j=0:8+4=12 >9? 12>9 -> update? 12 > 9, no improvement. Actually 12 > 9, so not better.
j=1:8+0=8 =8.
j=2:8+5=13 >5 no.
j=3:8+3=11 >2 no.
j=4:8+6=14 >2 no.
j=5:8+5=13 >0 no.
j=6:8+3=11 >4 no.
j=7:8+7=15 >7 no.
j=8:8+2=10 >6? 10>6 no.
j=9:8+7=15 >5 no.
No changes.
i=6: D[6] row: [5,4,6,3,2,3,0,5,4,3]. D[6][1]=4. Candidate =4 + D[1][j]:
j=0:4+4=8 >5? 8>5 no.
j=1:4+0=4 =4.
j=2:4+5=9 >6? 9>6 no.
j=3:4+3=7 >3? 7>3 no.
j=4:4+6=10 >2 no.
j=5:4+5=9 >3 no.
j=6:4+3=7 >0 no.
j=7:4+7=11 >5 no.
j=8:4+2=6 >4? 6>4 no.
j=9:4+7=11 >3 no.
No changes.
i=7: D[7] row: [3,6,2,3,4,2,4,0,6,6]. D[7][1]=6. Candidate =6 + D[1][j]:
j=0:6+4=10 >3 no.
j=1:6+0=6 =6.
j=2:6+5=11 >2 no.
j=3:6+3=9 >3? 9>3 no.
j=4:6+6=12 >4 no.
j=5:6+5=11 >2 no.
j=6:6+3=9 >4? 9>4 no.
j=7:6+7=13 >0 no.
j=8:6+2=8 >6? 8>6 no (8 > 6, so no improvement)
j=9:6+7=13 >6 no.
No changes.
i=8: D[8] row: [4,6,5,4,3,5,3,2,0,7]. D[8][1]=6. Candidate =6 + D[1][j]:
j=0:6+4=10 >4 no.
j=1:6+0=6 =6.
j=2:6+5=11 >5 no.
j=3:6+3=9 >4? 9>4 no.
j=4:6+6=12 >3 no.
j=5:6+5=11 >5 no.
j=6:6+3=9 >3? 9>3 no.
j=7:6+7=13 >2 no.
j=8:6+2=8 >0 no.
j=9:6+7=13 >7? 13 > 7 no.
No changes.
i=9: D[9] row: [2,1,3,4,4,7,4,6,4,0]. D[9][1]=1. Candidate =1 + D[1][j]:
j=0:1+4=5 >2? 5>2 no.
j=1:1+0=1 =1.
j=2:1+5=6 >3? 6>3 no.
j=3:1+3=4 =4? current D[9][3]=4, so equal. No improvement.
j=4:1+6=7 >4? 7>4 no.
j=5:1+5=6 >7? Actually 6 < 7! Because D[9][5] currently 7. So candidate 6 < 7, so update D[9][5]=6.
j=6:1+3=4 =4? D[9][6]=4, equal.
j=7:1+7=8 >6? 8>6 no.
j=8:1+2=3 >4? 3 < 4! D[9][8] currently 4, candidate 3, update D[9][8]=3.
j=9:1+7=8 >0 no.
So we have updates for row9: col5 becomes 6, col8 becomes 3.
Thus D2 after k=1: changes only in row9: now row9: [2,1,3,4,4,6,4,6,3,0] (col5=6, col8=3). Others unchanged.
Now k=2. We'll use D2 (which is D after k=1). We'll do updates with k=2: for each i,j, candidate = D[i][2] + D[2][j].
First, we need D[2][j] row2 from D2. Row2 is currently: [5,7,0,7,2,9,3,2,8,1] (unchanged from D1 because no updates in row2 during k=1). Also D[i][2] are the distances from i to 2.
Let's note D2 rows:
Row0: [0,4,3,6,2,7,2,5,3,3]
Row1: [4,0,5,3,6,5,3,7,2,7]
Row2: [5,7,0,7,2,9,3,2,8,1]
Row3: [3,6,2,0,2,4,5,4,2,3]
Row4: [3,5,6,4,0,6,5,7,6,6]
Row5: [9,8,5,2,2,0,4,7,6,5]
Row6: [5,4,6,3,2,3,0,5,4,3]
Row7: [3,6,2,3,4,2,4,0,6,6]
Row8: [4,6,5,4,3,5,3,2,0,7]
Row9: [2,1,3,4,4,6,4,6,3,0]
Now k=2.
We need to compute for each i, j: candidate = D[i][2] + D[2][j]. D[2][j] is row2 as above.
We'll update each row i.
First note D[i][2] values (col2):
i=0:3
i=1:5
i=2:0
i=3:2
i=4:6
i=5:5
i=6:6
i=7:2
i=8:5
i=9:3
Now we iterate i.
i=0: D[0][j] = row0. D[0][2]=3. Candidate = 3 + D[2][j].
Compute D[2][j] row2: [5,7,0,7,2,9,3,2,8,1]
So candidate per j:
j=0:3+5=8 >0 -> no.
j=1:3+7=10 >4? 10>4 no.
j=2:3+0=3 equals current 3.
j=3:3+7=10 >6? 10>6 no.
j=4:3+2=5 >2? 5>2 no.
j=5:3+9=12 >7? 12>7 no.
j=6:3+3=6 >2? 6>2 no.
j=7:3+2=5 =5? current 5, equal.
j=8:3+8=11 >3? 11>3 no.
j=9:3+1=4 >3? 4>3? 4>3, so candidate 4 > current 3, no improvement.
No changes for i=0.
i=1: D[1][2]=5. Candidate =5 + D[2][j].
j=0:5+5=10 >4? no.
j=1:5+7=12 >0? no.
j=2:5+0=5 =5 current? D[1][2]=5, okay.
j=3:5+7=12 >3? no.
j=4:5+2=7 >6? 7>6, no? current D[1][4]=6, 7>6 so no.
j=5:5+9=14 >5? no.
j=6:5+3=8 >3? no.
j=7:5+2=7 =7? D[1][7]=7, equal.
j=8:5+8=13 >2? no.
j=9:5+1=6 >7? 6 < 7? Actually D[1][9]=7, candidate 6 < 7, so update D[1][9]=6.
Thus row1 col9 becomes 6 (improved from 7).
Also check j=4: we said 7 >6 no. j=8:13>2 no. others no.
So row1 now: col9 updated.
i=2: D[2][2]=0, candidate =0+row2 = row2, no changes.
i=3: D[3][2]=2. Candidate =2 + D[2][j].
j=0:2+5=7 >3? no.
j=1:2+7=9 >6? no.
j=2:2+0=2 =2, ok.
j=3:2+7=9 >0? no.
j=4:2+2=4 >2? 4>2 no.
j=5:2+9=11 >4? no.
j=6:2+3=5 =5? D[3][6]=5, equal.
j=7:2+2=4 =4? D[3][7]=4, equal.
j=8:2+8=10 >2? no.
j=9:2+1=3 =3? D[3][9]=3, equal.
No changes for i=3.
i=4: D[4][2]=6. Candidate =6 + D[2][j].
j=0:6+5=11 >3? no.
j=1:6+7=13 >5? no.
j=2:6+0=6 =6, ok.
j=3:6+7=13 >4? no.
j=4:6+2=8 >0? no.
j=5:6+9=15 >6? no.
j=6:6+3=9 >5? no.
j=7:6+2=8 >7? 8>7 no.
j=8:6+8=14 >6? no.
j=9:6+1=7 >6? 7>6? D[4][9]=6, candidate 7 >6 no.
No changes.
i=5: D[5][2]=5. Candidate =5 + D[2][j].
j=0:5+5=10 >9? 10>9? no, but 10 > 9, so no improvement.
j=1:5+7=12 >8? no.
j=2:5+0=5 =5.
j=3:5+7=12 >2? no.
j=4:5+2=7 >2? 7>2 no.
j=5:5+9=14 >0 no.
j=6:5+3=8 >4? 8>4 no.
j=7:5+2=7 =7? D[5][7]=7, equal.
j=8:5+8=13 >6? no.
j=9:5+1=6 >5? 6>5? D[5][9]=5, candidate 6 >5 no.
No changes.
i=6: D[6][2]=6. Candidate =6 + D[2][j].
j=0:6+5=11 >5? no.
j=1:6+7=13 >4? no.
j=2:6+0=6 =6.
j=3:6+7=13 >3? no.
j=4:6+2=8 >2? no.
j=5:6+9=15 >3? no.
j=6:6+3=9 >0 no.
j=7:6+2=8 >5? no.
j=8:6+8=14 >4? no.
j=9:6+1=7 >3? 7>3 no.
No changes.
i=7: D[7][2]=2. Candidate =2 + D[2][j].
j=0:2+5=7 >3? no.
j=1:2+7=9 >6? no.
j=2:2+0=2 =2.
j=3:2+7=9 >3? no.
j=4:2+2=4 =4? D[7][4]=4, equal.
j=5:2+9=11 >2? no.
j=6:2+3=5 >4? 5>4? no (5>4, but not less). So no.
j=7:2+2=4 >0? no.
j=8:2+8=10 >6? no.
j=9:2+1=3 >6? 3 < 6! D[7][9]=6, candidate 3 < 6, so update D[7][9]=3.
Also check j=6: current 4, candidate 5 not less. j=4: 4 equal.
So row7 col9 becomes 3.
i=8: D[8][2]=5. Candidate =5 + D[2][j].
j=0:5+5=10 >4? no.
j=1:5+7=12 >6? no.
j=2:5+0=5 =5.
j=3:5+7=12 >4? no.
j=4:5+2=7 >3? no.
j=5:5+9=14 >5? no.
j=6:5+3=8 >3? 8>3 no.
j=7:5+2=7 >2? no.
j=8:5+8=13 >0 no.
j=9:5+1=6 >7? 6 < 7? D[8][9]=7, candidate 6 < 7, update D[8][9]=6.
Also check other j.
So row8 col9 becomes 6.
i=9: D[9][2]=3. Candidate =3 + D[2][j].
j=0:3+5=8 >2? no.
j=1:3+7=10 >1? no.
j=2:3+0=3 =3.
j=3:3+7=10 >4? no.
j=4:3+2=5 >4? 5>4 no.
j=5:3+9=12 >6? 12>6 no.
j=6:3+3=6 >4? 6>4 no.
j=7:3+2=5 >6? 5 < 6? D[9][7]=6, candidate 5 < 6, update D[9][7]=5.
j=8:3+8=11 >3? 11>3 no.
j=9:3+1=4 >0 no.
Also check j=5? Already 6, 12 not <6.
So row9 col7 becomes 5.
Thus after k=2, we have updates:
Row1: col9=6.
Row7: col9=3.
Row8: col9=6.
Row9: col7=5.
Also maybe others? That's it.
Now D3 after k=2 (we'll call it after finishing k=2) with these changes.
Update rows accordingly:
Row0: unchanged.
Row1: now [4,0,5,3,6,5,3,7,2,6]
Row2: unchanged.
Row3: unchanged.
Row4: unchanged.
Row5: unchanged.
Row6: unchanged.
Row7: [3,6,2,3,4,2,4,0,6,3]
Row8: [4,6,5,4,3,5,3,2,0,6]
Row9: [2,1,3,4,4,6,4,5,3,0] (col7=5)
Now k=3. We'll use D after k=2.
We need D[i][3] (col3) and D[3][j] (row3). Row3 is currently: [3,6,2,0,2,4,5,4,2,3] (unchanged from earlier, as no updates to row3 in k=2). So row3: as D1.
Let's list D after k=2 (D3) rows:
Row0: [0,4,3,6,2,7,2,5,3,3]
Row1: [4,0,5,3,6,5,3,7,2,6]
Row2: [5,7,0,7,2,9,3,2,8,1]
Row3: [3,6,2,0,2,4,5,4,2,3]
Row4: [3,5,6,4,0,6,5,7,6,6]
Row5: [9,8,5,2,2,0,4,7,6,5]
Row6: [5,4,6,3,2,3,0,5,4,3]
Row7: [3,6,2,3,4,2,4,0,6,3]
Row8: [4,6,5,4,3,5,3,2,0,6]
Row9: [2,1,3,4,4,6,4,5,3,0]
Now D[i][3] values (col3):
i=0:6
i=1:3
i=2:7
i=3:0
i=4:4
i=5:2
i=6:3
i=7:3
i=8:4
i=9:4
Row3 as above.
Now for each i, compute candidate = D[i][3] + D[3][j].
We'll go i=0..9.
i=0: D[0][3]=6. Candidate =6 + D[3][j].
D[3][j]: [3,6,2,0,2,4,5,4,2,3]
So candidate per j:
j=0:6+3=9 >0 no.
j=1:6+6=12 >4 no.
j=2:6+2=8 >3? 8>3 no.
j=3:6+0=6 =6, equal.
j=4:6+2=8 >2 no.
j=5:6+4=10 >7 no.
j=6:6+5=11 >2 no.
j=7:6+4=10 >5 no.
j=8:6+2=8 >3? 8>3 no.
j=9:6+3=9 >3? 9>3 no.
No changes.
i=1: D[1][3]=3. Candidate =3 + D[3][j].
j=0:3+3=6 >4? 6>4 no.
j=1:3+6=9 >0 no.
j=2:3+2=5 =5? D[1][2]=5, equal.
j=3:3+0=3 =3, equal.
j=4:3+2=5 <6? D[1][4]=6, candidate 5 < 6, so update D[1][4]=5.
j=5:3+4=7 >5? 7>5 no.
j=6:3+5=8 >3? 8>3 no.
j=7:3+4=7 =7? D[1][7]=7, equal.
j=8:3+2=5 >2? 5>2 no.
j=9:3+3=6 =6? D[1][9]=6, equal.
Thus row1 col4 becomes 5.
i=2: D[2][3]=7. Candidate =7 + D[3][j].
j=0:7+3=10 >5 no.
j=1:7+6=13 >7 no.
j=2:7+2=9 >0 no.
j=3:7+0=7 =7, equal.
j=4:7+2=9 >2 no.
j=5:7+4=11 >9 no.
j=6:7+5=12 >3 no.
j=7:7+4=11 >2 no.
j=8:7+2=9 >8? 9>8 no.
j=9:7+3=10 >1 no.
No changes.
i=3: D[3][3]=0, candidate = row3, no changes.
i=4: D[4][3]=4. Candidate =4 + D[3][j].
j=0:4+3=7 >3? no.
j=1:4+6=10 >5? no.
j=2:4+2=6 =6? D[4][2]=6, equal.
j=3:4+0=4 =4.
j=4:4+2=6 >0 no.
j=5:4+4=8 >6? 8>6 no.
j=6:4+5=9 >5? no.
j=7:4+4=8 >7? 8>7 no.
j=8:4+2=6 =6? D[4][8]=6, equal.
j=9:4+3=7 >6? 7>6 no.
No changes.
i=5: D[5][3]=2. Candidate =2 + D[3][j].
j=0:2+3=5 >9? 5 < 9! D[5][0]=9, candidate 5 < 9, update D[5][0]=5.
j=1:2+6=8 >8? 8 = 8? D[5][1]=8, equal.
j=2:2+2=4 <5? D[5][2]=5, candidate 4 < 5, update D[5][2]=4.
j=3:2+0=2 =2, equal.
j=4:2+2=4 >2? 4>2 no.
j=5:2+4=6 >0 no.
j=6:2+5=7 >4? 7>4 no.
j=7:2+4=6 >7? 6 < 7! D[5][7]=7, candidate 6 < 7, update D[5][7]=6.
j=8:2+2=4 <6? D[5][8]=6, candidate 4 < 6, update D[5][8]=4.
j=9:2+3=5 =5? D[5][9]=5, equal.
So row5 gets multiple updates: col0=5, col2=4, col7=6, col8=4.
i=6: D[6][3]=3. Candidate =3 + D[3][j].
j=0:3+3=6 >5? 6>5 no.
j=1:3+6=9 >4? 9>4 no.
j=2:3+2=5 <6? D[6][2]=6, candidate 5 < 6, update D[6][2]=5.
j=3:3+0=3 =3.
j=4:3+2=5 >2? 5>2 no.
j=5:3+4=7 >3? 7>3 no.
j=6:3+5=8 >0 no.
j=7:3+4=7 >5? 7>5 no.
j=8:3+2=5 >4? 5>4, no (5>4, not less). Actually 5 > 4, so no.
j=9:3+3=6 >3? 6>3 no.
Thus row6 col2 becomes 5.
i=7: D[7][3]=3. Candidate =3 + D[3][j].
j=0:3+3=6 >3? no.
j=1:3+6=9 >6? no.
j=2:3+2=5 >2? 5>2 no.
j=3:3+0=3 =3.
j=4:3+2=5 >4? 5>4 no.
j=5:3+4=7 >2? 7>2 no.
j=6:3+5=8 >4? 8>4 no.
j=7:3+4=7 >0? no.
j=8:3+2=5 >6? 5 < 6? D[7][8]=6, candidate 5 < 6, update D[7][8]=5.
j=9:3+3=6 >3? 6>3? D[7][9]=3, candidate 6 > 3, no.
So row7 col8 becomes 5.
i=8: D[8][3]=4. Candidate =4 + D[3][j].
j=0:4+3=7 >4? 7>4 no.
j=1:4+6=10 >6 no.
j=2:4+2=6 >5? 6>5 no.
j=3:4+0=4 =4.
j=4:4+2=6 >3? 6>3 no.
j=5:4+4=8 >5? 8>5 no.
j=6:4+5=9 >3? 9>3 no.
j=7:4+4=8 >2? 8>2 no.
j=8:4+2=6 >0? no.
j=9:4+3=7 >6? 7>6? D[8][9]=6, candidate 7 >6 no.
No changes.
i=9: D[9][3]=4. Candidate =4 + D[3][j].
j=0:4+3=7 >2? no.
j=1:4+6=10 >1 no.
j=2:4+2=6 >3? 6>3 no.
j=3:4+0=4 =4.
j=4:4+2=6 >4? 6>4 no.
j=5:4+4=8 >6? 8>6 no.
j=6:4+5=9 >4? 9>4 no.
j=7:4+4=8 >5? 8>5 no.
j=8:4+2=6 >3? 6>3? 6>3, no improvement.
j=9:4+3=7 >0 no.
No changes.
Thus after k=3, we have updates:
Row1: col4=5.
Row5: col0=5, col2=4, col7=6, col8=4.
Row6: col2=5.
Row7: col8=5.
Now D after k=3 (D4) becomes:
Row0: [0,4,3,6,2,7,2,5,3,3]
Row1: [4,0,5,3,5,5,3,7,2,6] (col4=5)
Row2: [5,7,0,7,2,9,3,2,8,1]
Row3: [3,6,2,0,2,4,5,4,2,3]
Row4: [3,5,6,4,0,6,5,7,6,6]
Row5: [5,8,4,2,2,0,4,6,4,5] (col0=5, col2=4, col7=6, col8=4)
Row6: [5,4,5,3,2,3,0,5,4,3] (col2=5)
Row7: [3,6,2,3,4,2,4,0,5,3] (col8=5)
Row8: [4,6,5,4,3,5,3,2,0,6]
Row9: [2,1,3,4,4,6,4,5,3,0]
Now k=4. We'll use D after k=3.
We need D[i][4] (col4) and D[4][j] (row4). Row4 currently: [3,5,6,4,0,6,5,7,6,6] (unchanged from earlier? Actually row4 hasn't been updated since D1, and no updates in k=2,3? In k=3 we didn't update row4. So row4 remains same: [3,5,6,4,0,6,5,7,6,6]).
Now D[i][4] values (col4):
i=0:2
i=1:5 (updated to 5)
i=2:2
i=3:2
i=4:0
i=5:2
i=6:2
i=7:4
i=8:3
i=9:4
Now we iterate i.
i=0: D[0][4]=2. Candidate =2 + D[4][j].
D[4][j]: [3,5,6,4,0,6,5,7,6,6]
Compute candidate for each j:
j=0:2+3=5 >0 no.
j=1:2+5=7 >4 no.
j=2:2+6=8 >3 no.
j=3:2+4=6 =6? D[0][3]=6, equal.
j=4:2+0=2 =2.
j=5:2+6=8 >7 no.
j=6:2+5=7 >2? 7>2 no.
j=7:2+7=9 >5 no.
j=8:2+6=8 >3 no.
j=9:2+6=8 >3 no.
No changes.
i=1: D[1][4]=5. Candidate =5 + D[4][j].
j=0:5+3=8 >4? no.
j=1:5+5=10 >0 no.
j=2:5+6=11 >5? 11>5 no.
j=3:5+4=9 >3? no.
j=4:5+0=5 =5.
j=5:5+6=11 >5? no.
j=6:5+5=10 >3? no.
j=7:5+7=12 >7? no.
j=8:5+6=11 >2? no.
j=9:5+6=11 >6? 11>6 no.
No changes.
i=2: D[2][4]=2. Candidate =2 + D[4][j].
j=0:2+3=5 =5? D[2][0]=5, equal.
j=1:2+5=7 =7? D[2][1]=7, equal.
j=2:2+6=8 >0 no.
j=3:2+4=6 <7? D[2][3]=7, candidate 6 < 7, update D[2][3]=6.
j=4:2+0=2 =2.
j=5:2+6=8 <9? D[2][5]=9, candidate 8 <9, update D[2][5]=8.
j=6:2+5=7 >3? no.
j=7:2+7=9 >2? no.
j=8:2+6=8 =8? D[2][8]=8, equal.
j=9:2+6=8 >1? 8>1 no.
Thus row2: col3 becomes 6, col5 becomes 8.
i=3: D[3][4]=2. Candidate =2 + D[4][j].
j=0:2+3=5 >3? no.
j=1:2+5=7 >6? 7>6 no.
j=2:2+6=8 >2? no.
j=3:2+4=6 >0? no.
j=4:2+0=2 =2.
j=5:2+6=8 >4? 8>4 no.
j=6:2+5=7 >5? 7>5 no.
j=7:2+7=9 >4? no.
j=8:2+6=8 >2? no.
j=9:2+6=8 >3? 8>3 no.
No changes.
i=4: D[4][4]=0, candidate = row4, no changes.
i=5: D[5][4]=2. Candidate =2 + D[4][j].
j=0:2+3=5 =5? D[5][0]=5, equal.
j=1:2+5=7 <8? D[5][1]=8, candidate 7 <8, update D[5][1]=7.
j=2:2+6=8 >4? 8>4 no.
j=3:2+4=6 >2? no.
j=4:2+0=2 =2.
j=5:2+6=8 >0 no.
j=6:2+5=7 >4? 7>4 no.
j=7:2+7=9 >6? 9>6 no.
j=8:2+6=8 >4? 8>4 no.
j=9:2+6=8 >5? 8>5 no.
So row5 col1 becomes 7.
i=6: D[6][4]=2. Candidate =2 + D[4][j].
j=0:2+3=5 =5? D[6][0]=5, equal.
j=1:2+5=7 >4? 7>4 no.
j=2:2+6=8 >5? 8>5 no.
j=3:2+4=6 >3? 6>3 no.
j=4:2+0=2 =2.
j=5:2+6=8 >3? 8>3 no.
j=6:2+5=7 >0 no.
j=7:2+7=9 >5? 9>5 no.
j=8:2+6=8 >4? 8>4 no.
j=9:2+6=8 >3? 8>3 no.
No changes.
i=7: D[7][4]=4. Candidate =4 + D[4][j].
j=0:4+3=7 >3? no.
j=1:4+5=9 >6? no.
j=2:4+6=10 >2? no.
j=3:4+4=8 >3? no.
j=4:4+0=4 =4.
j=5:4+6=10 >2? no.
j=6:4+5=9 >4? 9>4 no.
j=7:4+7=11 >0? no.
j=8:4+6=10 >5? 10>5 no.
j=9:4+6=10 >3? 10>3 no.
No changes.
i=8: D[8][4]=3. Candidate =3 + D[4][j].
j=0:3+3=6 >4? no.
j=1:3+5=8 >6? no.
j=2:3+6=9 >5? 9>5 no.
j=3:3+4=7 >4? 7>4 no.
j=4:3+0=3 =3.
j=5:3+6=9 >5? 9>5 no.
j=6:3+5=8 >3? 8>3 no.
j=7:3+7=10 >2? no.
j=8:3+6=9 >0? no.
j=9:3+6=9 >6? 9>6 no.
No changes.
i=9: D[9][4]=4. Candidate =4 + D[4][j].
j=0:4+3=7 >2? no.
j=1:4+5=9 >1 no.
j=2:4+6=10 >3? no.
j=3:4+4=8 >4? 8>4 no.
j=4:4+0=4 =4.
j=5:4+6=10 >6? 10>6 no.
j=6:4+5=9 >4? no.
j=7:4+7=11 >5? no.
j=8:4+6=10 >3? no.
j=9:4+6=10 >0 no.
No changes.
Thus after k=4, we have updates:
Row2: col3=6, col5=8.
Row5: col1=7.
Now D after k=4 (D5):
Row0: [0,4,3,6,2,7,2,5,3,3]
Row1: [4,0,5,3,5,5,3,7,2,6]
Row2: [5,7,0,6,2,8,3,2,8,1] (col3=6, col5=8)
Row3: [3,6,2,0,2,4,5,4,2,3]
Row4: [3,5,6,4,0,6,5,7,6,6]
Row5: [5,7,4,2,2,0,4,6,4,5] (col1=7)
Row6: [5,4,5,3,2,3,0,5,4,3]
Row7: [3,6,2,3,4,2,4,0,5,3]
Row8: [4,6,5,4,3,5,3,2,0,6]
Row9: [2,1,3,4,4,6,4,5,3,0]
Now k=5. Use D after k=4.
We need D[i][5] (col5) and D[5][j] (row5). Row5 currently: [5,7,4,2,2,0,4,6,4,5]
Now D[i][5] values (col5):
i=0:7
i=1:5
i=2:8
i=3:4
i=4:6
i=5:0
i=6:3
i=7:2
i=8:5
i=9:6
Now iterate i.
i=0: D[0][5]=7. Candidate =7 + D[5][j].
D[5][j]: as above.
Compute candidate for each j:
j=0:7+5=12 >0 no.
j=1:7+7=14 >4 no.
j=2:7+4=11 >3 no.
j=3:7+2=9 >6? 9>6 no.
j=4:7+2=9 >2 no.
j=5:7+0=7 =7.
j=6:7+4=11 >2 no.
j=7:7+6=13 >5 no.
j=8:7+4=11 >3 no.
j=9:7+5=12 >3 no.
No changes.
i=1: D[1][5]=5. Candidate =5 + D[5][j].
j=0:5+5=10 >4? no.
j=1:5+7=12 >0 no.
j=2:5+4=9 >5? 9>5 no.
j=3:5+2=7 >3? 7>3 no.
j=4:5+2=7 >5? 7>5 no.
j=5:5+0=5 =5.
j=6:5+4=9 >3? 9>3 no.
j=7:5+6=11 >7? 11>7 no.
j=8:5+4=9 >2? no.
j=9:5+5=10 >6? 10>6 no.
No changes.
i=2: D[2][5]=8. Candidate =8 + D[5][j].
j=0:8+5=13 >5 no.
j=1:8+7=15 >7 no.
j=2:8+4=12 >0 no.
j=3:8+2=10 >6? 10>6 no.
j=4:8+2=10 >2 no.
j=5:8+0=8 =8.
j=6:8+4=12 >3 no.
j=7:8+6=14 >2 no.
j=8:8+4=12 >8? 12>8 no.
j=9:8+5=13 >1 no.
No changes.
i=3: D[3][5]=4. Candidate =4 + D[5][j].
j=0:4+5=9 >3 no.
j=1:4+7=11 >6 no.
j=2:4+4=8 >2? 8>2 no.
j=3:4+2=6 >0 no.
j=4:4+2=6 >2 no.
j=5:4+0=4 =4.
j=6:4+4=8 >5? 8>5 no.
j=7:4+6=10 >4 no.
j=8:4+4=8 >2? 8>2 no.
j=9:4+5=9 >3 no.
No changes.
i=4: D[4][5]=6. Candidate =6 + D[5][j].
j=0:6+5=11 >3 no.
j=1:6+7=13 >5 no.
j=2:6+4=10 >6? 10>6 no.
j=3:6+2=8 >4? 8>4 no.
j=4:6+2=8 >0 no.
j=5:6+0=6 =6.
j=6:6+4=10 >5? 10>5 no.
j=7:6+6=12 >7? 12>7 no.
j=8:6+4=10 >6? 10>6 no.
j=9:6+5=11 >6? 11>6 no.
No changes.
i=5: D[5][5]=0, candidate = row5, no changes.
i=6: D[6][5]=3. Candidate =3 + D[5][j].
j=0:3+5=8 >5? 8>5 no.
j=1:3+7=10 >4? 10>4 no.
j=2:3+4=7 >5? 7>5? yes, 7 > 5, but not less. Actually current D[6][2]=5, candidate 7 >5, no.
j=3:3+2=5 >3? 5>3 no.
j=4:3+2=5 >2? 5>2 no.
j=5:3+0=3 =3.
j=6:3+4=7 >0 no.
j=7:3+6=9 >5? 9>5 no.
j=8:3+4=7 >4? 7>4 no.
j=9:3+5=8 >3? 8>3 no.
No changes.
i=7: D[7][5]=2. Candidate =2 + D[5][j].
j=0:2+5=7 >3? no.
j=1:2+7=9 >6? no.
j=2:2+4=6 >2? 6>2 no.
j=3:2+2=4 >3? 4>3 no? Actually current D[7][3]=3, candidate 4 > 3, no.
j=4:2+2=4 =4? D[7][4]=4, equal.
j=5:2+0=2 =2.
j=6:2+4=6 >4? 6>4 no.
j=7:2+6=8 >0? no.
j=8:2+4=6 >5? 6>5? no, 6 > 5 so no improvement.
j=9:2+5=7 >3? 7>3 no.
No changes.
i=8: D[8][5]=5. Candidate =5 + D[5][j].
j=0:5+5=10 >4? no.
j=1:5+7=12 >6? no.
j=2:5+4=9 >5? 9>5 no.
j=3:5+2=7 >4? no.
j=4:5+2=7 >3? no.
j=5:5+0=5 =5.
j=6:5+4=9 >3? 9>3 no.
j=7:5+6=11 >2? no.
j=8:5+4=9 >0? no.
j=9:5+5=10 >6? 10>6 no.
No changes.
i=9: D[9][5]=6. Candidate =6 + D[5][j].
j=0:6+5=11 >2 no.
j=1:6+7=13 >1 no.
j=2:6+4=10 >3 no.
j=3:6+2=8 >4? no.
j=4:6+2=8 >4? no.
j=5:6+0=6 =6.
j=6:6+4=10 >4? no.
j=7:6+6=12 >5? no.
j=8:6+4=10 >3? no.
j=9:6+5=11 >0 no.
No changes.
So after k=5, no updates? Actually we had none. So D remains same as after k=4.
Now k=6. Use D after k=5 (same as D5). We need D[i][6] (col6) and D[6][j] (row6). Row6 currently: [5,4,5,3,2,3,0,5,4,3]
Now D[i][6] values (col6):
i=0:2
i=1:3
i=2:3
i=3:5
i=4:5
i=5:4
i=6:0
i=7:4
i=8:3
i=9:4
Now iterate i.
i=0: D[0][6]=2. Candidate =2 + D[6][j].
D[6][j]: as above.
Compute:
j=0:2+5=7 >0 no.
j=1:2+4=6 >4? 6>4 no.
j=2:2+5=7 >3? 7>3 no.
j=3:2+3=5 <6? D[0][3]=6, candidate 5 <6, update D[0][3]=5.
j=4:2+2=4 >2? 4>2 no.
j=5:2+3=5 <7? D[0][5]=7, candidate 5 <7, update D[0][5]=5.
j=6:2+0=2 =2.
j=7:2+5=7 >5? 7>5 no.
j=8:2+4=6 >3? 6>3 no.
j=9:2+3=5 >3? 5>3 no.
Thus row0 updates: col3=5, col5=5.
i=1: D[1][6]=3. Candidate =3 + D[6][j].
j=0:3+5=8 >4? no.
j=1:3+4=7 >0? no.
j=2:3+5=8 >5? 8>5 no.
j=3:3+3=6 >3? 6>3? D[1][3]=3, candidate 6 >3 no.
j=4:3+2=5 =5? D[1][4]=5, equal.
j=5:3+3=6 >5? 6>5 no.
j=6:3+0=3 =3.
j=7:3+5=8 >7? 8>7 no.
j=8:3+4=7 >2? 7>2 no.
j=9:3+3=6 =6? D[1][9]=6, equal.
No changes.
i=2: D[2][6]=3. Candidate =3 + D[6][j].
j=0:3+5=8 >5? no.
j=1:3+4=7 =7? D[2][1]=7, equal.
j=2:3+5=8 >0 no.
j=3:3+3=6 =6? D[2][3]=6, equal.
j=4:3+2=5 >2? no.
j=5:3+3=6 <8? D[2][5]=8, candidate 6 <8, update D[2][5]=6.
j=6:3+0=3 =3.
j=7:3+5=8 >2? no.
j=8:3+4=7 <8? D[2][8]=8, candidate 7 <8, update D[2][8]=7.
j=9:3+3=6 >1? no.
Thus row2: col5=6, col8=7.
i=3: D[3][6]=5. Candidate =5 + D[6][j].
j=0:5+5=10 >3? no.
j=1:5+4=9 >6? no.
j=2:5+5=10 >2? no.
j=3:5+3=8 >0 no.
j=4:5+2=7 >2? no.
j=5:5+3=8 >4? no.
j=6:5+0=5 =5.
j=7:5+5=10 >4? no.
j=8:5+4=9 >2? no.
j=9:5+3=8 >3? 8>3 no.
No changes.
i=4: D[4][6]=5. Candidate =5 + D[6][j].
j=0:5+5=10 >3? no.
j=1:5+4=9 >5? 9>5 no.
j=2:5+5=10 >6? 10>6 no.
j=3:5+3=8 >4? no.
j=4:5+2=7 >0? no.
j=5:5+3=8 >6? 8>6 no.
j=6:5+0=5 =5.
j=7:5+5=10 >7? 10>7 no.
j=8:5+4=9 >6? 9>6 no.
j=9:5+3=8 >6? 8>6 no.
No changes.
i=5: D[5][6]=4. Candidate =4 + D[6][j].
j=0:4+5=9 >5? 9>5 no.
j=1:4+4=8 >7? 8>7 no.
j=2:4+5=9 >4? 9>4 no.
j=3:4+3=7 >2? 7>2 no.
j=4:4+2=6 >2? 6>2 no.
j=5:4+3=7 >0 no.
j=6:4+0=4 =4.
j=7:4+5=9 >6? 9>6 no.
j=8:4+4=8 >4? 8>4 no.
j=9:4+3=7 >5? 7>5 no.
No changes.
i=6: D[6][6]=0, candidate = row6, no changes.
i=7: D[7][6]=4. Candidate =4 + D[6][j].
j=0:4+5=9 >3? no.
j=1:4+4=8 >6? no.
j=2:4+5=9 >2? no.
j=3:4+3=7 >3? 7>3 no.
j=4:4+2=6 >4? 6>4 no.
j=5:4+3=7 >2? no.
j=6:4+0=4 =4.
j=7:4+5=9 >0? no.
j=8:4+4=8 >5? 8>5 no.
j=9:4+3=7 >3? 7>3 no.
No changes.
i=8: D[8][6]=3. Candidate =3 + D[6][j].
j=0:3+5=8 >4? no.
j=1:3+4=7 >6? no.
j=2:3+5=8 >5? 8>5 no.
j=3:3+3=6 >4? 6>4 no.
j=4:3+2=5 >3? 5>3 no.
j=5:3+3=6 >5? 6>5 no? Actually D[8][5]=5, candidate 6 > 5, so no.
j=6:3+0=3 =3.
j=7:3+5=8 >2? no.
j=8:3+4=7 >0? no.
j=9:3+3=6 =6? D[8][9]=6, equal.
No changes.
i=9: D[9][6]=4. Candidate =4 + D[6][j].
j=0:4+5=9 >2? no.
j=1:4+4=8 >1? no.
j=2:4+5=9 >3? 9>3 no.
j=3:4+3=7 >4? 7>4 no.
j=4:4+2=6 >4? 6>4 no.
j=5:4+3=7 >6? 7>6 no? D[9][5]=6, candidate 7 >6, no.
j=6:4+0=4 =4.
j=7:4+5=9 >5? 9>5 no.
j=8:4+4=8 >3? 8>3 no.
j=9:4+3=7 >0 no.
No changes.
Thus after k=6, we have updates:
Row0: col3=5, col5=5.
Row2: col5=6, col8=7.
Now D after k=6 (D6):
Row0: [0,4,3,5,2,5,2,5,3,3] (col3=5, col5=5)
Row1: [4,0,5,3,5,5,3,7,2,6]
Row2: [5,7,0,6,2,6,3,2,7,1] (col5=6, col8=7)
Row3: [3,6,2,0,2,4,5,4,2,3]
Row4: [3,5,6,4,0,6,5,7,6,6]
Row5: [5,7,4,2,2,0,4,6,4,5]
Row6: [5,4,5,3,2,3,0,5,4,3]
Row7: [3,6,2,3,4,2,4,0,5,3]
Row8: [4,6,5,4,3,5,3,2,0,6]
Row9: [2,1,3,4,4,6,4,5,3,0]
Now k=7. Use D after k=6.
We need D[i][7] (col7) and D[7][j] (row7). Row7 currently: [3,6,2,3,4,2,4,0,5,3]
Now D[i][7] values (col7):
i=0:5
i=1:7
i=2:2
i=3:4
i=4:7
i=5:6
i=6:5
i=7:0
i=8:2
i=9:5
Now iterate i.
i=0: D[0][7]=5. Candidate =5 + D[7][j].
Compute D[7][j]: [3,6,2,3,4,2,4,0,5,3]
j=0:5+3=8 >0 no.
j=1:5+6=11 >4 no.
j=2:5+2=7 >3? 7>3 no.
j=3:5+3=8 >5? 8>5? D[0][3]=5, candidate 8 >5, no.
j=4:5+4=9 >2 no.
j=5:5+2=7 >5? 7>5 no? D[0][5]=5, candidate 7 >5, no.
j=6:5+4=9 >2 no.
j=7:5+0=5 =5.
j=8:5+5=10 >3 no.
j=9:5+3=8 >3 no.
No changes.
i=1: D[1][7]=7. Candidate =7 + D[7][j].
j=0:7+3=10 >4 no.
j=1:7+6=13 >0 no.
j=2:7+2=9 >5? 9>5 no.
j=3:7+3=10 >3? 10>3 no.
j=4:7+4=11 >5? 11>5 no.
j=5:7+2=9 >5? 9>5 no.
j=6:7+4=11 >3? 11>3 no.
j=7:7+0=7 =7.
j=8:7+5=12 >2? 12>2 no.
j=9:7+3=10 >6? 10>6 no.
No changes.
i=2: D[2][7]=2. Candidate =2 + D[7][j].
j=0:2+3=5 =5? D[2][0]=5, equal.
j=1:2+6=8 >7? 8>7 no.
j=2:2+2=4 >0? no.
j=3:2+3=5 <6? D[2][3]=6, candidate 5 <6, update D[2][3]=5.
j=4:2+4=6 >2? 6>2 no.
j=5:2+2=4 <6? D[2][5]=6, candidate 4 <6, update D[2][5]=4.
j=6:2+4=6 >3? 6>3? D[2][6]=3, candidate 6 >3, no.
j=7:2+0=2 =2.
j=8:2+5=7 =7? D[2][8]=7, equal.
j=9:2+3=5 >1? 5>1 no.
Thus row2: col3=5, col5=4.
i=3: D[3][7]=4. Candidate =4 + D[7][j].
j=0:4+3=7 >3? no.
j=1:4+6=10 >6? 10>6 no.
j=2:4+2=6 >2? 6>2 no.
j=3:4+3=7 >0 no.
j=4:4+4=8 >2 no.
j=5:4+2=6 >4? 6>4 no.
j=6:4+4=8 >5? 8>5 no.
j=7:4+0=4 =4.
j=8:4+5=9 >2? no.
j=9:4+3=7 >3? 7>3 no.
No changes.
i=4: D[4][7]=7. Candidate =7 + D[7][j].
j=0:7+3=10 >3? no.
j=1:7+6=13 >5? no.
j=2:7+2=9 >6? 9>6 no.
j=3:7+3=10 >4? 10>4 no.
j=4:7+4=11 >0 no.
j=5:7+2=9 >6? 9>6 no.
j=6:7+4=11 >5? no.
j=7:7+0=7 =7.
j=8:7+5=12 >6? 12>6 no.
j=9:7+3=10 >6? no.
No changes.
i=5: D[5][7]=6. Candidate =6 + D[7][j].
j=0:6+3=9 >5? 9>5 no.
j=1:6+6=12 >7? 12>7 no.
j=2:6+2=8 >4? 8>4 no.
j=3:6+3=9 >2? 9>2 no.
j=4:6+4=10 >2? 10>2 no.
j=5:6+2=8 >0 no.
j=6:6+4=10 >4? 10>4 no.
j=7:6+0=6 =6.
j=8:6+5=11 >4? 11>4 no.
j=9:6+3=9 >5? 9>5 no.
No changes.
i=6: D[6][7]=5. Candidate =5 + D[7][j].
j=0:5+3=8 >5? 8>5 no.
j=1:5+6=11 >4? no.
j=2:5+2=7 >5? 7>5 no.
j=3:5+3=8 >3? 8>3 no.
j=4:5+4=9 >2 no.
j=5:5+2=7 >3? 7>3 no.
j=6:5+4=9 >0 no.
j=7:5+0=5 =5.
j=8:5+5=10 >4? 10>4 no.
j=9:5+3=8 >3? 8>3 no.
No changes.
i=7: D[7][7]=0, candidate = row7, no changes.
i=8: D[8][7]=2. Candidate =2 + D[7][j].
j=0:2+3=5 >4? 5>4 no.
j=1:2+6=8 >6? 8>6 no.
j=2:2+2=4 <5? D[8][2]=5, candidate 4 <5, update D[8][2]=4.
j=3:2+3=5 >4? 5>4 no? D[8][3]=4, candidate 5 >4, no.
j=4:2+4=6 >3? 6>3 no.
j=5:2+2=4 <5? D[8][5]=5, candidate 4 <5, update D[8][5]=4.
j=6:2+4=6 >3? 6>3 no.
j=7:2+0=2 =2.
j=8:2+5=7 >0? no.
j=9:2+3=5 <6? D[8][9]=6, candidate 5 <6, update D[8][9]=5.
Thus row8: col2=4, col5=4, col9=5.
i=9: D[9][7]=5. Candidate =5 + D[7][j].
j=0:5+3=8 >2? no.
j=1:5+6=11 >1 no.
j=2:5+2=7 >3? 7>3 no.
j=3:5+3=8 >4? 8>4 no.
j=4:5+4=9 >4? 9>4 no.
j=5:5+2=7 >6? 7>6 no? D[9][5]=6, candidate 7 >6, no.
j=6:5+4=9 >4? 9>4 no.
j=7:5+0=5 =5? D[9][7]=5, equal.
j=8:5+5=10 >3? 10>3 no.
j=9:5+3=8 >0 no.
No changes.
After k=7, D7:
Row0: unchanged: [0,4,3,5,2,5,2,5,3,3]
Row1: unchanged: [4,0,5,3,5,5,3,7,2,6]
Row2: [5,7,0,5,2,4,3,2,7,1] (col3=5, col5=4)
Row3: unchanged: [3,6,2,0,2,4,5,4,2,3]
Row4: unchanged: [3,5,6,4,0,6,5,7,6,6]
Row5: unchanged: [5,7,4,2,2,0,4,6,4,5]
Row6: unchanged: [5,4,5,3,2,3,0,5,4,3]
Row7: unchanged: [3,6,2,3,4,2,4,0,5,3]
Row8: [4,6,4,4,3,4,3,2,0,5] (col2=4, col5=4, col9=5)
Row9: unchanged: [2,1,3,4,4,6,4,5,3,0]
Now k=8. Use D after k=7.
We need D[i][8] (col8) and D[8][j] (row8). Row8 currently: [4,6,4,4,3,4,3,2,0,5]
Now D[i][8] values (col8):
i=0:3
i=1:2
i=2:7
i=3:2
i=4:6
i=5:4
i=6:4
i=7:5
i=8:0
i=9:3
Now iterate i.
i=0: D[0][8]=3. Candidate =3 + D[8][j].
Compute D[8][j] as above.
j=0:3+4=7 >0 no.
j=1:3+6=9 >4? 9>4 no.
j=2:3+4=7 >3? 7>3 no.
j=3:3+4=7 >5? 7>5 no? D[0][3]=5, 7>5, no.
j=4:3+3=6 >2 no.
j=5:3+4=7 >5? 7>5 no.
j=6:3+3=6 >2 no.
j=7:3+2=5 =5? D[0][7]=5, equal.
j=8:3+0=3 =3.
j=9:3+5=8 >3? 8>3 no.
No changes.
i=1: D[1][8]=2. Candidate =2 + D[8][j].
j=0:2+4=6 >4? 6>4 no.
j=1:2+6=8 >0? no.
j=2:2+4=6 >5? 6>5? D[1][2]=5, candidate 6>5 no.
j=3:2+4=6 >3? 6>3 no.
j=4:2+3=5 =5? D[1][4]=5, equal.
j=5:2+4=6 >5? 6>5 no? D[1][5]=5, 6>5 no.
j=6:2+3=5 >3? 5>3 no.
j=7:2+2=4 <7? D[1][7]=7, candidate 4 <7, update D[1][7]=4.
j=8:2+0=2 =2.
j=9:2+5=7 >6? 7>6? D[1][9]=6, candidate 7 >6 no.
Thus row1 col7 becomes 4.
i=2: D[2][8]=7. Candidate =7 + D[8][j].
j=0:7+4=11 >5 no.
j=1:7+6=13 >7 no.
j=2:7+4=11 >0 no.
j=3:7+4=11 >5? 11>5 no.
j=4:7+3=10 >2 no.
j=5:7+4=11 >4? 11>4 no.
j=6:7+3=10 >3 no.
j=7:7+2=9 >2? no.
j=8:7+0=7 =7.
j=9:7+5=12 >1 no.
No changes.
i=3: D[3][8]=2. Candidate =2 + D[8][j].
j=0:2+4=6 >3? no.
j=1:2+6=8 >6? 8>6 no.
j=2:2+4=6 >2? 6>2 no.
j=3:2+4=6 >0 no.
j=4:2+3=5 >2? no.
j=5:2+4=6 >4? 6>4? D[3][5]=4, candidate 6>4 no.
j=6:2+3=5 =5? D[3][6]=5, equal.
j=7:2+2=4 =4? D[3][7]=4, equal.
j=8:2+0=2 =2.
j=9:2+5=7 >3? 7>3 no.
No changes.
i=4: D[4][8]=6. Candidate =6 + D[8][j].
j=0:6+4=10 >3? no.
j=1:6+6=12 >5? no.
j=2:6+4=10 >6? 10>6 no.
j=3:6+4=10 >4? 10>4 no.
j=4:6+3=9 >0 no.
j=5:6+4=10 >6? 10>6 no.
j=6:6+3=9 >5? no.
j=7:6+2=8 >7? 8>7 no? D[4][7]=7, candidate 8 >7, no.
j=8:6+0=6 =6.
j=9:6+5=11 >6? 11>6 no.
No changes.
i=5: D[5][8]=4. Candidate =4 + D[8][j].
j=0:4+4=8 >5? 8>5 no.
j=1:4+6=10 >7? 10>7 no.
j=2:4+4=8 >4? 8>4 no? D[5][2]=4, candidate 8 >4, no.
j=3:4+4=8 >2? 8>2 no.
j=4:4+3=7 >2? 7>2 no.
j=5:4+4=8 >0 no.
j=6:4+3=7 >4? 7>4 no.
j=7:4+2=6 =6? D[5][7]=6, equal.
j=8:4+0=4 =4.
j=9:4+5=9 >5? 9>5 no.
No changes.
i=6: D[6][8]=4. Candidate =4 + D[8][j].
j=0:4+4=8 >5? 8>5 no.
j=1:4+6=10 >4? 10>4 no.
j=2:4+4=8 >5? 8>5 no? D[6][2]=5, candidate 8>5 no.
j=3:4+4=8 >3? 8>3 no.
j=4:4+3=7 >2 no.
j=5:4+4=8 >3? 8>3 no.
j=6:4+3=7 >0 no.
j=7:4+2=6 >5? 6>5 no? D[6][7]=5, candidate 6>5, no.
j=8:4+0=4 =4.
j=9:4+5=9 >3? 9>3 no.
No changes.
i=7: D[7][8]=5. Candidate =5 + D[8][j].
j=0:5+4=9 >3? no.
j=1:5+6=11 >6? 11>6 no.
j=2:5+4=9 >2? 9>2 no.
j=3:5+4=9 >3? 9>3 no.
j=4:5+3=8 >4? 8>4 no.
j=5:5+4=9 >2? 9>2 no.
j=6:5+3=8 >4? 8>4 no.
j=7:5+2=7 >0? no.
j=8:5+0=5 =5.
j=9:5+5=10 >3? 10>3 no.
No changes.
i=8: D[8][8]=0, candidate = row8, no changes.
i=9: D[9][8]=3. Candidate =3 + D[8][j].
j=0:3+4=7 >2? no.
j=1:3+6=9 >1 no.
j=2:3+4=7 >3? 7>3 no.
j=3:3+4=7 >4? 7>4 no.
j=4:3+3=6 >4? 6>4 no.
j=5:3+4=7 >6? 7>6? D[9][5]=6, candidate 7 >6, no.
j=6:3+3=6 >4? 6>4 no.
j=7:3+2=5 =5? D[9][7]=5, equal.
j=8:3+0=3 =3.
j=9:3+5=8 >0 no.
No changes.
After k=8, D8:
Row1 col7 updated to 4.
Thus D8:
Row0: [0,4,3,5,2,5,2,5,3,3]
Row1: [4,0,5,3,5,5,3,4,2,6] (col7=4)
Row2: [5,7,0,5,2,4,3,2,7,1]
Row3: [3,6,2,0,2,4,5,4,2,3]
Row4: [3,5,6,4,0,6,5,7,6,6]
Row5: [5,7,4,2,2,0,4,6,4,5]
Row6: [5,4,5,3,2,3,0,5,4,3]
Row7: [3,6,2,3,4,2,4,0,5,3]
Row8: [4,6,4,4,3,4,3,2,0,5]
Row9: [2,1,3,4,4,6,4,5,3,0]
Now k=9 (final). Use D after k=8.
We need D[i][9] (col9) and D[9][j] (row9). Row9 currently: [2,1,3,4,4,6,4,5,3,0]
Now D[i][9] values (col9):
i=0:3
i=1:6
i=2:1
i=3:3
i=4:6
i=5:5
i=6:3
i=7:3
i=8:5
i=9:0
Now iterate i.
i=0: D[0][9]=3. Candidate =3 + D[9][j].
D[9][j] row9 as above.
j=0:3+2=5 >0 no.
j=1:3+1=4 =4? D[0][1]=4, equal.
j=2:3+3=6 >3? 6>3 no.
j=3:3+4=7 >5? 7>5 no? D[0][3]=5, 7>5, no.
j=4:3+4=7 >2 no.
j=5:3+6=9 >5? 9>5 no? D[0][5]=5, 9>5, no.
j=6:3+4=7 >2 no.
j=7:3+5=8 >5? 8>5 no? D[0][7]=5, 8>5 no.
j=8:3+3=6 >3? 6>3 no.
j=9:3+0=3 =3.
No changes.
i=1: D[1][9]=6. Candidate =6 + D[9][j].
j=0:6+2=8 >4? no.
j=1:6+1=7 >0 no.
j=2:6+3=9 >5? 9>5 no.
j=3:6+4=10 >3? 10>3 no.
j=4:6+4=10 >5? 10>5 no.
j=5:6+6=12 >5? 12>5 no.
j=6:6+4=10 >3? no.
j=7:6+5=11 >4? 11>4 no.
j=8:6+3=9 >2? no.
j=9:6+0=6 =6.
No changes.
i=2: D[2][9]=1. Candidate =1 + D[9][j].
j=0:1+2=3 <5? D[2][0]=5, candidate 3 <5, update D[2][0]=3.
j=1:1+1=2 <7? D[2][1]=7, update D[2][1]=2.
j=2:1+3=4 >0 no.
j=3:1+4=5 =5? D[2][3]=5, equal.
j=4:1+4=5 >2? 5>2 no.
j=5:1+6=7 >4? 7>4? D[2][5]=4, candidate 7>4, no.
j=6:1+4=5 >3? 5>3 no.
j=7:1+5=6 >2? 6>2 no.
j=8:1+3=4 <7? D[2][8]=7, candidate 4 <7, update D[2][8]=4.
j=9:1+0=1 =1.
Thus row2: col0=3, col1=2, col8=4.
i=3: D[3][9]=3. Candidate =3 + D[9][j].
j=0:3+2=5 >3? 5>3? D[3][0]=3, candidate 5 >3, no.
j=1:3+1=4 <6? D[3][1]=6, candidate 4 <6, update D[3][1]=4.
j=2:3+3=6 >2? 6>2 no.
j=3:3+4=7 >0 no.
j=4:3+4=7 >2? 7>2 no.
j=5:3+6=9 >4? 9>4 no.
j=6:3+4=7 >5? 7>5? D[3][6]=5, candidate 7 >5, no.
j=7:3+5=8 >4? 8>4 no.
j=8:3+3=6 >2? 6>2 no.
j=9:3+0=3 =3.
Thus row3 col1 becomes 4.
i=4: D[4][9]=6. Candidate =6 + D[9][j].
j=0:6+2=8 >3? no.
j=1:6+1=7 >5? 7>5? D[4][1]=5, candidate 7 >5, no.
j=2:6+3=9 >6? 9>6 no? D[4][2]=6, candidate 9>6, no.
j=3:6+4=10 >4? 10>4 no.
j=4:6+4=10 >0 no.
j=5:6+6=12 >6? 12>6 no.
j=6:6+4=10 >5? 10>5 no.
j=7:6+5=11 >7? 11>7 no.
j=8:6+3=9 >6? 9>6 no.
j=9:6+0=6 =6.
No changes.
i=5: D[5][9]=5. Candidate =5 + D[9][j].
j=0:5+2=7 >5? 7>5 no? D[5][0]=5, candidate 7 >5, no.
j=1:5+1=6 <7? D[5][1]=7, candidate 6 <7, update D[5][1]=6.
j=2:5+3=8 >4? 8>4 no.
j=3:5+4=9 >2? 9>2 no.
j=4:5+4=9 >2 no.
j=5:5+6=11 >0 no.
j=6:5+4=9 >4? 9>4 no.
j=7:5+5=10 >6? 10>6 no.
j=8:5+3=8 >4? 8>4 no.
j=9:5+0=5 =5.
Thus row5 col1 becomes 6.
i=6: D[6][9]=3. Candidate =3 + D[9][j].
j=0:3+2=5 =5? D[6][0]=5, equal.
j=1:3+1=4 =4? D[6][1]=4, equal.
j=2:3+3=6 >5? 6>5 no? D[6][2]=5, candidate 6>5, no.
j=3:3+4=7 >3? 7>3 no.
j=4:3+4=7 >2 no.
j=5:3+6=9 >3? 9>3 no.
j=6:3+4=7 >0 no.
j=7:3+5=8 >5? 8>5 no.
j=8:3+3=6 >4? 6>4 no.
j=9:3+0=3 =3.
No changes.
i=7: D[7][9]=3. Candidate =3 + D[9][j].
j=0:3+2=5 >3? no.
j=1:3+1=4 <6? D[7][1]=6, candidate 4 <6, update D[7][1]=4.
j=2:3+3=6 >2? 6>2 no.
j=3:3+4=7 >3? 7>3 no.
j=4:3+4=7 >4? 7>4 no? D[7][4]=4, candidate 7 >4, no.
j=5:3+6=9 >2? 9>2 no.
j=6:3+4=7 >4? 7>4 no? D[7][6]=4, 7>4, no.
j=7:3+5=8 >0 no.
j=8:3+3=6 >5? 6>5? D[7][8]=5, candidate 6 >5, no.
j=9:3+0=3 =3.
Thus row7 col1 becomes 4.
i=8: D[8][9]=5. Candidate =5 + D[9][j].
j=0:5+2=7 >4? 7>4 no.
j=1:5+1=6 =6? D[8][1]=6, equal.
j=2:5+3=8 >4? 8>4 no? D[8][2]=4, 8>4, no.
j=3:5+4=9 >4? 9>4 no.
j=4:5+4=9 >3? 9>3 no.
j=5:5+6=11 >4? 11>4 no.
j=6:5+3=8 >3? 8>3 no.
j=7:5+5=10 >2? 10>2 no.
j=8:5+0=5 >0? 5>0, but not less. Actually D[8][8]=0, candidate 5>0, no.
j=9:5+0=5 =5? D[8][9]=5, equal.
No changes.
i=9: D[9][9]=0, candidate = row9, no changes.
After k=9, final D (all-pairs shortest paths) includes these updates.
Now we have final D (after k=9). We need dist[i][1] (column 1) for each i.
Let's compile final rows with all updates.
We'll gather all rows after final iteration (k=9). We had D after k=8, then applied k=9 updates. So final D:
Row0: unchanged from D8: [0,4,3,5,2,5,2,5,3,3]
Row1: from D8: [4,0,5,3,5,5,3,4,2,6] (col7=4) unchanged by k=9.
Row2: from D8: [5,7,0,5,2,4,3,2,7,1] then k=9 updates: col0=3, col1=2, col8=4. Also we had col3=5, col5=4, etc. So row2 becomes: [3,2,0,5,2,4,3,2,4,1] (check: col0=3, col1=2, col2=0, col3=5, col4=2, col5=4, col6=3, col7=2, col8=4, col9=1).
Row3: from D8: [3,6,2,0,2,4,5,4,2,3] then k=9 update: col1=4. So row3: [3,4,2,0,2,4,5,4,2,3].
Row4: unchanged: [3,5,6,4,0,6,5,7,6,6]
Row5: from D8: [5,7,4,2,2,0,4,6,4,5] then k=9 update: col1=6. So row5: [5,6,4,2,2,0,4,6,4,5].
Row6: unchanged: [5,4,5,3,2,3,0,5,4,3]
Row7: from D8: [3,6,2,3,4,2,4,0,5,3] then k=9 update: col1=4. So row7: [3,4,2,3,4,2,4,0,5,3].
Row8: unchanged: [4,6,4,4,3,4,3,2,0,5]
Row9: unchanged: [2,1,3,4,4,6,4,5,3,0]
Now we have final distances to 1, i.e., column 1 values:
dist[0][1] = row0 col1 = 4
dist[1][1] = 0
dist[2][1] = row2 col1 = 2
dist[3][1] = row3 col1 = 4
dist[4][1] = row4 col1 = 5
dist[5][1] = row5 col1 = 6
dist[6][1] = row6 col1 = 4
dist[7][1] = row7 col1 = 4
dist[8][1] = row8 col1 = 6
dist[9][1] = row9 col1 = 1
Now compute sum for digits in example 3: digits list: 3,5,2,6,1, 2,5,3,2,1, 6,9,2,5,6. But wait grid:
Row1: 3 5 2 6 1
Row2: 2 5 3 2 1
Row3: 6 9 2 5 6
So digits (excluding 1's):
3,5,2,6, 2,5,3,2, 6,9,2,5,6. That's 13 digits as earlier.
Now costs:
3 -> 4
5 -> 6
2 -> 2
6 -> 4
2 -> 2
5 -> 6
3 -> 4
2 -> 2
6 -> 4
9 -> 1
2 -> 2
5 -> 6
6 -> 4
Sum = 4+6+2+4 +2+6+4+2+4 +1+2+6+4 = compute: group: (4+6+2+4)=16; (2+6+4+2+4)=18? Let's do stepwise:
List:
1: 4
2: 6 -> total 10
3: 2 -> 12
4: 4 -> 16
5: 2 -> 18
6: 6 -> 24
7: 4 -> 28
8: 2 -> 30
9: 4 -> 34
10: 1 -> 35
11: 2 -> 37
12: 6 -> 43
13: 4 -> 47
Yes 47 matches output. Good.
Thus algorithm is correct.
Now we just need to implement.
Implementation plan:
- Read H, W.
- Initialize cost[10][10] matrix.
- For i in 0..9: read 10 integers and assign to cost[i][j].
- Compute shortest path distances dist[10][10] using Floyd-Warshall: initialize dist = cost. Then for k in 0..9: for i in 0..9: for j in 0..9: if dist[i][j] > dist[i][k] + dist[k][j]: update.
- Then read grid lines: H lines each with W integers.
- For each cell value a (if a != -1): add dist[a][1] to answer.
- Print answer.
Edge Cases: The cost matrix includes c[i][j]=0 for i=j. Also c[i][j] up to 1000. So distances will fit in int? H,W up to 200 => at most 40000 digits, each distance max maybe 91000? Actually shortest path can be at most 91000=9000 if we go through at most 9 edges. So sum up to 40000*9000 = 360,000,000 fits in 32-bit int? 3.6e8 fits within 2^31-1 (~2.1e9). But safe to use 64-bit (long long). Use int64_t to avoid overflow.
Thus answer can be long long.
Complexities fine.
Let's think about potential pitfalls: Input might have spaces and newlines. Just use standard reading.
Also note that there is at least one digit, so answer >=0.
Now code.
But wait, we need to consider if it's allowed to change a digit multiple times. The problem statement: "Rewriting a digit i with j costs c_{i,j} MP." It doesn't explicitly say you can perform multiple rewrites, but it's implied that you can do any number of operations. Indeed, the example uses a path through 8->4->9->1, so it's allowed. So we are correct.
Thus solution is straightforward.
One more nuance: Could there be an advantage to changing a digit to something else, then later changing another digit that originally was something else, and maybe using a "batch" rewrite? No, each rewrite is per digit.
Thus answer is sum of individual minimal costs.
So implement.
Now, for completeness, we can also use Dijkstra from each node to compute distances to 1, but Floyd is simpler.
Thus final answer.
Now I'll produce final message with solution explanation and code.
I'll include explanation:
- The problem is to minimize total cost to transform each existing digit to 1.
- Since each digit can be rewritten multiple times, the minimal cost to turn digit d into 1 equals the shortest path distance from d to 1 in the directed graph where vertices are digits 0..9 and edge cost from i to j is c_{i,j}.
- Compute all-pairs shortest paths using Floyd-Warshall (10 nodes).
- Then sum over all grid cells with a digit: dist[digit][1].
That's it.
Now provide code in Python (since AtCoder accepts Python). But for this analysis, we need to output solution. I'll produce Python code.
But careful: The input reading: first line H W. Then 10 lines of cost matrix. Then H lines each with W integers.
Implement:
def solve():
import sys
input = sys.stdin.readline
H,W = map(int,input().split())
cost = [list(map(int,input().split())) for _ in range(10)]
# Floyd-Warshall
for k in range(10):
for i in range(10):
ci = cost[i]
cik = ci[k]
if i==k: continue # maybe skip?
for j in range(10):
if ci[j] > cik + cost[k][j]:
ci[j] = cik + cost[k][j]
ans = 0
for _ in range(H):
row = list(map(int,input().split()))
for a in row:
if a != -1:
ans += cost[a][1]
print(ans)
But careful: Floyd modifies the cost matrix in place; but it's fine because we no longer need original.
But typical 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]).
We'll use nested loops.
Alternatively, we can use list of lists.
Now check: In Python, updating while iterating, but it's fine.
One optimization: we could just compute distance to 1 by running Dijkstra 10 times, but it's small.
Alternatively, we can compute only distances to 1 by running Floyd but we need all pairs for potential indirect paths? Actually to compute distance from each node to 1, we can run Bellman-Ford from target 1 on reversed graph? But Floyd is simpler.
But note that the graph is directed; shortest path from i to 1 is not necessarily the same as from 1 to i reversed, because costs may be asymmetric. So we need directed distances from i to 1.
But Floyd gives all pairs. So it's fine.
Now code.
Edge Cases: The cost matrix includes c[i][i]=0. That's given.
Testing with examples:
Example 1: H=2 W=4, cost matrix as above, grid rows: -1 -1 -1 -1 and 8 1 1 8. Floyd should produce dist[8][1]=6, sum=12. Outputs 12.
Example 2: all digits 1, sum=0.
Example 3: 47.
Thus solution is correct.
Now produce final answer.
I'll also add comment.
Now produce final output. ```python
import sys
def solve() -> None:
input = sys.stdin.readline
H, W = map(int, input().split())
# cost matrix of size 10 x 10
cost = [list(map(int, input().split())) for _ in range(10)]
# Floyd-Warshall: compute shortest paths between all digit pairs
for k in range(10):
for i in range(10):
if i == k:
continue
cik = cost[i][k]
# inner loop over j
for j in range(10):
# if a better path via k exists, update
if cost[i][j] > cik + cost[k][j]:
cost[i][j] = cik + cost[k][j]
total = 0
for _ in range(H):
row = list(map(int, input().split()))
for a in row:
if a != -1:
total += cost[a][1] # minimal cost to turn a into 1
print(total)
if name == "main":
solve()