题目链接:C. Coloring Trees

题意:给出n棵树的颜色,有些树被染了,有些没有。现在让你把没被染色的树染色。使得beauty = k。问,最少使用的颜料是多少。

     K:连续的颜色为一组,一共有多少组。

颜料用量:p[i][j]表示第i棵树用颜料j染色 需要p[i][j]颜料。

思路:DP.

   dp方程:dp[i][j][k] = a 表示前i棵树beauty = j,且第j棵树染色为k时,需要的最少颜料为a。

状态转移:初始化第一棵树dp[1][1][col[1]or(1~m)].

遍历剩下的2~n棵树i,beauty = j (1~min(k, i))时,如果已经被染色了,直接更新dp[i][j or (j+1)][col[i]]为min(dp[i-1][j][1~m])。

如果没被染色,尝试染第kkk(1~m)种颜色,并更新dp[i][j or (j+1)][kkk]为min(dp[i-1][j][kk]) (1<=kk<=m, 1<=KKK<=m)。

喜欢这个题。

#include <stdio.h>
#include <string.h>
#include <iostream>
#define maxn 210
#define inf 1e16
#define LL long long
using namespace std; int col[maxn];
int p[maxn][maxn];
LL dp[maxn][maxn][maxn]; int main() {
int n, m, k;
// freopen("in.cpp", "r", stdin);
while(~scanf("%d%d%d", &n, &m, &k)) {
//input
for (int i=1; i<=n; ++i) {
scanf("%d", &col[i]);
} for (int i=1; i<=n; ++i) {
for (int j=1; j<=m; ++j) {
scanf("%I64d", &p[i][j]);
}
} //init
for (int i=1; i<=n; ++i) {
for (int j=1; j<=k; ++j) {
for (int kk=1; kk<=m; ++kk) {
dp[i][j][kk] = inf;
}
}
}
if (col[1]) dp[1][1][col[1]] = 0;
else {
for (int i=1; i<=m; ++i) {
dp[1][1][i] = p[1][i];
}
} //solve
for (int i=2; i<=n; ++i) { ///第i棵树
for (int j=1; j<=i && j<=k; ++j) { /// i-1 beauty=j
if (col[i]) { ///第i棵树颜色已经有了
for (int kk=1; kk<=m; ++kk) { ///
if (kk == col[i])
dp[i][j][col[i]] = min(dp[i][j][col[i]], dp[i-1][j][kk]);
else dp[i][j+1][col[i]] = min(dp[i][j+1][col[i]], dp[i-1][j][kk]);
}
} else {
for (int kk=1; kk<=m; ++kk) { ///i
for (int kkk=1; kkk<=m; ++kkk) { ///i-1
if (kk == kkk)
dp[i][j][kk] = min(dp[i][j][kk], dp[i-1][j][kkk]+p[i][kk]);
else dp[i][j+1][kk] = min(dp[i][j+1][kk], dp[i-1][j][kkk]+p[i][kk]);
}
}
}
}
} LL ans = inf;
for (int i=1; i<=m; ++i) {
ans = min(ans, dp[n][k][i]);
}
if (ans == inf) ans = -1;
printf("%I64d\n", ans);
}
return 0;
}

CodeForces #369 C. Coloring Trees DP的更多相关文章

  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 711 C. Coloring Trees (dp)

    题目链接:http://codeforces.com/problemset/problem/711/C 给你n棵树,m种颜色,k是指定最后的完美值.接下来一行n个数 表示1~n树原本的颜色,0的话就是 ...

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

  4. Codeforces 677C. Coloring Trees dp

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

  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. PHP5与MySQL数据库操作

    1 建立数据库表:  2 读取数据 2.1 建立01.php 2.2 建立member.php  3 修改数据 3.1 建立level.php(修改数据) 3.2 建立up_level.php  4 ...

  2. Java集合容器简介

    Java集合容器主要有以下几类: 1,内置容器:数组 2,list容器:Vetor,Stack,ArrayList,LinkedList, CopyOnWriteArrayList(1.5),Attr ...

  3. C++之路进阶——bzoj3876(支线剧情)

    F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  hyxzc Logout 捐赠本站 Notice:由于本OJ建立在 ...

  4. 什么是FOUC?如何避免FOUC?///////////////////////////zzzz

     一个新的名词叫做 FOUC 浏览器样式闪烁,之前也听说过一些类似的东西,比如样式突变等等,但这东西竟然有学名的..什么是FOUC(文档样式短暂失效)?如果使用import方法对CSS进行导入,会导致 ...

  5. JQuery基础二

    1.表单过滤器 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  6. CSS 笔记二(Text/Fonts/Links/Lists)

    CSS Text 1> Text Color used to set the color of the text 2> Text Alignment used to set the hor ...

  7. postgresql流复制配置

    一.配置环境: 示例环境 主机名 IP 角色 系统版本 数据目录 pg版本 db1 192.168.128.128 主库 RedHat5.3 /app/postgreSQL/data 9.1.7 db ...

  8. :before\:after伪元素用法

    :before和:after这两个伪元素在真正的页面元素之前和之后插入一个额外的的元素,等效于下面的代码: <p> <span>:before</span> HTM ...

  9. noi 4977 怪盗基德的滑翔翼

    题目链接: http://noi.openjudge.cn/ch0206/4977/ LIS http://paste.ubuntu.com/23406594/

  10. 从零开始学习jQuery(转)

    本系列文章导航 从零开始学习jQuery (一) 开天辟地入门篇 从零开始学习jQuery (二) 万能的选择器 从零开始学习jQuery (三) 管理jQuery包装集 从零开始学习jQuery ( ...