CF392B.Tower of Hanoi

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The Tower of Hanoi is a well-known mathematical puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.

The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:

  1. Only one disk can be moved at a time.
  2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
  3. No disk may be placed on top of a smaller disk.

With three disks, the puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2n12^{n}-1 , where nn is the number of disks. (c) Wikipedia.

SmallY's puzzle is very similar to the famous Tower of Hanoi. In the Tower of Hanoi puzzle you need to solve a puzzle in minimum number of moves, in SmallY's puzzle each move costs some money and you need to solve the same puzzle but for minimal cost. At the beginning of SmallY's puzzle all nn disks are on the first rod. Moving a disk from rod ii to rod jj (1<=i,j<=3)(1<=i,j<=3) costs tijt_{ij} units of money. The goal of the puzzle is to move all the disks to the third rod.

In the problem you are given matrix tt and an integer nn . You need to count the minimal cost of solving SmallY's puzzle, consisting of nn disks.

输入格式

Each of the first three lines contains three integers — matrix tt . The jj -th integer in the ii -th line is tijt_{ij} ( 1<=tij<=10000; ij1<=t_{ij}<=10000; i≠j ). The following line contains a single integer nn (1<=n<=40)(1<=n<=40) — the number of disks.

It is guaranteed that for all ii (1<=i<=3)(1<=i<=3) , tii=0t_{ii}=0 .

输出格式

Print a single integer — the minimum cost of solving SmallY's puzzle.

输入输出样例

  • 输入#1

    0 1 1
    1 0 1
    1 1 0
    3
    

    输出#1

    7
    
  • 输入#2

    0 2 2
    1 0 100
    1 2 0
    3
    

    输出#2

    19
    
  • 输入#3

    0 2 1
    1 0 100
    1 2 0
    5
    

    输出#3

    87
    
首页