题目链接: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]

  1. //#pragma comment(linker, "/STACK:102400000, 102400000")
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <cstdio>
  7. #include <vector>
  8. #include <cmath>
  9. #include <ctime>
  10. #include <list>
  11. #include <set>
  12. #include <map>
  13. using namespace std;
  14. typedef __int64 LL;
  15. typedef pair <int, int> P;
  16. const int N = 1e2 + ;
  17. LL dp[N][N][N], a[N][N], val[N], INF = 1e16;
  18. //dp[i][k][j] i棵树 k完美值 j颜色
  19.  
  20. int main()
  21. {
  22. LL n, k, m;
  23. scanf("%lld %lld %lld", &n, &m, &k);
  24. for(LL i = ; i <= n; ++i)
  25. scanf("%lld", val + i);
  26. for(LL i = ; i <= n; ++i) {
  27. for(LL j = ; j <= m; ++j) {
  28. scanf("%lld", &a[i][j]);
  29. }
  30. }
  31. for(int i = ; i <= n; ++i) {
  32. for(int j = ; j <= k; ++j) {
  33. for(int x = ; x <= m; ++x) {
  34. dp[i][j][x] = INF;
  35. }
  36. }
  37. }
  38. if(val[]) { //已有颜色
  39. dp[][][val[]] = ;
  40. } else {
  41. for(LL i = ; i <= m; ++i) {
  42. dp[][][i] = a[][i];
  43. }
  44. }
  45. for(LL i = ; i <= n; ++i) {
  46. for(LL j = ; j <= k; ++j) {
  47. for(LL x = ; x <= m; ++x) {
  48. if(dp[i - ][j][x] == INF)
  49. continue;
  50. if(val[i]) {
  51. if(val[i] == x) { //与前一个颜色一致
  52. dp[i][j][val[i]] = min(dp[i - ][j][x], dp[i][j][val[i]]);
  53. } else {
  54. dp[i][j + ][val[i]] = min(dp[i - ][j][x], dp[i][j + ][val[i]]);
  55. }
  56. } else {
  57. for(LL y = ; y <= m; ++y) {
  58. if(y == x) {
  59. dp[i][j][y] = min(dp[i][j][y], dp[i - ][j][x] + a[i][y]);
  60. } else {
  61. dp[i][j + ][y] = min(dp[i][j + ][y], dp[i - ][j][x] + a[i][y]);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. LL res = INF;
  69. for(LL i = ; i <= m; ++i) {
  70. if(dp[n][k][i] == -)
  71. continue;
  72. res = min(res, dp[n][k][i]);
  73. }
  74. printf("%lld\n", res == (LL)INF ? -: res);
  75. return ;
  76. }

Codeforces 711 C. Coloring Trees (dp)的更多相关文章

  1. CodeForces #369 C. Coloring Trees DP

    题目链接:C. Coloring Trees 题意:给出n棵树的颜色,有些树被染了,有些没有.现在让你把没被染色的树染色.使得beauty = k.问,最少使用的颜料是多少.   K:连续的颜色为一组 ...

  2. codeforces 711C C. Coloring Trees(dp)

    题目链接: C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  3. Codeforces 677C. Coloring Trees dp

    C. Coloring Trees time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  4. 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 ...

  5. C. Coloring Trees DP

    传送门:http://codeforces.com/problemset/problem/711/C 题目: C. Coloring Trees time limit per test 2 secon ...

  6. Codeforces 1027E Inverse Coloring 【DP】

    Codeforces 1027E Inverse Coloring 题目链接 #include<bits/stdc++.h> using namespace std; #define N ...

  7. CodeForces 711C Coloring Trees (DP)

    题意:给定n棵树,其中有一些已经涂了颜色,然后让你把没有涂色的树涂色使得所有的树能够恰好分成k组,让你求最少的花费是多少. 析:这是一个DP题,dp[i][j][k]表示第 i 棵树涂第 j 种颜色恰 ...

  8. Codeforces 596D Wilbur and Trees dp (看题解)

    一直在考虑, 每一段的贡献, 没想到这个东西能直接dp..因为所有的h都是一样的. #include<bits/stdc++.h> #define LL long long #define ...

  9. 【Codeforces 711C】Coloring Trees

    [链接] 我是链接,点我呀:) [题意] 连续相同的数字分为一段 你可以改变其中0为1~m中的某个数字(改变成不同数字需要不同花费) 问你最后如果要求分成恰好k段的话,最少需要多少花费 [题解] dp ...

随机推荐

  1. Codeforces 279 B Books

    题意:给出n本书,总的时间t,每本书的阅读时间a[i],必须按照顺序来阅读,问最多能够阅读多少本书 有点像紫书的第七章讲的那个滑动区间貌似 维护一个区间的消耗的时间小于等于t,然后维护一个区间的最大值 ...

  2. 【英语】Bingo口语笔记(3) - 无所谓

    what's in it for me? 这对我有什么好处?

  3. #define XXX do{...}while(0)

    <ol> <li>函数式宏定义的参数没有类型,预处理器只负责做形式上的替换,而不做参数类型检查,所以传参时要格外小心.</li> <li>调用真正函数的 ...

  4. Arduino中的数据类型范围

    注意int不是4字节而仅仅是2字节!!! int: -32,768 ~ 32,767 (2字节) long: 4字节 http://www.arduino.cc/en/Reference/Int

  5. 康乐不风流之爱解题的pde灌水王张祖锦

    康乐不风流之爱解题的pde灌水王张祖锦 师弟: 邓洪存 (现在烟台大学任教) 好吧, 我一直想写写康乐园里与我相熟的这几个人, 不如趁此机会开始. 第一批人物为张祖锦.苏延辉.张会春.黄显涛.刘兴兴. ...

  6. struts2异常处理,global-results定义全局结果处理

    <global-results>定义全局结果处理 一般发生异常之后 结果返回errHandler 因为errHandler是由<global-exception-mappings&g ...

  7. DDoS攻防战(三):ip黑白名单防火墙frdev的原理与实现

    在上一篇文章<DDoS攻防战 (二) :CC攻击工具实现与防御理论>中,笔者阐述了一个防御状态机,它可用来抵御来自应用层的DDoS攻击,但是该状态机依赖一个能应对大量条目快速增删的ip黑白 ...

  8. MongoDB Auto-Sharding(自动分片)入门介绍

    MongoDB是10gen团队开发的一款面向文档的NoSQL数据库.最近一年多以来,MongoDB被越来越多的大型网站应用到生产环境中,比较著名的有Foursquare, bit.ly, Source ...

  9. OFBIZ安装

    1. 安装SVN客户端,从Apache OFBiz Source Repository获取OFBIZ下载地址.此处以12.04为例,下载地址为http://svn.apache.org/repos/a ...

  10. <面试经典题>输入框的功能测试点分析

    (废话几句:这个是网上找来的一份模板,高亮部分为自己修改内容,且此面试题很像当年高考的“必考题”性质,触类旁通吧) 1. 输入框UI是否预计了输入内容长度(尽量完整的显示输入内容): 2. 输入框之前 ...