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

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef __int64 LL;
typedef pair <int, int> P;
const int N = 1e2 + ;
LL dp[N][N][N], a[N][N], val[N], INF = 1e16;
//dp[i][k][j] i棵树 k完美值 j颜色 int main()
{
LL n, k, m;
scanf("%lld %lld %lld", &n, &m, &k);
for(LL i = ; i <= n; ++i)
scanf("%lld", val + i);
for(LL i = ; i <= n; ++i) {
for(LL j = ; j <= m; ++j) {
scanf("%lld", &a[i][j]);
}
}
for(int i = ; i <= n; ++i) {
for(int j = ; j <= k; ++j) {
for(int x = ; x <= m; ++x) {
dp[i][j][x] = INF;
}
}
}
if(val[]) { //已有颜色
dp[][][val[]] = ;
} else {
for(LL i = ; i <= m; ++i) {
dp[][][i] = a[][i];
}
}
for(LL i = ; i <= n; ++i) {
for(LL j = ; j <= k; ++j) {
for(LL x = ; x <= m; ++x) {
if(dp[i - ][j][x] == INF)
continue;
if(val[i]) {
if(val[i] == x) { //与前一个颜色一致
dp[i][j][val[i]] = min(dp[i - ][j][x], dp[i][j][val[i]]);
} else {
dp[i][j + ][val[i]] = min(dp[i - ][j][x], dp[i][j + ][val[i]]);
}
} else {
for(LL y = ; y <= m; ++y) {
if(y == x) {
dp[i][j][y] = min(dp[i][j][y], dp[i - ][j][x] + a[i][y]);
} else {
dp[i][j + ][y] = min(dp[i][j + ][y], dp[i - ][j][x] + a[i][y]);
}
}
}
}
}
}
LL res = INF;
for(LL i = ; i <= m; ++i) {
if(dp[n][k][i] == -)
continue;
res = min(res, dp[n][k][i]);
}
printf("%lld\n", res == (LL)INF ? -: res);
return ;
}

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. 51nod1052 最大M子段和

    dp优化我总是不太熟练.这一次首先我写了O(n4)->O(n3)->O(n2).一步步的优化过来.yyl好像用的是单调队列优化dp我看不懂他的代码... O(n4) #include< ...

  2. iOS8 LaunchScreen.storyboard

    我目前的需求是需要将启动图片通过LaunchScreen.storyboard  来实现. 我首先想到的是创建一个Sb,使用自动布局来布局imageview,并设置如下图: 布局好之后,在Image里 ...

  3. pandas

  4. PHP Simple HTML DOM Parser Manual-php解析DOM

    PHP Simple HTML DOM Parser Manual http://www.lupaworld.com/doc-doc-api-770.html PHP Simple HTML DOM ...

  5. Activiti 多个并发子流程的应用

    多个部门发起资金计划,最后统一到财务部审批,每个部门发起资金计划是一个子流程,财务部审批是多个部门的计划同时审批,审批完成后,再提交上级领导审批. 流程如下: 要解决以上问题,需要实现多个子流程并行处 ...

  6. Python内置数据类型之Tuple篇

    Tuple 是不可变的 list.一旦创建了一个 tuple,就不可以改变它.这个有点像C++中的const修饰的变量.下面这段话摘自Dive Into Python: Tuple 比 list 操作 ...

  7. python练习程序(c100经典例17)

    题目: 输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. def foo(a): l=len(a); letters=0; space=0; digit=0; others=0; f ...

  8. wx处理鼠标事件

    #include "MainFrame.h" BEGIN_EVENT_TABLE(MyFrame,wxFrame) EVT_LEFT_DOWN(MyFrame::OnMouseLe ...

  9. HDU1026 Ignatius and the Princess I

    解题思路:打印路径是关键,细节处理见代码. #include<cstdio> #include<cstring> #include<algorithm> using ...

  10. 字符串string

    1.字符串获取类.封装检测数字的方法 var str = '前端开发'; //alert(str.length); //alert(str.charAt()); //没有参数 取得索引是0 结果是:前 ...