light oj1074

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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 08:38:55

light oj1074的相关文章

light oj 1236 【大数分解】

给定一个大数,分解质因数,每个质因子的个数为e1,e2,e3,--em, 则结果为((1+2*e1)*(1+2*e2)--(1+2*em)+1)/2. //light oj 1236 大数分解素因子 #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> #include <ctype.h> #i

编译或运行时可能会出现错误: Error:Error retrieving parent for item: No resource found that matches the given name “Theme.AppCompat.Light”

这个问题我在刚开始写"HelloWorld"时就遇到,以为是API版本太高,下载了常用的API 19.17--一系列的,后来还是有问题.就上网查了很多,遇到几篇不错的文章,记录下来,方便以后查看. No resource found that matches the given name 'Theme.AppCompat.Light 的完美解决方案  http://www.360doc.com/content/15/0316/15/9200790_455576135.shtml And

[2016-04-21][light]OJ[1234][Harmonic Number]

时间:2016-04-21 22:18:26 星期四 题目编号:[2016-04-21][light]OJ[1234][Harmonic Number] 题目大意:求∑nk=11kn∈(1,108),精确到10?8求∑k=1n1kn∈(1,108),精确到10?8 分析: 想法是打表,然后输出,但是直接打表会爆内存 解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值 对应的整百就是n

CSS无需Webfont实现近似「微软雅黑Light」的字体效果

在网页制作中,美观的字体能很大程度上提升一个网页的整体外观浏览效果(说白了就是提升逼格),但受各种因素的影响(例如中文字体应用Webfont的麻 烦),使开发者在制作过程中不得不使用“Web安全字体”,这使网页的外观效果大幅下降.今天在改模板的过程中偶然发现一个很笨的方法,实现近似微软雅黑 Light的字体效果. 实际上「微软雅黑Light」字体就是相对于「微软雅黑」字体较细,但是它的显示效果却要让人觉得舒适很多,只要字体不会太小,显示效果还是不错的. 所以直接用font-weight属性设置字

MVVM Light须要注意的10个问题

MVVM Light须要注意的10个问题 从使用XAML技术基础開始(实际上并非非常久曾经).我便关注MVVM(Model – View – ViewModel)模式.偶然接触到MVVM Light不久后便喜欢上它的工作方式. 不光我包含业余和专业开发者在内的非常多开发者都喜欢这个函数库. 依照开发者意愿,MVVM Light 不是一个框架而是函数库,该函数库注重于探究建立一个MVVM结构而且提供一些额外的帮助类以便于应用. MVVM Light在发展过程中改变了非常多.非常多元素被增加又有非常

Theme.AppCompat.Light无法找到问题(转)

使用adt开发新建一个Android app,选择支持的SDK版本如果小于11(Android3.0)就会报如下错误. error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'. 官网给出的答案是: https://developer.android.com/tools/support-library/setup.html#add-l

解决IDEA下运行android项目 No resource found that matches the given name &#39;Theme.AppCompat.Light&#39;.

1.右击项目选择 解决IDEA下运行android项目 No resource found that matches the given name 'Theme.AppCompat.Light'.

Light 1008

H - 字 Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1008 Description Fibsieve had a fantabulous (yes, it's an actual word) birthday party this year. He had so many gifts that he was actually

哈理工2015 暑假训练赛 zoj 2976 Light Bulbs

Light BulbsTime Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu SubmitStatusPracticeZOJ 2976 Description Wildleopard had fallen in love with his girlfriend for 20 years. He wanted to end the long match for their love and get marri