Neighbor House LightOJ - 1047

Neighbor House LightOJ - 1047

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

原文地址:https://www.cnblogs.com/hehe54321/p/loj-1047.html

时间: 2024-10-13 03:14:06

Neighbor House LightOJ - 1047的相关文章

lightOJ 1047 Neighbor House (DP)

题目链接:lightOJ 1047 Neighbor House 题意:有N做房子,每个房子涂3种颜色各有一个花费,相邻的房子颜色不能一样,给N个房子涂颜色,问完成这个任务的最小花费. dp[i][j] 表示涂到第i个房子涂j颜色的最小花费. 状态转移方程:dp[i][k]=min(dp[i][k],dp[i-1][j]+p[i].c[k]); AC代码: #include <stdio.h> #include <string.h> #include <algorithm&g

LightOJ 1047 Neighbor House (DP 数字三角形变形)

1047 - Neighbor House PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB The people of Mohammadpur have decided to paint each oftheir houses red, green, or blue. They've also decided that no two neighboringhouses will be pai

LightOJ 1047 - Neighbor House 【DP】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1047 题意:求(p[i][j])上下相邻的 j 不能相同的数塔的最小和. 解法:看代码! 代码: #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <bitset> #include <math

周赛C题 LightOJ 1047 (DP)

C - C Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu 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

lightoj-1047 - Neighbor House(简单的线性dp)

1047 - Neighbor House PDF (English) Statistics ForumTime Limit: 0.5 second(s) Memory Limit: 32 MBThe 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 pai

LightOJ 1030 Discovering Gold【概率】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题意:基础概率题. 代码: #include <stdio.h> #include <string.h> #include <vector> #include <string> #include <algorithm> #include <iostream> #include <iterator>

LightOJ - 1370 Bi-shoe and Phi-shoe

题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1370 题目大意:给N个数a[i], N <= 1e6,问使 Φ(x) >= a[i] 成立的最小x的所有x的和是多少. 解题思路:我们知道的是对于素数 m 来说,phi[m] = m - 1.另一方面,对于一个合数 m 来说, phi[m] < phi[x] , x > m && x 是素数. 因此,我们可以认为,每

lightoj 1057 - Collecting Gold(状压dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1057 题解:看似有点下记忆话搜索但是由于他是能走8个方向的也就是说两点的距离其实就是最大的x轴或y轴的差.然后只有15个藏金点状压一下加dfs就行了. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define inf 0X3f3f3f

Lightoj 1088 - Points in Segments 【二分】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1088 题意: 有一维的n个点和q条线段.询问每条线段上的点有多少个: 思路:寻找这些点中对于每条线段的上下界即可. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include