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

[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;
}

Codeforces Round #471 div.2 ABC题解

2018-03-24 14:16:58

Codeforces Round #426 (Div. 2) D. The Bakery（DP+线段树）

2017-07-31 09:16:44

Codeforces 902 B.Coloring a Tree 简单的树

2017-12-20 18:20:12

Codeforces Round #482 (Div. 2)

2018-05-15 12:51:24

Codeforces Round #495 (Div. 2) C. Sonya and Robots（模拟）

2018-07-07 15:36:24

Codeforces Round #480 (Div. 2): C. Posterized（贪心）

2018-05-09 17:11:24

CF-Codeforces Round #483 (Div. 2)-D-XOR-pyramid

2018-05-16 01:15:04

Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2) C. Alternating Sum

2018-04-18 11:14:58

C. DNA Alignment(Codeforces Round #295(div2))

2015-03-02 18:27:17

Codeforces Round #448 (Div. 2) ABC

2017-11-27 14:47:02