LightOJ 1047 - Neighbor House (dp)
来源:互联网 发布:数据库实用教程答案 编辑:程序博客网 时间:2024/05/18 06:14
题意:
n<=20个房子涂3色,每个房子涂不同颜色代价不用,相邻房子颜色不能相同,求涂完的最小代价
分析:
煞笔题,dp[i][j]:=前i个房子涂完,且第i个房子颜色为j的最小代价
ans=min{dp[n][i],i∈[0,3]}
代码:
//// Created by TaoSama on 2015-11-13// Copyright (c) 2015 TaoSama. All rights reserved.////#pragma comment(linker, "/STACK:1024000000,1024000000")#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;#define pr(x) cout << #x << " = " << x << " "#define prln(x) cout << #x << " = " << x << endlconst int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;int n, a[25][3], dp[25][3];int main() {#ifdef LOCAL freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);#endif ios_base::sync_with_stdio(0); int t; scanf("%d", &t); int kase = 0; while(t--) { scanf("%d", &n); for(int i = 1; i <= n; ++i) for(int j = 0; j < 3; ++j) scanf("%d", &a[i][j]); memset(dp, 0x3f, sizeof dp); int ans = INF; for(int i = 0; i < 3; ++i) dp[1][i] = a[1][i]; for(int i = 2; i <= n; ++i) { for(int j = 0; j < 3; ++j) { for(int k = 0; k < 3; ++k) { if(j == k) continue; dp[i][j] = min(dp[i][j], dp[i - 1][k] + a[i][j]); } if(i == n) ans = min(ans, dp[i][j]); } } printf("Case %d: %d\n", ++kase, ans); } return 0;}
0 0
- lightOJ 1047 Neighbor House (DP)
- LightOJ 1047 - Neighbor House(dp)
- LightOJ 1047 - Neighbor House (dp)
- LightOj 1047 Neighbor House(基础dp)
- 【LightOJ】1047 - Neighbor House(dp)
- LightOJ 1047 - Neighbor House 【DP】
- lightoj 1047 Neighbor House(Dp)
- LightOJ - 1047 Neighbor House(DP)
- LightOJ 1047 - Neighbor House(DP)
- lightoj 1047 - Neighbor House DP
- 【 lightoj 1047 - Neighbor House + dp】
- lightoj 1047 - Neighbor House DP(简单线性DP)
- LightOJ 1047 Neighbor House (DP 数字三角形变形)
- LightOJ 1047 Neighbor House (DP 数字三角形变形)
- lightoj-1047-Neighbor House【矩阵DP】
- LightOJ - 1217 Neighbor House (II)(dp)
- LightOJ 1047 - Neighbor House
- LightOJ 1047 - Neighbor House
- CS架构
- c++中的const
- 数据库课堂笔记
- ZOJ-3162-To Go or Not to Go
- H.264官方软件JM源代码简单分析-编码器lencod
- LightOJ 1047 - Neighbor House (dp)
- VS 2015 使用经验
- 第十一周项目1 - 二叉树算法验证(3)中序线索化二叉树的算法验证
- hdu 1247 Hat’s Words
- ping命令使用及返回总结
- 牛客网 | 链表中环的入口结点
- 表达树构造
- java swing 设置文件保存路径
- 深度学习(1) 关于图像卷积和卷积神经网络(CNN)