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 ...
随机推荐
- 51nod1476 括号序列的最小代价
这题应该可以用费用流写吧?不过我想不出贪心来TAT.其实还是单调队列乱搞啊T_T //ÍøÉϵÄ̰ÐÄËã·¨ºÃÉñ°¡¡£¡£¡£ÎÒÖ»»áÓÃ×îС·ÑÓÃ×î´óÁ÷ÅÜTAT #in ...
- ios中get,post和解压缩用法
一. 网络概念 1. 在Linux系统上,运行的Web服务器的名字叫做Apache 2. 所有的http访问都是基于html或者相关的文件,例如:php,asp,jsp,asp.net 这些文件最终都 ...
- HDU 5339 Untitled (暴力枚举)
题意:给定一个序列,要求从这个序列中挑出k个数字,使得n%a1%a2%a3....=0(顺序随你意).求k的最小值. 思路:排个序,从大的数开始模起,这是因为小的模完还能模大的么? 每个元素可以选,也 ...
- 物联网操作系统HelloX已成功移植到MinnowBoard MAX开发板上
在HelloX开发团队的努力下,以及Winzent Tech公司(总部在瑞典斯德哥尔摩)的支持下,HelloX最新版本V1.78已成功移植到MinnowBoard MAX开发板上.相关源代码已经发布到 ...
- (六)6.9 Neurons Networks softmax regression
SoftMax回归模型,是logistic回归在多分类问题的推广,即现在logistic回归数据中的标签y不止有0-1两个值,而是可以取k个值,softmax回归对诸如MNIST手写识别库等分类很有用 ...
- source 命令
作用: 当我修改了/etc/profile文件,我想让它立刻生效,而不用重新登录:这时就想到用source命令,如:source /etc/profile 介绍:source命令也称为“点命令”,也就 ...
- android 带边框的圆角按钮
新建buttonstyle.xml 代码如下 <?xml version="1.0" encoding="UTF-8"?> <layer-li ...
- SQL0668N 由于表 "db2inst1.test" 上的原因代码 "3",所以不允许操作(解因为LOAD引起的LOAD暂挂状态锁)
DB2解因为LOAD引起的LOAD暂挂状态锁 一般解锁命名是,SET INTEGRITY FOR temp_test IMMEDIATE CHECKED 但是load暂挂状态是解不了的,可以l ...
- Why automate?为什么要自动化?
The need for speed is practically the mantra of the information age. Because technology is now being ...
- list 容器 排序函数.xml
pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...