题目链接: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. 用 python 实现一个多线程网页下载器

    今天上来分享一下昨天实现的一个多线程网页下载器. 这是一个有着真实需求的实现,我的用途是拿它来通过 HTTP 方式向服务器提交游戏数据.把它放上来也是想大家帮忙挑刺,找找 bug,让它工作得更好. k ...

  2. Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead

    Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead 在解决问题Underscores can only ...

  3. datatable动态添加,及填充数据

    DataTable tblDatas = new DataTable("Datas"); tblDatas.Columns.Add("ID", Type.Get ...

  4. MVC 移除复数表名的契约

    在数据库上下文中添加: using System.Data.Entity.ModelConfiguration.Conventions; protected override void OnModel ...

  5. android 文字图片合成

    引用:http://blog.csdn.net/cq361106306/article/details/8142526 两种方法: 1.直接在图片上写文字 String str = "PIC ...

  6. python 清楚数组重复字符串元素

    l1 = ['bb','c','d','bb','c','a','a'] l2 = {}.fromkeys(l1).keys() print (l2)

  7. * 和 ** python

    *代表tuple集合,**代表dict def func(a, b, c=0,  *args, **kw) print ('a=',a, 'b=',b,'c=',c,'args=',args,'kw= ...

  8. SQL学习指南 ——笔记

    前言:每章的练习题很实用,跟着练了一遍.答案附录有 1.流行的商业级关系数据库:

  9. [转]使用xcode4 workspace 多个project协同工作

    转载地址:http://erlangprogram.com/use-xcode4-workspace   一般的某个应用单独新建一个 project 就可以了,然后把所有的程序文件都放在里面,这个可以 ...

  10. 关于.net编译时目标生成平台

    x86: 将程序集编译为由兼容 x86 的 32 位公共语言运行库运行. x64: 将程序集编译为由支持 AMD64 或 EM64T 指令集的计算机上的 64 位公共语言运行库运行. anycpu:( ...