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

题目:

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户人,打算把他们的房子图上颜色,有red、green、blue三种颜色,每家人涂不同的

颜色要花不同的费用,而且相邻两户人家之间的颜色要不同,求最小的总花费费用。

分析:

这个跟数字三角形问题很像,这个是求最小的值,而且有些限制,每次走到点要和上次走的点不在同一列。

动态转移方程:

dp[i][j]=a[i][j]+min(dp[i+1][j-1],dp[i+1][j+1])

 #include<iostream>
#include<cstring>
using namespace std;
int a[][];
int dp[][];
int min(int a,int b)
{
if(a<b) return a;
else return b;
}
int main()
{
int t,n,i,c=,j;
cin>>t;
while(t--)
{
c++;
cin>>n;
memset(dp,,sizeof(dp));
for(i=;i<=n;i++)
for(j=;j<=;j++)
cin>>a[i][j];
for(i=;i<=;i++) dp[n][i]=a[n][i];
for(i=n-;i>=;i--)
for(j=;j<=;j++)
{
if(j==)
dp[i][j]=a[i][j]+min(dp[i+][],dp[i+][]); //不能与上一步相同
if(j==)
dp[i][j]=a[i][j]+min(dp[i+][],dp[i+][]);
if(j==)
dp[i][j]=a[i][j]+min(dp[i+][],dp[i+][]);
}
if(dp[][]<dp[][]) //比较出最大值
dp[][]=dp[][];
if(dp[][]<dp[][])
dp[][]=dp[][];
cout<<"Case "<<c<<": "<<dp[][]<<endl;
}
return ;
}

light oj 1047 - Neighbor House 动态规划的更多相关文章

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

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

  2. lightOJ 1047 Neighbor House (DP)

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

  3. Light OJ 1114 Easily Readable 字典树

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

  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. memcache(使用php操作memcache)

    .概念 memcache 是一个高效的分布式的内存对象缓存系统,他可以支持把php的各种数据(数组,对象,基本数据类型)放在它管理的内存中 . 安装步骤 1.下载php_memcache.dll文件并 ...

  2. hdu 4274 2012长春赛区网络赛 树形dp ***

    设定每个节点的上限和下限,之后向上更新,判断是否出现矛盾 #include<cstdio> #include<iostream> #include<algorithm&g ...

  3. C++Primer快速浏览笔记-复合类型

    C++Primer2.3节介绍了两种复合类型:引用和指针 1.引用 引用并非对象,它只是为一个已经存在的对象所起的别名. 一旦初始化完成,引用将和它的初始值对象一直绑定在一起,不能重新绑定到另一个对象 ...

  4. linux中socket的理解

    对linux中socket的理解 一.socket 一般来说socket有一个别名也叫做套接字. socket起源于Unix,都可以用“打开open –> 读写write/read –> ...

  5. 第十一篇:SOUI系统资源管理

    SOUI资源管理模块 从前篇已经讲到在SOUI中所有资源文件通过一个uires.idx文件进行索引. 这里将介绍在程序中如何引用这些资源文件. 在SOUI系统中,资源文件通过一个统一的接口对象读取: ...

  6. HorizontalScrollView

    HorizontalScrollView 链接

  7. Lucene.net 全文检索 盘古分词

    lucene.net + 盘古分词 引用: 1.Lucene.Net.dll 2.PanGu.Lucene.Analyzer.dll 3.PanGu.HighLight.dll 4.PanGu.dll ...

  8. 编译器 expected unqualified-id before numeric constant 错误

    今天调试代码,碰到expected unqualified-id before numeric constant 错误,代码的错误模块出现在一个函数模块上, 奇怪的是这个函数模块之前编译了很多次,也没 ...

  9. python web编程 创建一个web服务器

    这里就介绍几个底层的用于创建web服务器的模块,其中最为主要的就是BaseHTTPServer,很多框架和web服务器就是在他们的基础上创建的 基础知识 要建立一个Web 服务,一个基本的服务器和一个 ...

  10. 函数式编程语言LISP,python,haskell,clojure

    说说我自己的背景吧,我是个半吊子的程序员,做任何事情喜欢比较了解然后再尝试,我接触过很多语言,大多数都把它当成工具来使用 我现在的工作大部分主要在于数据挖掘与机器学习方面,也学习web开发,我第一个拿 ...