周赛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 ...
随机推荐
- openssl生成RSA格式,并转为pkcs8格式
原文地址:http://www.thinkingquest.net/articles/391.html?utm_source=tuicool 支付宝接口开发相关:openssl 加密工具 支付宝“手机 ...
- centos安装GO
1,下载 go1.5.1.linux-amd64.tar.gz 2,将go解压到/opt,个人喜好罢了[root@localhost ~]# tar -C /opt -xzf ./go1.5.1.li ...
- RabbitMQ-死信(Dead Letter)
对于有异常的消息我们可以有如下做法: 记录下来再ack. nack或者reject,同时将requeue设为false. 在第2条的基础上增加死信(Dead Letter). 上边的第3个做法可以 ...
- NYOJ 491 幸运三角形
幸运三角形 import java.util.Scanner; public class Main{//搜索 static int sum=0,n; public static void main(S ...
- [转] tomcat结合nginx使用小结
相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人未必了解. 说到反向代理,可能很多人都听说,但具体什么是反向代理,很多人估计就不清楚了 ...
- Ubuntu下Qt项目的部署
部署涉及到以下内容: 1. 程序执行文件: 2. 动态链接库: 3. Qt的一些插件(plugins),例如图片插件(imageformats),数据库插件(sqldrivers): 4. 其他资源文 ...
- 模板-->欧几里得算法
如果有相应的OJ题目,欢迎同学们提供相应的链接 相关链接 所有模板的快速链接 简单的测试 None 代码模板 /* * TIME complexity:O(logN) means very fast. ...
- 使用MWC四轴起飞侧翻解决方法
原因如下:1.电机顺序错了,如上图所示,上面蓝色的箭头是机头,绿色的箭头是电机转向,3.10.11.9对应MWC飞控版上的D3,D9,D11,D9,蓝色箭头对应MWC飞控板的箭头 或者传感器的Y轴 以 ...
- Python Socket通信原理
[Python之旅]第五篇(一):Python Socket通信原理 python Socket 通信理论 socket例子 摘要: 只要和网络服务涉及的,就离不开Socket以及Socket编 ...
- 1247 排排站 USACO(查分+hash)
/* 暴力查分 n*n */ #include<cstdio> #include<cstring> #include<iostream> #define maxn ...