Codeforces Round #230 (Div. 2) C. Blocked Points D. Tower of Hanoi

Tower of Hanoi
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 is2n - 1, where n 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 n disks are on the first rod. Moving a disk from rod i to rod j (1 ≤ i, j ≤ 3) costs tij 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 t and an integer n. You need to count the minimal cost of solving SmallY's puzzle, consisting of ndisks.

Input

Each of the first three lines contains three integers — matrix t. The j-th integer in the i-th line is tij (1 ≤ tij ≤ 10000; i ≠ j). The following line contains a single integer n (1 ≤ n ≤ 40) — the number of disks.

It is guaranteed that for all i (1 ≤ i ≤ 3)tii = 0.

Output

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

Sample test(s)
input
0 1 1
1 0 1
1 1 0
3
output
7
input
0 2 2
1 0 100
1 2 0
3
output
19
input
0 2 1
1 0 100
1 2 0
5
output
87

dp题,有两个状态转移

dp[n][a][c]=dp[n-1][a][b]+cost[a][c]+dp[n-1][b][c]

dp[n][a][c]=dp[n-1][a][c]+cost[a][b]+dp[n-1][c][a]+cost[b][c]+dp[n-1][a][c]

两者取最优


#include <iostream>
using namespace std;

typedef long long LL;
const int INF = 0x7fffffff;
LL cost[4][4];
LL dp[50][4][4];

LL dfs(LL n,int a,int c)
{
    if(dp[n][a][c]!=INF) return dp[n][a][c];
    if(n==0) return 0;
    int b=6-a-c;
    LL t1 = dfs(n-1,a,b)+cost[a][c]+dfs(n-1,b,c);
    LL t2 = 2*dfs(n-1,a,c)+cost[a][b]+dfs(n-1,c,a)+cost[b][c];
    dp[n][a][c]=t1>t2?t2:t1;
    return dp[n][a][c];
}
int main()
{
    LL n;
    for(int i=1; i<=3; i++)
        for(int j=1; j<=3; j++)
            cin >> cost[i][j];
    cin >> n;
    for(int i=0; i<=n; i++)
        for(int j=1; j<=3; j++)
            for(int k=1; k<=3; k++)
                dp[i][j][k]=INF;

    cout << dfs(n,1,3)<<endl;
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值