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;
}