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 ...
随机推荐
- 51nod1711 平均数
二分答案.check有多少个区间的平均数>xbi=ai-x;将sm离散化.然后logn求出有多少个小于sm[i].类似于求逆序对的思路. 一直WA一个点...所以我就下载数据特判了TAT #in ...
- 51nod1175 区间中第K大的数
裸的主席树. #include<cstdio> #include<cstring> #include<cctype> #include<algorithm&g ...
- Book 最短路算法
用HDU2544整理一下最近学的最短路算法 1.Dijkstra算法 原理:集合S表示已经找到最短路径的点,d[]表示当前各点到源点的距离 初始时,集合里面只有源点,当每个点u进入集合S时,用d[u] ...
- shockwave flash has crashed(Flash 插件崩溃导致页面中的flash不显示)怎么办
1.原理: 应该电脑里最近装了chorme或者基于chorme内核的浏览器.越来越多的人开始使用chrome的浏览器,很多用户都遇到过flash崩溃的问题,有时候重启chrome可以解决,有时候会导致 ...
- find命令下的atime,ctime,mtime
Linux下的find命令在目录结构中搜索文件,并执行指定的操作.linux下的find命令提供了相当多的查找条件,功能很强大,由于find的功能很强大,所以他的选项也很多,今天我们来细说一下find ...
- 【转】 ARM Linux 3.x的设备树(Device Tree)
1. ARM Device Tree起源 http://blog.csdn.net/21cnbao/article/details/8457546 Linus Torvalds在2011年3月1 ...
- [Everyday Mathematics]20150228
试证: $$\bex \int_0^\infty \sin\sex{x^3+\frac{\pi}{4}}\rd x =\frac{\sqrt{6}+\sqrt{2}}{4}\int_0^\infty ...
- VB6.0编程笔记——(2)开发环境准备&学习前导入
工欲善其事必先利其器,着手开始学习写代码之前,我们需要先准备好需要用到的工具.这篇文章会教大家部署好环境,同时会告知前期我们需要知道的一点内容(可以不用特别理解,只要记住用法就行,后续会深入展开介绍) ...
- LoadRunner error -27257
检查点函数 web_reg_find("Search=body", "savecount=num", "Text=test1", LAST) ...
- Selenium RC配置
Selenium RC: -----作者: 虫师 ========================================================================== ...