zoukankan      html  css  js  c++  java
  • lightOJ 1047 Neighbor House (DP)

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

    代码:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>  //memset函数头文件
     5 using namespace std;
     6 
     7 int p[25][5];
     8 int dp[25][25]; //第i户人家涂第j种颜色的最小花费
     9 
    10 int min(int a,int b)//输出最小值
    11 {
    12     return a>b?b:a;
    13 }
    14 
    15 int main()
    16 {
    17     int t;
    18     int n;
    19     int m=1;
    20     scanf("%d",&t);//t组案例
    21     while(t--)
    22     {
    23         memset(dp,0,sizeof(dp));
    24         scanf("%d",&n);  //n户人家
    25         for(int i=1;i<=n;i++)
    26             for(int j=1;j<=3;j++)
    27                 scanf("%d",&p[i][j]);//第i户人家涂第j种颜色的花费
    28             for(int i=1;i<=n;i++)
    29                 for(int j=1;j<=3;j++)
    30                 {
    31                     if (j == 1)//涂第一种颜色
    32                         dp[i][j] = min(dp[i - 1][2], dp[i - 1][3]) + p[i][j];
    33                     if (j == 2)//涂第二种颜色
    34                         dp[i][j] = min(dp[i - 1][1], dp[i - 1][3]) + p[i][j];
    35                     if (j == 3)//涂第三种颜色
    36                         dp[i][j] = min(dp[i - 1][2], dp[i - 1][1]) + p[i][j];
    37                 }
    38                 printf("Case %d: %d
    ",m++,min(dp[n][1],min(dp[n][2],dp[n][3])));
    39     }
    40     return 0;
    41 }
  • 相关阅读:
    js --- for in 和 for of
    vue -- config index.js 配置文件详解
    vue -- 脚手架之webpack.dev.conf.js
    vue --- 解读vue的中webpack.base.config.js
    vue 引入第三方字体包
    js call 和 apply
    vue2.0中的$router 和 $route的区别
    懒加载js实现和优化
    vue的指令在webstrom下报错
    BFC的布局规则和触发条件
  • 原文地址:https://www.cnblogs.com/ttmj865/p/4735075.html
Copyright © 2011-2022 走看看