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. 网络粘贴---Xcode中可用到的快捷键

    快捷键: 1.StoryBoard技巧 当你想直接在view中选择自己想要的元素时,但是又碍于一个view上叠加的元素太多很难直接选中,那么在这时,你同时按住键盘上的shift和 control键,然 ...

  2. HashMap循环遍历方式及其性能对比(zhuan)

    http://www.trinea.cn/android/hashmap-loop-performance/ ********************************************* ...

  3. mysql 批处理文件--- 创建 用户 以及 导入数据

    在window下,通过批处理文件(.bat),进行开启MYSQL服务,导入数据文件(.sql) 1)新建一个txt文件,写入以下内容 rem 启动mysql56服务  mysql56是我的mysql服 ...

  4. phalcon: crypt-encrypt/decrypt用法

    phalcon:crypt加密与解密 可以在入口文件index.php进行配置,也可以不配置: $di->set('crypt', function(){ $crypt = new \Phalc ...

  5. Unity5 新功能解析--物理渲染与standard shader

    Unity5 新功能解析--物理渲染与standard shader http://blog.csdn.net/leonwei/article/details/48395061 物理渲染是UNITY5 ...

  6. Unity的HTC VIVE SDK研究(手柄按键功能的研究,比较详细)

    http://blog.csdn.net/ystistheking/article/details/51553237 想交流的朋友我们可以微博互粉,我的微博黑石铸造厂厂长 ,缺粉丝啊 .....求粉求 ...

  7. 记一次基于Unity的Profiler性能分析

    A. WaitForTargetFPS: Vsync(垂直同步)功能所,即显示当前帧的CPU等待时间    B. Overhead:       Profiler总体时间-所有单项的记录时间总和.用于 ...

  8. Laravel 分页详解

    Laravel分页很简单,但功能又很强大噢! 首先在控制器的方法中使用paginate(页面显示条数)方法,传入页面显示的条数 然后在模板页面使用方法render()来生成html元素 appends ...

  9. Qt之QSS(黑色炫酷)

    简述 Qt助手中有关于各种部件的QSS详细讲解,资源很丰富,请参考:Qt Style Sheets Examples. 黑色炫酷 - 一款漂亮的QSS风格. 之前博客中分享了很多关于Qt的样式效果,几 ...

  10. 联系 管理 Hibernate4+Spring JPA+SpringMVC+Volecity搭建web应用(三)

    hibernate注解实体类示例 package cn.bdqn.smvc.entity; import java.io.Serializable; import javax.persistence. ...