using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("3 3");
WillReturn.Add("10 1");
WillReturn.Add("10 10");
WillReturn.Add("1 10");
WillReturn.Add("1 10 1");
WillReturn.Add("1 10 1");
//9
}
else if (InputPattern == "Input2") {
WillReturn.Add("7 11");
WillReturn.Add("42 77 94 76 40 66 43 28 66 23");
WillReturn.Add("27 34 41 31 83 13 64 69 81 82");
WillReturn.Add("23 81 0 22 39 51 4 37 84 43");
WillReturn.Add("62 37 82 86 26 67 45 78 85 2");
WillReturn.Add("79 18 72 62 68 84 69 88 19 48");
WillReturn.Add("0 27 21 51 71 13 87 45 39 11");
WillReturn.Add("74 57 32 0 97 41 87 96 17 98");
WillReturn.Add("69 58 76 32 51 16 38 68 86 82 64");
WillReturn.Add("53 47 33 7 51 75 43 14 96 86 70");
WillReturn.Add("80 58 12 76 94 50 59 2 1 54 25");
WillReturn.Add("14 14 62 28 12 43 15 70 65 44 41");
WillReturn.Add("56 50 50 54 53 34 16 3 2 59 88");
WillReturn.Add("27 85 50 79 48 86 27 81 78 78 64");
//498
}
else if (InputPattern == "Input3") {
WillReturn.Add("4 4");
WillReturn.Add("0 0 0");
WillReturn.Add("0 0 0");
WillReturn.Add("0 0 0");
WillReturn.Add("0 0 0");
WillReturn.Add("0 0 0 0");
WillReturn.Add("0 0 0 0");
WillReturn.Add("0 0 0 0");
//0
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
struct EdgeInfoDef
{
internal int ToX;
internal int ToY;
internal int Cost;
internal int Kind;
}
static Dictionary<int, List<EdgeInfoDef>> mEdgeInfoListDict = new Dictionary<int, List<EdgeInfoDef>>();
static int UB_X;
static int UB_Y;
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
int R = wkArr[0];
int C = wkArr[1];
UB_X = C - 1;
UB_Y = R - 1;
int[,] AArr = CreateBanArr(InputList.Skip(1).Take(R));
int[,] BArr = CreateBanArr(InputList.Skip(1 + R));
for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
int Hash = GetHash(LoopX, LoopY);
mEdgeInfoListDict[Hash] = new List<EdgeInfoDef>();
if (LoopX < UB_X) {
EdgeInfoDef WillAdd;
WillAdd.ToX = LoopX + 1;
WillAdd.ToY = LoopY;
WillAdd.Cost = AArr[LoopX, LoopY];
WillAdd.Kind = 1;
mEdgeInfoListDict[Hash].Add(WillAdd);
}
if (0 < LoopX) {
EdgeInfoDef WillAdd;
WillAdd.ToX = LoopX - 1;
WillAdd.ToY = LoopY;
WillAdd.Cost = AArr[LoopX - 1, LoopY];
WillAdd.Kind = 1;
mEdgeInfoListDict[Hash].Add(WillAdd);
}
if (LoopY < UB_Y) {
EdgeInfoDef WillAdd;
WillAdd.ToX = LoopX;
WillAdd.ToY = LoopY + 1;
WillAdd.Cost = BArr[LoopX, LoopY];
WillAdd.Kind = 1;
mEdgeInfoListDict[Hash].Add(WillAdd);
}
if (0 < LoopY) {
EdgeInfoDef WillAdd;
WillAdd.ToX = LoopX;
WillAdd.ToY = LoopY - 1;
WillAdd.Cost = 1;
WillAdd.Kind = 2;
mEdgeInfoListDict[Hash].Add(WillAdd);
}
}
}
Dijkstra();
}
static int GetHash(int pX, int pY)
{
return pX * 1000 + pY;
}
//ダイクストラ法で、各ノードまでの最短距離を求める
static void Dijkstra()
{
var InsPQueue = new PQueue();
// 距離合計[確定ノード]なDict
var KakuteiNodeDict = new Dictionary<int, int>();
KakuteiNodeDict.Add(GetHash(0, 0), 0);
//Enqueue処理
Action<int, int, int> EnqueueAct = (pCurrX, pCurrY, pKind) =>
{
int CurrHash = GetHash(pCurrX, pCurrY);
if (mEdgeInfoListDict.ContainsKey(CurrHash) == false) {
return;
}
foreach (EdgeInfoDef EachEdge in mEdgeInfoListDict[CurrHash]) {
int ToHash = GetHash(EachEdge.ToX, EachEdge.ToY);
// 確定ノードならContinue
if (KakuteiNodeDict.ContainsKey(ToHash)) continue;
int wkSumCost = KakuteiNodeDict[CurrHash] + EachEdge.Cost;
PQueue.PQueueJyoutaiDef WillEnqueue;
WillEnqueue.CurrX = EachEdge.ToX;
WillEnqueue.CurrY = EachEdge.ToY;
WillEnqueue.SumCost = wkSumCost;
if (EachEdge.Kind == 2 && pKind != 2) {
WillEnqueue.SumCost++;
}
WillEnqueue.CurrKind = EachEdge.Kind;
WillEnqueue.SumCost *= 10;
if (EachEdge.Kind != 2) {
WillEnqueue.SumCost++;
}
InsPQueue.Enqueue(WillEnqueue);
}
};
EnqueueAct(0, 0, -1);
int GoalHash = GetHash(UB_X, UB_Y);
while (InsPQueue.IsEmpty() == false) {
PQueue.PQueueJyoutaiDef Dequeued = InsPQueue.Dequeue();
int CurrHash = GetHash(Dequeued.CurrX, Dequeued.CurrY);
Dequeued.SumCost /= 10;
//確定ノードならContinue
if (KakuteiNodeDict.ContainsKey(CurrHash)) continue;
KakuteiNodeDict.Add(CurrHash, Dequeued.SumCost);
// 枝切り
if (CurrHash == GoalHash) break;
EnqueueAct(Dequeued.CurrX, Dequeued.CurrY, Dequeued.CurrKind);
}
Console.WriteLine(KakuteiNodeDict[GoalHash]);
}
////////////////////////////////////////////////////////////////
// IEnumerable<string>をintの2次元配列に設定する
////////////////////////////////////////////////////////////////
static int[,] CreateBanArr(IEnumerable<string> pStrEnum)
{
var StrList = pStrEnum.ToList();
if (StrList.Count == 0) {
return new int[0, 0];
}
int[] IntArr = { };
Action<string> SplitAct = pStr =>
IntArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
SplitAct(StrList[0]);
int UB_X = IntArr.GetUpperBound(0);
int UB_Y = StrList.Count - 1;
int[,] WillReturn = new int[UB_X + 1, UB_Y + 1];
for (int Y = 0; Y <= UB_Y; Y++) {
SplitAct(StrList[Y]);
for (int X = 0; X <= UB_X; X++) {
WillReturn[X, Y] = IntArr[X];
}
}
return WillReturn;
}
}
#region PQueue
// 優先度付きキュー
internal class PQueue
{
internal struct PQueueJyoutaiDef
{
internal int CurrX;
internal int CurrY;
internal int SumCost;
internal int CurrKind;
}
private Dictionary<int, PQueueJyoutaiDef> mHeapDict = new Dictionary<int, PQueueJyoutaiDef>();
internal bool IsEmpty()
{
return mHeapDict.Count == 0;
}
// エンキュー処理
internal void Enqueue(PQueueJyoutaiDef pAddJyoutai)
{
int CurrNode = 1 + mHeapDict.Count;
mHeapDict[CurrNode] = pAddJyoutai;
while (1 < CurrNode && mHeapDict[CurrNode / 2].SumCost > mHeapDict[CurrNode].SumCost) {
PQueueJyoutaiDef Swap = mHeapDict[CurrNode];
mHeapDict[CurrNode] = mHeapDict[CurrNode / 2];
mHeapDict[CurrNode / 2] = Swap;
CurrNode /= 2;
}
}
// デキュー処理
internal PQueueJyoutaiDef Dequeue()
{
PQueueJyoutaiDef TopNode = mHeapDict[1];
int LastNode = mHeapDict.Count;
mHeapDict[1] = mHeapDict[LastNode];
mHeapDict.Remove(LastNode);
MinHeapify(1);
return TopNode;
}
// 根ノードを指定し、根から葉へヒープ構築
private void MinHeapify(int pRootNode)
{
if (mHeapDict.Count <= 1) {
return;
}
int Left = pRootNode * 2;
int Right = pRootNode * 2 + 1;
// 左の子、自分、右の子で値が最小のノードを選ぶ
int Smallest = mHeapDict[pRootNode].SumCost;
int SmallestNode = pRootNode;
if (mHeapDict.ContainsKey(Left) && mHeapDict[Left].SumCost < Smallest) {
Smallest = mHeapDict[Left].SumCost;
SmallestNode = Left;
}
if (mHeapDict.ContainsKey(Right) && mHeapDict[Right].SumCost < Smallest) {
Smallest = mHeapDict[Right].SumCost;
SmallestNode = Right;
}
// 子ノードのほうが大きい場合
if (SmallestNode != pRootNode) {
PQueueJyoutaiDef Swap = mHeapDict[SmallestNode];
mHeapDict[SmallestNode] = mHeapDict[pRootNode];
mHeapDict[pRootNode] = Swap;
// 再帰的に呼び出し
MinHeapify(SmallestNode);
}
}
}
#endregion