Lightoj1047——Neighbor House(dp)

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
Output for 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
Case 1: 137
Case 2: 96

要把一排屋子刷成三原色中的一種,要求鄰居不能相同
給出每個房子刷三種顏色的費用,求最小費用。
他們都說很簡單爲什麼我就是不會做呢???

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <map>
#define INF 0x3f3f3f3f
#define MAXN 105
#define Mod 20007
using namespace std;
int dp[1005][5],r[1005],g[1005],b[1005];
int main()
{
    int t;
    scanf("%d",&t);
    for(int cas=1; cas<=t; ++cas)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
            scanf("%d%d%d",&r[i],&g[i],&b[i]);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;++i)
        {
            dp[i][0]=min(dp[i-1][1],dp[i-1][2])+r[i];
            dp[i][1]=min(dp[i-1][0],dp[i-1][2])+g[i];
            dp[i][2]=min(dp[i-1][0],dp[i-1][1])+b[i];
        }
        printf("Case %d: %d\n",cas,min(dp[n][0],min(dp[n][1],dp[n][2])));
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章