zoukankan      html  css  js  c++  java
  • Neighbor House LightOJ

    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

    2

    4

    13 23 12

    77 36 64

    44 89 76

    31 78 45

    3

    26 40 83

    49 60 57

    13 89 99

    Sample Output

    Case 1: 137

    Case 2: 96

    与数字三角形很相似的题,数据量也小。

     1 #define INF 1e8
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 int n;
     9 int map[25][4],dp[25][4];
    10 
    11 int solve(){
    12     dp[1][1]=map[1][1],dp[1][2]=map[1][2],dp[1][3]=map[1][3];
    13     for(int i=2;i<=n;i++){
    14         dp[i][1]=min(dp[i-1][2],dp[i-1][3])+map[i][1];
    15         dp[i][2]=min(dp[i-1][1],dp[i-1][3])+map[i][2];
    16         dp[i][3]=min(dp[i-1][1],dp[i-1][2])+map[i][3];
    17     }
    18     int ans=INF;
    19     for(int i=1;i<=3;i++) ans=min(ans,dp[n][i]);
    20     return ans; 
    21 }
    22 
    23 
    24 int main()
    25 {   int kase;
    26     cin>>kase;
    27     for(int t=1;t<=kase;t++){
    28         cin>>n;
    29         for(int i=1;i<=n;i++) for(int j=1;j<=3;j++) scanf("%d",&map[i][j]);    
    30         printf("Case %d: %d
    ",t,solve());
    31     }
    32     return 0;
    33 } 
  • 相关阅读:
    .net 调用命令行进行解压缩
    《jQuery实战》第1章 引荐JQuery
    将指定URL文件下载到指定服务器
    Oracle学习笔记
    Spring3 AOP的使用
    Field类使用以及getDeclaredFields方法
    spring 3的使用
    Json数据类型
    RotateAnimation详解
    GsonJava对象生成Json串
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7337701.html
Copyright © 2011-2022 走看看