Description

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 Bare 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

题目大意:给n座房子刷漆,每座房子可以刷红,绿,蓝色中的任何一种,粉刷每一种颜色都要花费一定的money,但是相邻两座房子的颜色不能相同,

要求输出最少的花费。用i表示1~n座房子,用j表示三种颜色的花费。

分析:先输入1~n的房子中每种颜色所需的花费p[i][j],接着从1~n先比较第一座房子中所需花费最少的并标记其颜色,这其中有三种情况,j=1,2,3,

分别表示第一,二,三种颜色,用一个二维数组dp[i][j]=min(dp[i][j-2],dp[i][j-3])+p[i][j](当j=1时),然后比较第二座房子所需的花费最少,判断其颜色是否与第一座相

同,不同则直接累加,否则比较出第二少的,再累加。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
int n;
int dp[25][5],p[25][5];
int main()
{
int t,i,j,kase=0;
scanf("%d",&t);
while(t--)
{
memset(dp,0,sizeof(dp));
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=3;j++)
scanf("%d",&p[i][j]);
for(i=1;i<=n;i++)
for(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][1],dp[i-1][2])+p[i][j];
}
printf("Case %d: %d\n",++kase,min(dp[n][1],min(dp[n][2],dp[n][3])));
}
return 0;
}

LightOJ 1047-Program C的更多相关文章

  1. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  2. Neighbor House LightOJ - 1047

    Neighbor House LightOJ - 1047 #include<cstdio> #include<cstring> #include<algorithm&g ...

  3. LightOJ 1141 Program E

    Description In this problem, you are given an integer number s. You can transform any integer number ...

  4. 周赛C题 LightOJ 1047 (DP)

    C - C Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu   Description Th ...

  5. hdu 1047 Integer Inquiry

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1047 Integer Inquiry Description One of the first use ...

  6. 解决方案--java执行cmd命令ProcessBuilder--出错Exception in thread "main" java.io.IOException: Cannot run program "dir d:\": CreateProcess error=2(xjl456852原创)

    当我尝试在java中通过ProcessBuilder运行window的cmd命令时出现错误: public static void main(String [] args) throws IOExce ...

  7. LightOj:1030-Discovering Gold(期望dp模板)

    传送门:http://www.lightoj.com/volume_showproblem.php?problem=1030 Discovering Gold Time Limit: 2 second ...

  8. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  9. Solved: “Cannot execute a program. The command being executed was \roslyn\csc.exe”

    When you publish your ASP.NET project to a hosting account such as GoDaddy, you may run into the iss ...

  10. BZOJ 1047 二维单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...

随机推荐

  1. PHP中file_put_contents追加和换行

    在PHP的一些应用中需要写日志或者记录一些信息,这样的话.可以使用fopen(),fwrite()以及 fclose()这些进行操作.也可以简单的使用file_get_contents()和file_ ...

  2. 在linux(CentOS-6.7_x86_64)上安装mysql成功记录

    查看linux服务器的yum源设置: [root@hadoop03 yum.repos.d]# cd /etc/yum.repos.d [root@hadoop03 yum.repos.d]# ll ...

  3. 转!!java线程状态

    一. 线程状态类型1. 新建状态(New):新创建了一个线程对象.2. 就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中,变得可运 ...

  4. CodeIgniter配置之SESSION

    刚使用Codeigniter时也被其中的SESSION迷惑过,后来就再也没用过CI自带的SESSION,想必还是有必要整理一下SESSION.为弄清CI中的SESSION,先来说一下PHP中SESSI ...

  5. 小韦系统装工行网银U盾驱动的方法

    小韦系统装工行网银U盾驱动的方法 拷贝文件.bat @echo 开始注册echo n|copy /-y scarddlg.dll %windir%\system32\echo n|copy /-y w ...

  6. 【转】 Linux下的多线程编程

    作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/原文链接:http://www.cnblogs.com/gnuhpc/archive/2012/12/07/280 ...

  7. 设置westorm自动代码提示

    打开settings 然后在js文件下 打出co  按TAB键就出现了color了

  8. nodeschool.io 8

    ~~ HTTP COLLECT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the ...

  9. (11)odoo权限机制

    -----------------更新时间:10:21 2016-09-29 星期四14:31 2016-09-28 星期三 权限对象命名修改18:06 2016-09-18 星期日11:55 201 ...

  10. Feistel密码结构

    分组密码:是一种加解密方案,将输入的明文分组当作一个整体出来,输出一个等长的密文分组. 典型的分组大小为64位和128位.密钥长度一般为128位.迭代轮数典型值为16轮. Feistel 密码结构是用 ...