lightoj-1047 - Neighbor House(简单的线性dp)

1047 - Neighbor House
PDF (English) Statistics Forum
Time Limit: 0.5 second(s) Memory Limit: 32 MB
The people of Mohammadpur have decided to paint each of their houses red, green, or blue. They‘ve also decided that no two neighboring houses will be painted the same color. The neighbors of house i are houses i-1 and i+1. The first and last houses are not neighbors.

You will be given the information of houses. Each house will contain three integers "R G B" (quotes for clarity only), where R, G and B are the costs of painting the corresponding house red, green, and blue, respectively. Return the minimal total cost required to perform the work.

Input
Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case begins with a blank line and an integer n (1 ≤ n ≤ 20) denoting the number of houses. Each of the next n lines will contain 3 integers "R G B". These integers will lie in the range [1, 1000].

Output
For each case of input you have to print the case number and the minimal cost.

Sample Input
Output for Sample Input
2

4
13 23 12
77 36 64
44 89 76
31 78 45

3
26 40 83
49 60 57
13 89 99
Case 1: 137
Case 2: 96

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

const int inf = 1e9;
int dp[30][3];
int hourse[30][3];
int T,n,r,g,b;

int main(){

    scanf("%d",&T);
    for(int t=1;t<=T;t++){
        memset(dp,0,sizeof(dp));
        scanf("%d",&n);
        for(int i=0;i<n;i++) scanf("%d%d%d",&hourse[i][0],&hourse[i][1],&hourse[i][2]);

        for(int i=n-1;i>=0;i--){

            dp[i][0] = min(dp[i+1][1],dp[i+1][2]) + hourse[i][0];
            dp[i][1] = min(dp[i+1][0],dp[i+1][2]) + hourse[i][1];
            dp[i][2] = min(dp[i+1][0],dp[i+1][1]) + hourse[i][2];

        }
        printf("Case %d: %d\n",t,min(dp[0][0],min(dp[0][1],dp[0][2])));
    }

    return 0;
}

时间: 2024-10-05 23:37:43

lightoj-1047 - Neighbor House(简单的线性dp)的相关文章

lightOJ 1047 Neighbor House (DP)

题目链接:lightOJ 1047 Neighbor House 题意:有N做房子,每个房子涂3种颜色各有一个花费,相邻的房子颜色不能一样,给N个房子涂颜色,问完成这个任务的最小花费. dp[i][j] 表示涂到第i个房子涂j颜色的最小花费. 状态转移方程:dp[i][k]=min(dp[i][k],dp[i-1][j]+p[i].c[k]); AC代码: #include <stdio.h> #include <string.h> #include <algorithm&g

LightOJ 1047 Neighbor House (DP 数字三角形变形)

1047 - Neighbor House PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB The people of Mohammadpur have decided to paint each oftheir houses red, green, or blue. They've also decided that no two neighboringhouses will be pai

LightOJ 1047 - Neighbor House 【DP】

题目链接: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

POJ 1163 The Triangle (简单线性dp)

OJ题目 : click here~~ 题目分析:给一个数字三角形,从最上面一个数字开始,方向只能往左下或者右下,一直到最后一行,求经过的所有数字和的最大值. 搞清楚在输入的数据中,route的方向就行. AC_CODE int num[102][102]; int main(){ int n , i , j , k ; while(cin >> n){ int x[102][102]; for(i = 1;i <= n;i++) for(j = 1;j <= i;j++) sca

Neighbor House LightOJ - 1047

Neighbor House LightOJ - 1047 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int T,TT,n; 6 int a[22][22],ans[22][22]; 7 int main() 8 { 9 int i,j,k; 10 scanf("%d",&T); 11 for(TT=1;TT<

uva 11584 Partitioning by Palindromes 线性dp

// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串的数目 // // f[i] = min(f[i],f[j-1] + 1(j到i是回文串)) // // 这道题还是挺简单的,继续练 #include <algorithm> #include <bitset> #include <cassert> #include <

线性DP POJ2279 Mr.Young&#39;s Picture Permutations

Mr. Young's Picture Permutations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1128   Accepted: 562 Description Mr. Young wishes to take a picture of his class. The students will stand in rows with each row no longer than the row behin

LA 4256 Salesmen 线性dp

// LA 4256 Salesmen 线性dp // // 像LCS和LIS问题类似,因为每次修改一个值,都是根据 // 前一个值决定的,那么最后一个结尾的数字肯定要作为 // 状态,而长度作为状态是一目了然的 // // d[i][j]表示长度为i,最后以j结尾的数组修改的最小次数 // // 则状态转移方程为 // // d[i][j] = min(d[i][j],d[i-1][k]+(j,k是否相同或者相邻?0:1)); // // 个人感觉还是比较明显的,最后的答案就是min(d[L]

lightoj 1201 - A Perfect Murder(树形dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1201 题解:简单的树形dp,dp[0][i]表示以i为根结点不傻i的最多有多少dp[0][i]+=max(dp[0][j],dp[1][i]),dp[1][i]表示i傻的最多有多少dp[1][i]+=dp[0][j]. 注意这些点不一定是全联通的. #include <iostream> #include <cstring> #include <cstdi