本文主要是介绍LightOJ 1047 - Neighbor House(dp),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接:LightOJ 1047 - Neighbor House
代码
#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;
const int maxn = 30;
const int inf = 0x3f3f3f3f;int N, dp[maxn][3];int main () {int cas;scanf("%d", &cas);for (int kcas = 1; kcas <= cas; kcas++) {scanf("%d", &N);for (int i = 1; i <= N; i++) {for (int j = 0; j < 3; j++)scanf("%d", &dp[i][j]);}for (int i = 2; i <= N; i++) {for (int j = 0; j < 3; j++) {int s = inf;for (int k = 0; k < 3; k++) if (j != k)s = min(s, dp[i-1][k]);dp[i][j] += s;}}int ans = inf;for (int i = 0; i < 3; i++)ans = min (ans, dp[N][i]);printf("Case %d: %d\n", kcas, ans);}return 0;
}
这篇关于LightOJ 1047 - Neighbor House(dp)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!