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 ...
随机推荐
- sql DROP 和DELETE、TRUNCATE用法
DROP:删除数据库已存在的表DROP TABLE tbname DELETE:删除记录delete from tbname truncate:清空表,重置索引truncate table tbnam ...
- 自己实现字符串操作函数strlen(),strcat(),strcpy(),strcmp()
1.strlen()函数是求解字符串的有效长度的 1)非递归实现 size_t my_strlen(const char *str) { assert(str != NULL); //断言,保证指针 ...
- Oracle安装错误ora-00922(zhuan)
Oracle安装错误ora-00922(缺少或无效选项) (2012-03-19 10:49:27) 转载▼ 标签: 杂谈 安装Oracle 11g R2的过程中,在新建数据库实例时出现了该错误, ...
- BZOJ 1452 Count
长知识啦..二维BIT. #include<iostream> #include<cstdio> #include<cstring> #include<alg ...
- open行情
日k线 只能取8年 1分钟K线 只能取五天 前复权K线出现负数的股价 后复权K线会出现上千的股价
- Windows Store APP- C# to get IP Address
using Windows.Networking.Connectivity; public String GetIPString() { String ipString = String.Empty; ...
- RabbitMQ链接不上异常
链接代码 项目启动报的异常 本地main方法链接报的异常 网上查询原因 问题说明及解决方案: 网上原因很多,最终原因都是连接不到数据库造成的. 1.查看防火墙 2.tomcat端口是否屏蔽 3.查看连 ...
- Android Studio 小技巧合集
本文翻译自 Android Studio Tips by Philippe Breault,一共收集了62个 Android Studio 使用小技巧和快捷键. 根据这些小技巧的使用场景,本文将这62 ...
- android 横竖屏限制如何配置
在开发android的应用中,有时候需要限制横竖屏切换.只需要在AndroidManifest.xml文件中加入android:screenOrientation属性限制. ndroid:screen ...
- IPC_共享内存
在IPC(InterProcess Communication)的通信模式下,不管是使用消息队列还是共享内存,甚至是信号量,每个IPC的对象(object)都有唯一的名字,称为“键”(key).通过“ ...