# Coloring Trees (三维DP）

B题没有考虑到除了0点以外，原来就不符合的情况，又掉分了…..

n颗树，每棵树可以涂m种颜色，把树分成k组，连续相同颜色的树为合并为一组
给出了n颗树的涂色情况，当涂色为0的时候，表示还没有涂颜色
给n * m 的矩阵，第i行j列为 第i个点，图j颜色的消耗
求n颗树涂颜色涂成k组时，用的最少消耗是多少

dp[i][k][j] 表示到了第i个点，分成k组， 颜色为j时，最少消耗为多少
当前点被涂过颜色：
dp[now][j + (a[i] != k)][a[i]] = min(dp[now][j + (a[i] != k)][a[i]], dp[next][j][k]);

for(int l = 1; l <= m; l++)
dp[now][j + (l != k)][l] = min(dp[now][j + (l != k)][l], dp[next][j][k] + val[i][l]);

input
3 2 2
0 0 0
1 2
3 4
5 6
output
10

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 5;
int n, m, K, c[105], a[105][105];
LL f[105][105][105];
int main() {
scanf("%d%d%d", &n, &m, &K);
for (int i=1; i<=n; i++) scanf("%d", &c[i]);
for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) scanf("%d", &a[i][j]);
memset(f, 0x3f, sizeof(f));
if (c[1]) f[1][1][c[1]] = 0;
else for (int i=1; i<=m; i++) f[1][1][i] = a[1][i];
for (int i=2; i<=n; i++) {
for (int j=1; j<=i&&j<=K; j++) {
if (c[i]) {
f[i][j][c[i]] = min(f[i][j][c[i]], f[i-1][j][c[i]]);
for (int k=1; k<=m; k++) if (c[i]!=k) {
f[i][j][c[i]] = min(f[i][j][c[i]], f[i-1][j-1][k]);
}
} else {
for (int k=1; k<=m; k++) {
f[i][j][k] = min(f[i][j][k], f[i-1][j][k] + a[i][k]);
for (int q=1; q<=m; q++) if (q!=k) {
f[i][j][k] = min(f[i][j][k], f[i-1][j-1][q] + a[i][k]);
}
}
}
}
}
LL ans = f[104][104][104];
for (int i=1; i<=m; i++) {
ans = min(ans, f[n][K][i]);
}
if (ans == f[104][104][104]) {
cout << "-1" << endl;
} else {
cout << ans << endl;
}
return 0;
}

• 本文已收录于以下专栏：

## codeforces Round #369 (Div. 2) C. Coloring Trees (三维DP)

﻿﻿ C. Coloring Trees time limit per test 2 seconds memory limit per test ...

## CodeForces 369 div2 C Coloring Trees DP

C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input stan...

## [Codeforces Round #369 (Div. 2) C. Coloring Trees] DP

[Codeforces Round #369 (Div. 2) C. Coloring Trees] DP题目链接：[Codeforces Round #369 (Div. 2) C. Colorin...

## Codeforces Problem 711C Coloring Trees(DP)

Codeforces Problem 711C Coloring Trees(DP)

## Codeforces711C-Coloring Trees（dp）

• Lzedo
• 2016-09-23 20:56
• 96

## Codeforces #369（Div.2） C.Coloring Trees【Dp】

Coloring Trees Description ZS the Coder and Chris the Baboon has arrived at Udayland! ...

## codeforces Round_369 C. Coloring Trees

C. Coloring Trees

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)