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

385人阅读 评论(0)

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

dp[i][j][k]={min{dp[i1][t][t==j?k:k1]}min{dp[i1][t][t==j?k:k1]+pij},if ci!=0,otherwise

t表示上一棵树的颜色，负责度：O(NM2K)$O(N*M^2*K)$

#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define fst             first
#define snd             second

//typedef __int64 LL;
typedef long long LL;
typedef pair<int, int> PII;

const LL INF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 100 + 5;

int N, M, K;
LL C[MAXN], P[MAXN][MAXN];
LL dp[MAXN][MAXN][MAXN];

int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif // ONLINE_JUDGE
while (~scanf ("%d %d %d", &N, &M, &K) ) {
for (int i = 1; i <= N; i++) {
scanf ("%I64d", &C[i]);
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
scanf ("%I64d", &P[i][j]);
}
}
memset (dp,  0x3f, sizeof (dp) );
if (C[1] == 0) {
for (int j = 1; j <= M; j++) {
dp[1][j][1] = P[1][j];
}
} else {
dp[1][C[1]][1] = 0;
}
for (int i = 2; i <= N; i++) {
if (C[i]) {
int j = C[i];
for (int k = 1; k <= K; k++) {
for (int t = 1; t <= M; t++) {
if (j == t)  dp[i][j][k] = min (dp[i][j][k], dp[i - 1][t][k]);
else dp[i][j][k] = min (dp[i][j][k], dp[i - 1][t][k - 1]);
}
}
} else {
for (int j = 1; j <= M; j++) {
for (int k = 1; k <= K; k++) {
if (C[i -  1]) {
int t = C[i -  1];
if (t == j) dp[i][j][k] = min (dp[i][j][k], dp[i - 1][t][k] + P[i][j]);
else dp[i][j][k] = min (dp[i][j][k], dp[i - 1][t][k - 1] + P[i][j]);
} else {
for (int t = 1; t <= M; t++) {
if (t == j) dp[i][j][k] = min (dp[i][j][k], dp[i - 1][t][k] + P[i][j]);
else dp[i][j][k] = min (dp[i][j][k], dp[i - 1][t][k - 1] + P[i][j]);
}
}
}
}
}
}
LL ans = INF;
for (int j = 1; j <= M; j++) {
ans = min (ans,  dp[N][j][K]);
}
if (ans == INF) ans = -1;
printf ("%I64d\n", ans);
}
return 0;
}
0
0

个人资料
• 访问：311060次
• 积分：5904
• 等级：
• 排名：第5172名
• 原创：273篇
• 转载：8篇
• 译文：0篇
• 评论：45条
友情链接
最新评论