lightOJ 1047   Neighbor House (DP)

题目链接: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 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个邻居用红、绿、蓝三种颜色中的一种涂房子,相邻的房子颜色不能一样,每种颜色花费不同,求将N个用户全部涂完的总花费最小是什么。

分析:

动态规划,和数字三角形类似。先求每户人家涂颜色的最小花费,每次涂下一个房子时不能与上一个房子颜色相同

状态转移方程:
      dp[i][j] = min(dp[i - 1][2], dp[i - 1][3]) + p[i][j];
      dp[i][j] = min(dp[i - 1][1], dp[i - 1][3]) + p[i][j];
      dp[i][j] = min(dp[i - 1][2], dp[i - 1][1]) + p[i][j];

代码:

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring> //memset函数头文件
using namespace std; int p[][];
int dp[][]; //第i户人家涂第j种颜色的最小花费 int min(int a,int b)//输出最小值
{
return a>b?b:a;
} int main()
{
int t;
int n;
int m=;
scanf("%d",&t);//t组案例
while(t--)
{
memset(dp,,sizeof(dp));
scanf("%d",&n); //n户人家
for(int i=;i<=n;i++)
for(int j=;j<=;j++)
scanf("%d",&p[i][j]);//第i户人家涂第j种颜色的花费
for(int i=;i<=n;i++)
for(int j=;j<=;j++)
{
if (j == )//涂第一种颜色
dp[i][j] = min(dp[i - ][], dp[i - ][]) + p[i][j];
if (j == )//涂第二种颜色
dp[i][j] = min(dp[i - ][], dp[i - ][]) + p[i][j];
if (j == )//涂第三种颜色
dp[i][j] = min(dp[i - ][], dp[i - ][]) + p[i][j];
}
printf("Case %d: %d\n",m++,min(dp[n][],min(dp[n][],dp[n][])));
}
return ;
}

lightOJ 1047 Neighbor House (DP)的更多相关文章

  1. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  2. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  3. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  4. 初探动态规划(DP)

    学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...

  5. Tour(dp)

    Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...

  6. 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)

    .navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...

  7. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  8. 最长公共子序列长度(dp)

    /// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...

  9. Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)

    Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...

随机推荐

  1. win7系统中桌面图标显示不正常问题

    http://jingyan.baidu.com/article/466506580c9327f549e5f8dc.html 最近笔者在安装软件时,突然出现了桌面图标显示不正常了,一开始还以为是新安装 ...

  2. DNS解析

    大家好,今天51开源给大家介绍一个在配置文件,那就是/etc/resolv.conf.很多网友对此文件的用处不太了解.其实并不复杂,它是DNS客户机配置文件,用于设置DNS服务器的IP地址及DNS域名 ...

  3. D3.js学习记录 - 数据类型【转】【新】

    1.变量 JAVASCRIPT的变量是一种类型宽松的语言.定义变量不用指定数据类型.而且还是动态可变的. var value = 100;value = 99.9999;value = false;v ...

  4. Fastboot的使用简单教程

    大家都知道HTC手机重新启动进入所谓的project模式,就是HBOOT,然后能够进入FASTBOOT界面,在这个界面.我们能够在电脑端能够做非常多事,特别是HBOOT被改动过,假设是ENG S-OF ...

  5. [每日一题] OCP1z0-047 :2013-08-22 正则表达式---[^Ale|ax.r$]'

    正确答案:DE 一.Oracle正则表达式的相关知识点 '[^Ale|ax.r$]': ^:匹配行的开始字符 $:匹配行的结束字符 []:方括号表示指定一个匹配列表,该列表匹配列表中显示的任何表达式. ...

  6. nginx与ios实现https双向认证

    服务端配置 nginx关键配置例如以下: listen 443; server_name localhost; ssl on; ssl_certificate /usr/local/opt/nginx ...

  7. android环境下解决java.io.IOException: Malformed ipv6异常的方法

    今天做客户端想服务端提交信息的时候,报出了如标题所显示的方法 方法以及参数如下: 输入的参数为:http://192.168.1.173:8080/Api/petinfo/petinfo?flag=a ...

  8. linux中时间函数

    linux下常用时间类型有四种: time_t . struct   tm. struct  timeval .    struct   timespec 1.time_t   时间函数 time_t ...

  9. 注册表:无法打开 XXX 由于某个错误无法打开该密钥。详细信息:拒绝访问

    错误原因:没有注册表用户权限. 正确添加用户权限的步骤如下:(跟着图片步骤) 右击该项,权限: 选中想要添加为当前所有者的用户后,点击应用.如果没用户显示,可以从“其他用户或组”中添加进来. 权限添加 ...

  10. Laravel5 学习与使用(一)

    2015-07-04 (1)  安装Laravel框架 ① 安装前的准备工作 使用Apache24 + PHP 5.6 + MySQL 开发环境完成PHP网站开发,所以Laravel的安装是建立在以上 ...