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


题意:

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

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

思路:

这个题与刘汝佳的算法竞赛与入门经典中的数字三角形有点类似,能够參照其方法。利用动态规划的思想。找出

状态转移方程,dp[i][j%3+1] = a[i][j%3+1] + min(dp[i-1][(j-1)%3+1], dp[i-1][(j+1)%3+1],这样答案就出来了。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int dp[23][4];
int a[23][4];
int main()
{
int n, t;
scanf("%d",&t);
for(int k = 1; k <= t; k++)
{
scanf("%d",&n);
memset(a, 0, sizeof(a));
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= 3; j++)
scanf("%d",&a[i][j]);
}
for (int i = 1; i <= n; i++)
{
for (int j = 3; j < 6; j++)
{
dp[i][j%3+1] = a[i][j%3+1] + min(dp[i-1][(j-1)%3+1], dp[i-1][(j+1)%3+1]); }
}
printf("Case %d: %d\n", k, min(dp[n][1], min(dp[n][2], dp[n][3])));
}
return 0;
}


light oj1074的更多相关文章

  1. CSharpGL(13)用GLSL实现点光源(point light)和平行光源(directional light)的漫反射(diffuse reflection)

    CSharpGL(13)用GLSL实现点光源(point light)和平行光源(directional light)的漫反射(diffuse reflection) 2016-08-13 由于CSh ...

  2. 【MVVM Light】Messager的使用

    一.前言       在MVVM编程的模式中,有时候我们会遇到一个很尴尬的情况: 若干个xaml.cs都复用一个ViewModel,当ViewModel想传递一个特定的消息给某一个xaml.cs的时候 ...

  3. bzoj4691: Let There Be Light

    如果原点能被一个光源照到,那么这两个点之间一定没有任何球.我们可以通过三分距离来确定某线段和球是否有交点. 注意到m非常小,于是我们可以枚举原点被哪些光源照到.由于\(O(2^{n}*m)\)会超时, ...

  4. 【MVVM Light】新手初识MVVM,你一看就会

    一.前言 作为一个初入软件业的新手,各种设计模式与框架对我是眼花缭乱的.所以当我接触到这些新知识的时候就希望自己能总结几个步骤,以便更好更方便的在日常工作中进行使用. MVVM顾名思义就是Model- ...

  5. Sensor(LIGHT)

    package com.example.sensor01; import java.util.List; import android.hardware.Sensor; import android. ...

  6. sourcesafe.light 开源项目启动

    sourcesafe.light 源于一个2D独立砖块沙盒游戏. 在这个游戏的设计中碰到了一个瓶颈:这个游戏想把玩家变成一个个neo,在矩阵世界中没有什么不可以修改. 这个游戏要跨平台,玩家的修改操作 ...

  7. C#Light 和 uLua的对比第二弹

    上次的对比大家还有印象否,C#Light和ulua对比各有胜负 今天我们加入一个去反射优化,这是uLua没办法实现的优化,我们也就只能不要脸的胜之不武了 以原生执行同一测试时间为X1,数字越小的越快 ...

  8. C#Light/Evil合体啦

    决定将C#Light和C#Evil合并成一个项目,毕竟C#Evil包含C#Light所有的功能,分开两个,基本的表达式方面有什么bug还得两头改 暂时就C#Light/Evil这么叫吧,庆祝合体,画了 ...

  9. C#最良心脚本语言C#Light/Evil,Xamarin\WP8\Unity热更新最良心方案,再次进化.

    C#Light的定位是嵌入式脚本语言,一段C#Light脚本是一个函数 C#Evil定位为书写项目的脚本语言,多脚本文件合作,可以完全用脚本承载项目. C#Light/Evil 使用完全C#一致性语法 ...

随机推荐

  1. Jquery 可拖拽的Ztree

    比较懒,就只贴关键代码吧,自己把有用的属性全部打印出来了,也加了不少注释. 保存后涉及到的排序问题,刷新问题还未考虑到,后面有的话再加. $.fn.zTree.init($("#ztree& ...

  2. PHP常用的一些函数:

    背景:这一次是对一些函数进行整理,方便以后的使用. 1.date(); date()函数的作用是获取当前日期时间,由于PHP 5.0对date()函数进行了重写,因此,当前的日期时间函数比系统时间少了 ...

  3. JavaScript 封装插件学习笔记(一)

    此篇只是笔记,在借鉴.参考.模仿的过程,可能不完整,请多指教! 定义插件名称要注意命名冲突,防止全局污染. 1.第一种Javascript对象命名:(Javascript语言是“先解析,后运行”,解析 ...

  4. [Android]有关外部链接唤醒App需要注意的坑

    移动互联网发展到今天,一个移动app需要和各种各样的外部链接关联,它不再仅仅从手机的桌面启动,更多的将会从其他的应用.浏览器链接.短信.二维码或者微信分享等渠道启动,这里涉及到的是各种各样的营销渠道和 ...

  5. Windows2008 Server 常规设置及基本安全策略

    一.系统及程序 1.屏幕保护与电源 桌面右键--〉个性化--〉屏幕保护程序屏幕保护程序 选择无更改电源设置 选择高性能选择关闭显示器的时间 关闭显示器 选 从不 保存修改 2.安装IIS 管理工具-- ...

  6. HDU_2212_水

    DFS Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  7. java主要集合类的数据结构

    1).ArrayList  ArrayList维护着一个对象数组.如果调用new ArrayList()后,它会默认初始一个size=10的数组.  每次add操作都要检查数组容量,如果不够,重新 ...

  8. 让System.Drawing.Bitmap可以在linux运行

    .net core的bitmap使用的是以下类库,但无法在linux运行 https://github.com/CoreCompat/CoreCompat 在linux运行需要安装runtime.li ...

  9. ngFor 循环带索引

    *ngFor="let item of userList,let i = index"   或者 *ngFor="let item of userList index a ...

  10. 怎么用最短时间高效而踏实地学习Python?

    之所以写这篇文章,在标题里已经表达得很清楚了.做技术的人都知道,时间就是金钱不是一句空话,同一个技术,你比别人早学会半年,那你就能比别人多拿半年的钱.所以有时候别人去培训我也不怎么拦着,为什么?因为培 ...