Codeforces Round #230 (Div. 1) B. Tower of Hanoi

B. 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,有两种转移,

1. dp[x][a][c]=dp[x-1][a][b]+cost[a][c]+dfs[x-1][b][c];

2. dp[x][a][c]=dp[x-1][a][c]+cost[a][b]+dp[x-1][c][a]+cost[b][c]+dp[x-1][a][c];

很好理解,不用解释了。


感想:

这场cf打的特不在状态,第一题读了很久,题意还读错了,后来又读,想到暴力思路了还不敢下手写,怕TLE,写了特判还错了,WA了一次,以为是精度问题还瞎改了一会。。。 = =  第二题也是瞎想,最后想到思路了,好简单的样子,但没时间了。。。跪了,好吧,这不是借口,实力太菜,没办法,又掉到1700以下了。。。加油吧,快点能稳定在1700吧。


代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 205
#define MAXN 1005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

ll n,m,k,ans,cnt,tot,flag;
ll cost[4][4],dp[45][4][4];

ll dfs(ll x,ll a,ll c)
{
    if(x==0) return 0;
    if(dp[x][a][c]!=m) return dp[x][a][c];
    ll i,j,t,b,best=m;
    b=6-a-c;
    t=dfs(x-1,a,b)+cost[a][c]+dfs(x-1,b,c);
    best=min(best,t);
    t=2*dfs(x-1,a,c)+cost[a][b]+dfs(x-1,c,a)+cost[b][c];
    best=min(best,t);
    dp[x][a][c]=best;
    return best;
}
int main()
{
    ll i,j,t;
    m=1LL<<60;
        for(i=1;i<=3;i++)
        {
            for(j=1;j<=3;j++)
            {
                scanf("%I64d",&cost[i][j]);
            }
        }
        scanf("%I64d",&n);
        for(i=1;i<=n;i++) for(j=1;j<=3;j++) for(int k=1;k<=3;k++) dp[i][j][k]=m;
        ans=dfs(n,1,3);
        printf("%I64d\n",ans);
    return 0;
}



你可能感兴趣的:(dp)