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

Hint

Use simple DP

程序分析:此题的题意为有n户人,打算把他们的房子图上颜色,有red、green、blue三种颜色,每家人涂不同的颜色要花不同的费用,而且相邻两户人家之间的颜色要不同,求最小的总花费费用。我们所要使用的方法就是DP,其中有一个小问题值得注意就是开始我用C提交的时候就会提示CE,之后我们C++交的时候就过了,所以有时候选择语言也是很重要的。

程序代码:

#include <stdio.h>
#include <string.h>
#include <algorithm> using namespace std; int dp[][];
int a[][];
int min(int a,int b)
{
if(a>b)
return b;
else return a;
}
int main()
{
int n, t;
scanf("%d",&t);
for(int k = ; k <= t; k++)
{
scanf("%d",&n);
memset(a, , sizeof(a));
memset(dp, , sizeof(dp));
for (int l= ; l <= n; l++)
{
for (int j = ; j <= ; j++)
scanf("%d",&a[l][j]);
}
for (int i = ; i <= n; i++)
{
for (int j = ; j < ; j++)
{
dp[i][j%+] = a[i][j%+] + min(dp[i-][(j-)%+], dp[i-][(j+)%+]);
//这里我用这种方法避免了分情况讨论,、减少了代码量 }
}
printf("Case %d: %d\n", k, min(dp[n][], min(dp[n][], dp[n][])));
}
return ;
}

light oj 1047-neighbor house的更多相关文章

  1. light oj 1047 - Neighbor House 动态规划

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/C 题目: Description The peopl ...

  2. light oj 1047 - Neighbor House(贪心)

    The people of Mohammadpur have decided to paint each of their houses red, green, or blue. They've al ...

  3. Light OJ 1114 Easily Readable 字典树

    题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...

  4. lightOJ 1047 Neighbor House (DP)

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

  5. Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖

    题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...

  6. Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖

    标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...

  7. Light OJ 1316 A Wedding Party 最短路+状态压缩DP

    题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...

  8. light oj 1007 Mathematically Hard (欧拉函数)

    题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...

  9. Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖

    题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...

  10. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

随机推荐

  1. La=LaULb (单链表)

    #include<stdio.h> typedef struct LNode { int data; struct LNode *next; }LNode,*LinkList; void ...

  2. BootStrap学习1 输入提示

    首先参考这个页面http://www.bootcss.com/javascript.html#typeahead 我只是把里面最基本的东西抠出来了 <!DOCTYPE html> < ...

  3. 8,SSO,,eager copy,COW

    针对字符串不同的长度,“编译器”选择不同的优化策略:SSO, eager copy,COW,分别针对短字符串,中等长度字符串,长字符串.不过,现在(2016)的大多数编译器(gcc 4.9.1,vs2 ...

  4. 对程序员的不尊重是中国it产业的悲哀。

    电脑刚进入中国时,“程序员”三个字是一份令人尊敬的岗位,那个时候中国互联网人才奇缺.程序员的价格也就水涨船高.小的时候电视里到处播放着电脑培训学院的招生广告.一说到程序员,给我们的印象都是白领,高薪的 ...

  5. 英文论文中“such as, for example, e.g., i.e., etc., et al. ”的用法分析 (转)

    在英文论文的编辑加工中,常会遇到such as, for example, e. g. , i. e. , etc. 和et al. 的错误及混淆使用.这里,举例分析这几个词的意义,并阐述其正确用法. ...

  6. QT类型转换(九种转换)

    1.char * 与 const char *的转换 char *ch1="hello11";const char *ch2="hello22";ch2 = c ...

  7. 宣布发布 Windows Azure 导入/导出服务的预览版以及 Web 和移动解决方案场景的若干增强功能

    客户评估基于云的存储解决方案时,面临的挑战之一是以经济高效.安全快速的方式从 Blob 存储区移进和移出大量数据.今天,我们很高兴地宣布发布 Windows Azure 导入/导出的预览版,这款新服务 ...

  8. flexjson 的使用

    日期转换             JSONSerializer serializer = new JSONSerializer();         serializer.exclude(new St ...

  9. x0vncserver Fatal server error: no screens found

    I make a connection through SSH and  then I type: # x0vncserver --PasswordFile=/home/hello/.vnc/pass ...

  10. 关于left join 和 inner join

    今天遇到一个逻辑很复杂的SQL,虽然写出来了,但是并没有完全体会,找了找资料,算是摸清楚了left join和inner join 的实际意义. 感谢PCJIM的文章,写的非常明白,原文地址:http ...