题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1047
题意:求(p[i][j])上下相邻的 j 不能相同的数塔的最小和。
解法:看代码!
代码:
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <bitset>
#include <math.h>
#include <ctype.h>
#include <time.h>
#include <queue>
#include <map>
#include <set>
using namespace std;
int t;
int n;
int p[25][5];
int dp[25][25];
int pos[25];
int main()
{
cin >> t;
for (int ca = 1; ca <= t; ca++)
{
memset(dp,0,sizeof(dp));
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= 3; j++)
cin >> p[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= 3; j++)
{
if (j == 1)
dp[i][j] = min(dp[i - 1][2], dp[i - 1][3]) + p[i][j];
if (j == 2)
dp[i][j] = min(dp[i - 1][1], dp[i - 1][3]) + p[i][j];
if (j == 3)
dp[i][j] = min(dp[i - 1][2], dp[i - 1][1]) + p[i][j];
}
cout << "Case " << ca << ": " << min(dp[n][1] ,min(dp[n][2],dp[n][3]))<< endl;
}
return 0;
}
时间: 2024-10-18 06:06:04