Codeforces 711 C. Coloring Trees (dp)
题目链接:http://codeforces.com/problemset/problem/711/C
给你n棵树,m种颜色,k是指定最后的完美值。接下来一行n个数 表示1~n树原本的颜色,0的话就是没颜色(一定要上色),非0就是有颜色(不能上色)。
接下来n行 每行m个数,第i行第j个数表示 编号为i的树上第j种颜色的代价为a[i][j]。
问你最后要使完美值为k的上色代价最小为多少,要是不可能的话就为-1。
我们来考虑dp,每个树和前一个树有联系。
dp[i][j][x] 表示第i棵树 完美值为j 上第x种颜色的最小代价。
如果前一个树颜色和此树颜色相同,dp[i - 1][j][x] --> dp[i][j][x]
否则,dp[i - 1][j][y] --> dp[i][j + 1][x]
//#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef __int64 LL;
typedef pair <int, int> P;
const int N = 1e2 + ;
LL dp[N][N][N], a[N][N], val[N], INF = 1e16;
//dp[i][k][j] i棵树 k完美值 j颜色 int main()
{
LL n, k, m;
scanf("%lld %lld %lld", &n, &m, &k);
for(LL i = ; i <= n; ++i)
scanf("%lld", val + i);
for(LL i = ; i <= n; ++i) {
for(LL j = ; j <= m; ++j) {
scanf("%lld", &a[i][j]);
}
}
for(int i = ; i <= n; ++i) {
for(int j = ; j <= k; ++j) {
for(int x = ; x <= m; ++x) {
dp[i][j][x] = INF;
}
}
}
if(val[]) { //已有颜色
dp[][][val[]] = ;
} else {
for(LL i = ; i <= m; ++i) {
dp[][][i] = a[][i];
}
}
for(LL i = ; i <= n; ++i) {
for(LL j = ; j <= k; ++j) {
for(LL x = ; x <= m; ++x) {
if(dp[i - ][j][x] == INF)
continue;
if(val[i]) {
if(val[i] == x) { //与前一个颜色一致
dp[i][j][val[i]] = min(dp[i - ][j][x], dp[i][j][val[i]]);
} else {
dp[i][j + ][val[i]] = min(dp[i - ][j][x], dp[i][j + ][val[i]]);
}
} else {
for(LL y = ; y <= m; ++y) {
if(y == x) {
dp[i][j][y] = min(dp[i][j][y], dp[i - ][j][x] + a[i][y]);
} else {
dp[i][j + ][y] = min(dp[i][j + ][y], dp[i - ][j][x] + a[i][y]);
}
}
}
}
}
}
LL res = INF;
for(LL i = ; i <= m; ++i) {
if(dp[n][k][i] == -)
continue;
res = min(res, dp[n][k][i]);
}
printf("%lld\n", res == (LL)INF ? -: res);
return ;
}
Codeforces 711 C. Coloring Trees (dp)的更多相关文章
- CodeForces #369 C. Coloring Trees DP
题目链接:C. Coloring Trees 题意:给出n棵树的颜色,有些树被染了,有些没有.现在让你把没被染色的树染色.使得beauty = k.问,最少使用的颜料是多少. K:连续的颜色为一组 ...
- codeforces 711C C. Coloring Trees(dp)
题目链接: C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 677C. Coloring Trees dp
C. Coloring Trees time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees DP
C. Coloring Trees ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the pa ...
- C. Coloring Trees DP
传送门:http://codeforces.com/problemset/problem/711/C 题目: C. Coloring Trees time limit per test 2 secon ...
- Codeforces 1027E Inverse Coloring 【DP】
Codeforces 1027E Inverse Coloring 题目链接 #include<bits/stdc++.h> using namespace std; #define N ...
- CodeForces 711C Coloring Trees (DP)
题意:给定n棵树,其中有一些已经涂了颜色,然后让你把没有涂色的树涂色使得所有的树能够恰好分成k组,让你求最少的花费是多少. 析:这是一个DP题,dp[i][j][k]表示第 i 棵树涂第 j 种颜色恰 ...
- Codeforces 596D Wilbur and Trees dp (看题解)
一直在考虑, 每一段的贡献, 没想到这个东西能直接dp..因为所有的h都是一样的. #include<bits/stdc++.h> #define LL long long #define ...
- 【Codeforces 711C】Coloring Trees
[链接] 我是链接,点我呀:) [题意] 连续相同的数字分为一段 你可以改变其中0为1~m中的某个数字(改变成不同数字需要不同花费) 问你最后如果要求分成恰好k段的话,最少需要多少花费 [题解] dp ...
随机推荐
- HDU 1253 (简单三维广搜) 胜利大逃亡
奇葩!这么简单的广搜居然爆内存了,而且一直爆,一直爆,Orz 而且我也优化过了的啊,尼玛还是一直爆! 先把代码贴上睡觉去了,明天再来弄 //#define LOCAL #include <ios ...
- 阿里云linux服务器安装Phalcon-----"phalcon Volt directory can't be written" "gcc: internal compiler error: Killed (program cc1)"
这里特别蛋疼的一件事是官方英文文档和中文文档命令参数略有不同 中文文档: //通用平台下安装指定的软件包: sudo yum install git gcc make pcre-devel php-d ...
- Ubuntu中文输入法的安装
Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等.其中Scim和Ibus是输入法框架. 在Ubuntu的中文系统中自带了中文输入法,通过Ctrl+S ...
- ecshop 设置管理员
<?php define('IN_ECS', true); require(dirname(__FILE__) . '/includes/init.php'); $admin_name=trim ...
- linux下安装虚拟机qemu kqemu
一,为什么要装虚拟机,为什么选择qemu 我的系统里面有3个linux系统,这些系统都是独立的,有的时候,我想一台电脑,能更真实的模拟二台,这个时候我们就可以装个虚拟机.其实如果真的很有钱的话,可能考 ...
- 查看事务锁:innodb_trx+innodb_locks+innodb_lock_waits
当出现:ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction,要解决是一件麻烦的事情:特别是当一个SQL ...
- 剑指offer—第三章高质量的代码(按顺序打印从1到n位十进制数)
题目:输入一个数字n,按照顺序打印出1到最大n位十进制数,比如输入3,则打印出1,2,3直到最大的3位数999为止. 本题陷阱:没有考虑到大数的问题. 本题解题思路:将要打印的数字,看成字符串,不足位 ...
- Java反射基本玩法
三个主要的反射类 Class反射对象描述类语义结构,可以从Class对象中获取构造函数.成员变量.方法类等元素的反射对象,并以编程的方式通过这些反射对象对目标类对象进行操作.这些反射对象类在java. ...
- 配置apache以fastcgi运行php
apache默认是用自带的mod_php模块运行php,现在我们介绍使用fastcgi来执行php脚本.先说下fastcgi的优点: Fastcgi的优点: 从稳定性上看, fastcgi是以独立的进 ...
- mac 配置Python集成开发环境(Eclipse +Python+Pydev)
1.下载Mac版64位的Eclipse. 进入到Eclipse官方网站的下载页面(http://www.eclipse.org/downloads/),我选择了下图所示的软件包, 浏览器在下载过程中使 ...