C - C

Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

 

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

Hint

Use simple DP

题解:   有n家人,要把他们的房子涂上颜色,有红、绿、蓝三种颜色,每家涂不同的颜色要花不同的费用,而且相邻两户人家之间的颜色要不同,求最小的总花费费用。

看案例可以从第一行往下模拟,然后就知道了怎么去实现,还是动态规划,求最小的值,有限制条件,每次走到点和上次走的点不在同一列。

代码:

#include<iostream>
#include<cstdio>
using namespace std;
int dp[][];
int a[][];
int main()
{
int T,n,k=;
cin>>T;
while(T--)
{
cin>>n;
for(int i=; i<=n; i++)
for(int j=; j<=; j++)
cin>>a[i][j];
for(int i=; i<=n; i++)
for(int j=; j<=; j++)
{
dp[i][j%+]=min(dp[i-][(j-)%+],dp[i-][(j+)%+])+a[i][j%+]; // 相当于滚筒数组,优化
}
int total=min(dp[n][],min(dp[n][],dp[n][]));
cout<<"Case "<<k++<<": "<<total<<endl;
}
}

周赛C题 LightOJ 1047 (DP)的更多相关文章

  1. lightOJ 1047 Neighbor House (DP)

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

  2. 思维题练习专场-DP篇(附题表)

    转载请注明原文地址http://www.cnblogs.com/LadyLex/p/8536399.html 听说今年省选很可怕?刷题刷题刷题 省选已经结束了但是我们要继续刷题刷题刷题 目标是“有思维 ...

  3. Neighbor House LightOJ - 1047

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

  4. CSDN 轻松周赛赛题:能否被8整除

    轻松周赛赛题:能否被8整除 题目详情 给定一个非负整数,问能否重排它的全部数字,使得重排后的数能被8整除. 输入格式: 多组数据,每组数据是一个非负整数.非负整数的位数不超过10000位. 输出格式 ...

  5. 【刷题笔记】DP优化-斜率优化

    斜率优化,是一种利用斜率的优化(废话) 关于数论:咕咕咕 部分内容参考自学长 如果有这样的一个状态转移方程: \[f[i]=\min\limits_{j=L_j}^{R_j}\{f[j]+val(j, ...

  6. LightOJ 1248 Dice (III) (水题,期望DP)

    题意:给出一个n面的色子,问看到每个面的投掷次数期望是多少. 析:这个题很水啊,就是他解释样例解释的太...我鄙视他,,,,, dp[i] 表示 已经看到 i 面的期望是多少,然后两种选择一种是看到新 ...

  7. lightoj 1036 dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1036 #include <cstdio> #include <cst ...

  8. lightoj 1018 dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1018 #include <cstdio> #include <cst ...

  9. lightoj 1004 dp:数字三角形

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1004 #include <cstdio> #include <cst ...

随机推荐

  1. qemu kvm 虚拟化

    虚拟化: KVM是一个基于Linux内核的虚拟机,属于完全虚拟化.虚拟机监控的实现模型有两类:监控模型(Hypervisor)和宿主机模型(Host-based).由于监控模型需要进行处理器调度,还需 ...

  2. 使用Java Mail发送邮件

    本笔记参考自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17839983 JavaMail是SUN提供给开发人员在应用程序中实现 ...

  3. web.xml基本配置描述

    先加载一段写好的web.xml: <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2. ...

  4. Angular过滤器 自定义及使用方法

    首先设置自定义过滤器. 定义模块名:angular .module('myApp') .filter('filterName',function(){ return function(要过滤的对象,参 ...

  5. 堆排序中建堆过程时间复杂度O(n)怎么来的?

    首先这个循环是从i = headsize/2 -> 1,也就是说这是一个bottom-up的建堆.于是,有1/2的元素向下比较了一次,有1/4的向下比较了两次,1/8的,向下比较了3次,.... ...

  6. Linux下tomcat使用

    http://tomcat.apache.org/download-70.cgi这里下载 放到Linux目录下,解压开, 默认port:8080能够直接使用 经常使用启动命令catalina.sh   ...

  7. 深入理解Android 自定义attr Style styleable以及其应用

    相信每一位从事Android开发的猿都遇到过需要自己去自定义View的需求,如果想通过xml指定一些我们自己需要的参数,就需要自己声明一个styleable,并在里面自己定义一些attr属性,这个过程 ...

  8. php导出excel不断刷新缓冲区的思路(转)

    require('./db.class.php');$DB = new db();$DB->connect();//数据库链接 header("Content-Type: text/c ...

  9. [转] __thread关键字

    http://blog.csdn.net/liuxuejiang158blog/article/details/14100897 __thread是GCC内置的线程局部存储设施,存取效率可以和全局变量 ...

  10. java中的泛型(转)

    什么是泛型? 泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型的一个 ...