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

【题意】

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

【题解】

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

【代码】

import java.io.*;
import java.util.*; public class Main { static InputReader in;
static PrintWriter out; public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
} static int N = 100;
static class Task{ int n,m,k;
long dp[][][] = new long[N+10][N+10][N+10];
int p[][] = new int[N+10][N+10];
int c[] = new int[N+10];
//dp[i][j][k]前i棵树,分成j段,第j段最后一棵树颜色为m的最小花费 public void solve(InputReader in,PrintWriter out) {
n = in.nextInt(); m = in.nextInt();k = in.nextInt();
for (int i = 1;i <= n;i++) c[i] = in.nextInt();
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
p[i][j] = in.nextInt();
for (int i = 0;i <=N;i++)
for (int j = 0;j <= N;j++)
for(int k = 0;k <=N;k++)
dp[i][j][k] = -1;
if (c[1]==0) {
for (int j = 1;j <= m;j++) {
dp[1][1][j] = p[1][j];
}
}else {
dp[1][1][c[1]] = 0;
}
for (int i = 1;i < n;i++)
for (int j = 1;j <= k;j++)
for (int l = 1;l <= m;l++) {
if (dp[i][j][l]!=-1) {
if(c[i+1]==0) {
for (int l2=1;l2<=m;l2++) {
long temp = dp[i][j][l] + p[i+1][l2];
if (l2==l){
if (dp[i+1][j][l2]==-1) {
dp[i+1][j][l2] = temp;
}else {
dp[i+1][j][l2] = Math.min(dp[i+1][j][l2],temp);
}
}else {
if (dp[i+1][j+1][l2]==-1) {
dp[i+1][j+1][l2] = temp;
}else {
dp[i+1][j+1][l2] = Math.min(dp[i+1][j+1][l2], temp);
}
}
}
}else {
//c[i+1]=x
if (c[i+1]==l) {
if (dp[i+1][j][l]==-1) {
dp[i+1][j][l] = dp[i][j][l];
}else {
dp[i+1][j][l] = Math.min(dp[i+1][j][l], dp[i][j][l]);
}
}else {
if (dp[i+1][j+1][c[i+1]]==-1) {
dp[i+1][j+1][c[i+1]] = dp[i][j][l];
}else {
dp[i+1][j+1][c[i+1]] = Math.min(dp[i+1][j+1][c[i+1]], dp[i][j][l]);
}
}
}
}
}
//dp[n][k][1~m]
long ma = -1;
for (int i = 1;i <= m;i++)
if (dp[n][k][i]!=-1) {
if (ma==-1)
ma = dp[n][k][i];
else
ma = Math.min(ma, dp[n][k][i]);
}
out.println(ma);
}
} static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer; public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
} public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}
}

【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. 国外知名IT网站(转载)

    转自:http://supportopensource.iteye.com/blog/780566 =========================================== 1.Cnet ...

  2. MySQL5.7 windows二进制安装

    200 ? "200px" : this.width)!important;} --> 介绍 1.下载解压 下载地址:http://dev.mysql.com/get/Dow ...

  3. bzoj 4297: [PA2015]Rozstaw szyn【瞎搞】

    从叶子往上先拓扑一下,建立虚拟root,从root开始dfs.注意到每个点的最优取值一定是一个区间(中位数区间),从儿子区间推出父亲区间即可 #include<iostream> #inc ...

  4. P4556 [Vani有约会]雨天的尾巴(线段树合并)

    传送门 一道线段树合并 首先不难看出树上差分 我们把每一次修改拆成四个,在\(u,v\)分别放上一个,在\(lca\)和\(fa[lca]\)各减去一个,那么只要统计一下子树里的总数即可 然而问题就在 ...

  5. HTML 5.1 -- 14项新增功能及如何使用

    最近太忙了 过完年来 连续的加班让我筋疲力尽,今天终于把东西交了,抽空来点干货吧! 1. 响应式图像 W3C 引入了一些功能特性,无需使用 CSS 就可以实现响应式图像.它们是 … srcset 图像 ...

  6. 讯搜问题排查xunsearch

    mysql导入数据不成功,开始重建索引后提示 [XSException] ../local/xunsearch/sdk/php/lib/XS.php(1898): DB- 可打印的版本 开始重建索引 ...

  7. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...

  8. 1619. [HEOI2012]采花

    1619. [HEOI2012]采花 ★★☆   输入文件:1flower.in   输出文件:1flower.out   简单对比 时间限制:5 s   内存限制:128 MB [题目描述] 萧薰儿 ...

  9. es6之数据结构 set,WeakSet,mapWeakMap

    { let list = new Set(); list.add(1); list.add(2); list.add(1); console.log(list); //Set(2) {1, 2} le ...

  10. IBatis的分页研究

    IBatis的分页研究 博客分类: Ibatis学习   摘自: http://cpu.iteye.com/blog/311395 yangtingkun   Oracle分页查询语句 ibaits. ...