【链接】 我是链接,点我呀:)

【题意】

连续相同的数字分为一段
你可以改变其中0为1~m中的某个数字(改变成不同数字需要不同花费)
问你最后如果要求分成恰好k段的话,最少需要多少花费

【题解】

dp[i][j][k]前i棵树,分成j段,第j段最后一棵树颜色为m的最小花费
很好转移了,分情况就好

【代码】

  1. import java.io.*;
  2. import java.util.*;
  3. public class Main {
  4. static InputReader in;
  5. static PrintWriter out;
  6. public static void main(String[] args) throws IOException{
  7. //InputStream ins = new FileInputStream("E:\\rush.txt");
  8. InputStream ins = System.in;
  9. in = new InputReader(ins);
  10. out = new PrintWriter(System.out);
  11. //code start from here
  12. new Task().solve(in, out);
  13. out.close();
  14. }
  15. static int N = 100;
  16. static class Task{
  17. int n,m,k;
  18. long dp[][][] = new long[N+10][N+10][N+10];
  19. int p[][] = new int[N+10][N+10];
  20. int c[] = new int[N+10];
  21. //dp[i][j][k]前i棵树,分成j段,第j段最后一棵树颜色为m的最小花费
  22. public void solve(InputReader in,PrintWriter out) {
  23. n = in.nextInt(); m = in.nextInt();k = in.nextInt();
  24. for (int i = 1;i <= n;i++) c[i] = in.nextInt();
  25. for (int i = 1;i <= n;i++)
  26. for (int j = 1;j <= m;j++)
  27. p[i][j] = in.nextInt();
  28. for (int i = 0;i <=N;i++)
  29. for (int j = 0;j <= N;j++)
  30. for(int k = 0;k <=N;k++)
  31. dp[i][j][k] = -1;
  32. if (c[1]==0) {
  33. for (int j = 1;j <= m;j++) {
  34. dp[1][1][j] = p[1][j];
  35. }
  36. }else {
  37. dp[1][1][c[1]] = 0;
  38. }
  39. for (int i = 1;i < n;i++)
  40. for (int j = 1;j <= k;j++)
  41. for (int l = 1;l <= m;l++) {
  42. if (dp[i][j][l]!=-1) {
  43. if(c[i+1]==0) {
  44. for (int l2=1;l2<=m;l2++) {
  45. long temp = dp[i][j][l] + p[i+1][l2];
  46. if (l2==l){
  47. if (dp[i+1][j][l2]==-1) {
  48. dp[i+1][j][l2] = temp;
  49. }else {
  50. dp[i+1][j][l2] = Math.min(dp[i+1][j][l2],temp);
  51. }
  52. }else {
  53. if (dp[i+1][j+1][l2]==-1) {
  54. dp[i+1][j+1][l2] = temp;
  55. }else {
  56. dp[i+1][j+1][l2] = Math.min(dp[i+1][j+1][l2], temp);
  57. }
  58. }
  59. }
  60. }else {
  61. //c[i+1]=x
  62. if (c[i+1]==l) {
  63. if (dp[i+1][j][l]==-1) {
  64. dp[i+1][j][l] = dp[i][j][l];
  65. }else {
  66. dp[i+1][j][l] = Math.min(dp[i+1][j][l], dp[i][j][l]);
  67. }
  68. }else {
  69. if (dp[i+1][j+1][c[i+1]]==-1) {
  70. dp[i+1][j+1][c[i+1]] = dp[i][j][l];
  71. }else {
  72. dp[i+1][j+1][c[i+1]] = Math.min(dp[i+1][j+1][c[i+1]], dp[i][j][l]);
  73. }
  74. }
  75. }
  76. }
  77. }
  78. //dp[n][k][1~m]
  79. long ma = -1;
  80. for (int i = 1;i <= m;i++)
  81. if (dp[n][k][i]!=-1) {
  82. if (ma==-1)
  83. ma = dp[n][k][i];
  84. else
  85. ma = Math.min(ma, dp[n][k][i]);
  86. }
  87. out.println(ma);
  88. }
  89. }
  90. static class InputReader{
  91. public BufferedReader br;
  92. public StringTokenizer tokenizer;
  93. public InputReader(InputStream ins) {
  94. br = new BufferedReader(new InputStreamReader(ins));
  95. tokenizer = null;
  96. }
  97. public String next(){
  98. while (tokenizer==null || !tokenizer.hasMoreTokens()) {
  99. try {
  100. tokenizer = new StringTokenizer(br.readLine());
  101. }catch(IOException e) {
  102. throw new RuntimeException(e);
  103. }
  104. }
  105. return tokenizer.nextToken();
  106. }
  107. public int nextInt() {
  108. return Integer.parseInt(next());
  109. }
  110. }
  111. }

【Codeforces 711C】Coloring Trees的更多相关文章

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

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

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【42.07%】【codeforces 558A】Lala Land and Apple Trees

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【codeforces 755C】PolandBall and Forest

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【codeforces 750F】New Year and Finding Roots

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【22.73%】【codeforces 606D】Lazy Student

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  9. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

随机推荐

  1. sql2000数据库置疑造成的原因以及如何解决置疑

    造成数据库置疑一般有以下几点: 1)电脑非法关机或者意外停电: 2)磁盘有坏道或者损坏: 3)数据库感染病毒,日志文件损坏: 4)非正常情况下移动数据库文件 5)系统,硬盘,经常强制性关机(如断电)类 ...

  2. 常用的几个Dos命令-持续更新中

    1.服务相关 (1).查看服务 C:\Windows\system32>net start 已经启动以下 Windows 服务: (2).启动服务 C:\Windows\system32> ...

  3. 关于java的print()

    print方法是类PrintStream的方法成员,而System类有一个static的PrintStream类型的属性成员,名叫out,我们平时写的System.out.print("he ...

  4. PHP安装yaf在ubuntu下面的问题解决

    1.在执行make的时候出现如下错误: In file included from /root/yaf-2.1.2/yaf_router.c:28: /usr/include/php/ext/pcre ...

  5. python+opencv+Face++实现人脸识别比对

    2018-03-2010:16:55 代码仓库--GitHub--https://github.com/az666/python_opencv_face- 依旧是先来图片 下面这张是我进行识别的效果( ...

  6. C语言调用Python

    python模块:demo.py def print_arg(str): print str def add(a,b): print 'a=', a print 'b=', b return a + ...

  7. PHP实现写LOG日志的代码

    这篇文章给大家介绍的内容是关于PHP实现写LOG日志的代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. public function write_log(){ //设置目录时间 ...

  8. daxcie

    Database->Edit Current DBMS菜单 修改如下:选中General选项卡,依次打开Script->Sql->Fomat->CaseSensitivityU ...

  9. POJ_3565_Ants

    题意:给出N个白点和N个黑点,要求用N条不相交的线段把它们连接起来,其中每条线段恰好连接一个白点和一个黑点,每个点恰好连接到一条线段. 分析:因为有结点黑白两色,我们不难想到构造一个二分图,其中每个白 ...

  10. spring 实例 bean 的方式

    一.使用构造器实例化: <bean id="personService" class="cn.mytest.service.impl.PersonServiceBe ...