1047 - Neighbor House
Time Limit: 0.5 second(s) Memory Limit: 32 MB

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

 


 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 #define N 22
 8 #define inf 1<<29
 9 int dp[N][3],a[N][3];
10 
11 int main(){
12     int t,cas=1;
13     cin>>t;
14     while(t--){
15         int n;
16         cin>>n;
17         for(int i=1;i<=n;i++) for(int j=0;j<3;j++) scanf("%d",&a[i][j]);
18         for(int i=0;i<3;i++) dp[1][i]=a[1][i];
19         for(int i=2;i<=n;i++){
20             for(int j=0;j<3;j++){
21                 dp[i][j]=inf;
22                 for(int k=0;k<3;k++){
23                     if(j==k) continue;
24                     dp[i][j]=min(dp[i][j],dp[i-1][k]+a[i][j]);
25                 }
26             }
27         }
28         printf("Case %d: %d\n",cas++,min(dp[n][0],min(dp[n][1],dp[n][2])));
29     }
30     return 0;
31 }

题意:对于n个用户,选择R,G,B三种颜色中的一种涂色房子,第i和i+1,i-1的不相同,给出选某一颜色的花费,使得这n个人的总花费最小。

解决方法:类似数字三角形,只是每次的选择的要求不一样,直接A~~~

一道水到不能再水的DP,练手,直接上代码

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 #define N 22
 8 #define inf 1<<29
 9 int dp[N][3],a[N][3];
10 
11 int main(){
12     int t,cas=1;
13     cin>>t;
14     while(t--){
15         int n;
16         cin>>n;
17         for(int i=1;i<=n;i++) for(int j=0;j<3;j++) scanf("%d",&a[i][j]);
18         for(int i=0;i<3;i++) dp[1][i]=a[1][i];
19         for(int i=2;i<=n;i++){
20             for(int j=0;j<3;j++){
21                 dp[i][j]=inf;
22                 for(int k=0;k<3;k++){
23                     if(j==k) continue;
24                     dp[i][j]=min(dp[i][j],dp[i-1][k]+a[i][j]);
25                 }
26             }
27         }
28         printf("Case %d: %d\n",cas++,min(dp[n][0],min(dp[n][1],dp[n][2])));
29     }
30     return 0;
31 }

 

题意:对于n个用户,选择R,G,B三种颜色中的一种涂色房子,第i和i+1,i-1的不相同,给出选某一颜色的花费,使得这n个人的总花费最小。

posted on 2013-03-26 22:16  SCAU_Xxm  阅读(305)  评论(0)    收藏  举报