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

题解:这个题我一开始想的是每次都找最小的的那一个(除了第一个),并且颜色不相同,这时候开个标记数组,把每次可以涂得选一个较小的,这种解法有很大的问题,既然你考虑了第一个可能不是最小的那后边也可能不是最小的相加就是和最小的,因为你所致的最小是去除不能标记点的最小。而不是单纯每次求最小,和就是最小,是受前边影响的,那我们要求的是和最小,我们就可以每次找和最小的,这样最后找出来肯定是最小的。

附上两次的代码:

错误代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; int main()
{
int n;
int a[1005][3];
int mark[1005][3];
scanf("%d",&n);
for(int t=0;t<n;t++)
{
memset(mark,0,sizeof(mark));
int m;
scanf("%d",&m);
for(int j=0;j<m;j++)
{
for(int k=0;k<3;k++)
{
scanf("%d",&a[j][k]);
}
}
long long sum1=a[0][0];
memset(mark,0,sizeof(mark));
mark[0][0]=1;
int temp1; for(int j=1;j<m;j++)
{
int minn=9999;
for(int k=0;k<3;k++)
{
if(!mark[j-1][k])
{
if(minn>a[j][k])
{
minn=a[j][k];
temp1=k;
}
} }
sum1+=minn;
mark[j][temp1]=1; }
long long sum2=a[0][1];
memset(mark,0,sizeof(mark));
mark[0][1]=1;
int temp2; for(int j=1;j<m;j++)
{
int minn=9999;
for(int k=0;k<3;k++)
{
if(!mark[j-1][k])
{
if(minn>a[j][k])
{
minn=a[j][k];
temp2=k;
}
} }
sum2+=minn;
mark[j][temp2]=1; }
long long sum3=a[0][2];
memset(mark,0,sizeof(mark));
mark[0][2]=1;
int temp3; for(int j=1;j<m;j++)
{
int minn=9999;
for(int k=0;k<3;k++)
{
if(!mark[j-1][k])
{
if(minn>a[j][k])
{
minn=a[j][k];
temp3=k;
}
} }
sum3+=minn;
mark[j][temp3]=1; }
if(min(sum1,sum2)>sum3)
{
printf("Case %d: %lld\n",t+1,sum3);
}
else
{
printf("Case %d: %lld\n",t+1,min(sum1,sum2));
}
} return 0;
}

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream> using namespace std; int main()
{
int a[1005][3];
int sum[1005][3];
int n;
scanf("%d",&n);
for(int t=1;t<=n;t++)
{
int m;
scanf("%d",&m);
for(int j=0;j<m;j++)
{
for(int k=0;k<3;k++)
{
scanf("%d",&a[j][k]);
}
}
memset(sum,0,sizeof(sum));
sum[0][0]=a[0][0];
sum[0][1]=a[0][1];
sum[0][2]=a[0][2];
for(int j=1;j<m;j++)
{
sum[j][0]=min(sum[j-1][1],sum[j-1][2])+a[j][0];
sum[j][1]=min(sum[j-1][0],sum[j-1][2])+a[j][1];
sum[j][2]=min(sum[j-1][0],sum[j-1][1])+a[j][2];
}
printf("Case %d: %d\n",t,(min(sum[m-1][0],sum[m-1][1])<sum[m-1][2])?min(sum[m-1][0],sum[m-1][1]):sum[m-1][2]);
} return 0;
}

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 1114 Easily Readable 字典树

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

  3. lightOJ 1047 Neighbor House (DP)

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. ngx-bootstrap使用04 carousel组件

    1 carousel 是一个通过循环播放图片.文本的幻灯片:就像一个旋转旋转木马一样,但是不支持嵌套使用 2 如何使用 2.1 搭建ngx-bootstrap使用环境 参见博文:点击前往 2.2 在模 ...

  2. rest-framework组件 之 渲染器与版本

    浏览目录 渲染器 版本 渲染器 规定页面显示的效果(无用,了解即可). 局部渲染 只返回json数据. 效果: 看另一种情况: 既返回json数据,又嵌套在html中.注意:容易出bug. 效果如下: ...

  3. Mybatis避免出现语法错

    在使用MyBatis的时候,可能会看起来没有问题,但是代码运行的时候出现意想不到的错误. 看如下代码: <update id="updateByPrimaryKeySelective& ...

  4. dreamweaver cs5 快捷键

    撤销上一步:ctrl + Z: 回复上一步:ctrl + Y: 代码缩进:左下角(应用原格式)

  5. wordcount小程序

    wordcount小程序 (1)github网址 https://github.com/yuyuyu960818/count_txt_file (2)PSP表 PSP2.1 PSP阶段 预估耗时 (分 ...

  6. autoconf手册(一)

    Autoconf Creating Automatic Configuration Scripts Edition 2.13, for Autoconf version 2.13 December 1 ...

  7. Joda Time 使用

    Joda Time 使用 对于系统的一些时间操作很是不方便,为了方便转化,有时候用date,有时候用timestmp,有时候用calendar,忍不住想更改了. 但是任务巨大,先把笔记收藏了,后面有机 ...

  8. RGB565的转换

    RGB色彩模式也就是“红绿蓝”模式是一种颜色标准,是通过对红(R).绿(G).蓝(B)三种颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的,RGB即是代表红.绿.蓝三个通道的颜色,这个标准几 ...

  9. [C# 线程处理系列]专题四:线程同步

    目录: 一.线程同步概述 二.线程同步的使用 三 .总结 一.线程同步概述 前面的文章都是讲创建多线程来实现让我们能够更好的响应应用程序,然而当我们创建了多个线程时,就存在多个线程同时访问一个共享的资 ...

  10. c# 大文件分割 复制 Filestream 进度条

    大文件分割复制,每次复制100M 也可以复制别的较大数值. 小于1G的小文件就直接复制得了.代码里没写 ,但是很简单 直接写进去就好了,难得是分割复制 所以没写. 好吧 我还是改了 改成小文件也可以复 ...