周赛C题 LightOJ 1047 (DP)
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)的更多相关文章
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- 思维题练习专场-DP篇(附题表)
转载请注明原文地址http://www.cnblogs.com/LadyLex/p/8536399.html 听说今年省选很可怕?刷题刷题刷题 省选已经结束了但是我们要继续刷题刷题刷题 目标是“有思维 ...
- Neighbor House LightOJ - 1047
Neighbor House LightOJ - 1047 #include<cstdio> #include<cstring> #include<algorithm&g ...
- CSDN 轻松周赛赛题:能否被8整除
轻松周赛赛题:能否被8整除 题目详情 给定一个非负整数,问能否重排它的全部数字,使得重排后的数能被8整除. 输入格式: 多组数据,每组数据是一个非负整数.非负整数的位数不超过10000位. 输出格式 ...
- 【刷题笔记】DP优化-斜率优化
斜率优化,是一种利用斜率的优化(废话) 关于数论:咕咕咕 部分内容参考自学长 如果有这样的一个状态转移方程: \[f[i]=\min\limits_{j=L_j}^{R_j}\{f[j]+val(j, ...
- LightOJ 1248 Dice (III) (水题,期望DP)
题意:给出一个n面的色子,问看到每个面的投掷次数期望是多少. 析:这个题很水啊,就是他解释样例解释的太...我鄙视他,,,,, dp[i] 表示 已经看到 i 面的期望是多少,然后两种选择一种是看到新 ...
- lightoj 1036 dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1036 #include <cstdio> #include <cst ...
- lightoj 1018 dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1018 #include <cstdio> #include <cst ...
- lightoj 1004 dp:数字三角形
题目链接:http://lightoj.com/volume_showproblem.php?problem=1004 #include <cstdio> #include <cst ...
随机推荐
- poj 1458 Common Subsequence【LCS】
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: 17 ...
- Ueditor 1.4.3 jsp utf-8版图片上传问题
- SpringMVC在上传文件的时候提示The current request is not a multipart request错误
@RequestMapping("/insertOrder") @ResponseBody public Object insertOrder(String userId,Htt ...
- Java model 对象处理
public class BaseFlowBo { /** * 流量 */ private String flow; /** * Pv */ private String pv; /** * CTR ...
- Android源码学习
http://android-wheel.googlecode.com/svn/trunk/android-wheel-read-only 在github上面有一个叫做 android-wheel 的 ...
- Qt 学习之路 :Qt 线程相关类
希望上一章有关事件循环的内容还没有把你绕晕.本章将重新回到有关线程的相关内容上面来.在前面的章节我们了解了有关QThread类的简单使用.不过,Qt 提供的有关线程的类可不那么简单,否则的话我们也没必 ...
- linux 屏幕亮度调整命令
我的Fujitsu S7211的笔记本屏幕显示在Ubuntu 10.04不如在Vista下明亮,一直以为是驱动的问题.后来从老外的博客中,发现了这个亮度其实可以调节. 2种方法: 1. 调节屏幕对比度 ...
- vim中选择匹配文本删除技巧
试举几例如下: 如何只保留匹配内容行而删除其他行? :v/pattern/d :help :v 如何对每行只保留匹配内容而删除这一行中的其它内容 :%s/^.pattern.$/\1/g 删除包含特定 ...
- C#HttpWebResponse请求常见的状态码
成员名称 说明 Continue 等效于 HTTP 状态 100.Continue 指示客户端可能继续其请求. SwitchingProtocols 等效于 HTTP 状态 101.Switching ...
- VB 增强的部件与引用
常用部件 对话框 Microsoft Common Dialog Control 6.0 (sp6) COMDLG32.OCX Forms2.0控件 Microsoft Forms 2. ...