关闭

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

标签: dpcodeforces
385人阅读 评论(0) 收藏 举报
分类:

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

题目链接[Codeforces Round #369 (Div. 2) C. Coloring Trees]
题意描述:给定N棵树,有M种颜料,每个树最初的颜色分别是c1,c2,,cn(0ciM)ci为0表示的是第i棵树没有还染色,现在要对所有没有染过色的树进行染色(即ci=0),并且给定第i棵树染第j种颜料的费用是pij,在刚好将数段划分为K段的情况下求最小的花费, (1KN100,1M100)
解题思路
用dp[i][j][k]表示前i棵树染色并且最后一个的颜色是j,划分的数段数为k的最小花费。
那么,写出如下递推式:

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)

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

//#pragma comment(linker, "/STACK:1024000000,1024000000")

#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
查看评论

Codeforces Round #459 (Div. 2) C. The Monster

题目链接 C. The Monster time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output As Will is...
  • ffgcc
  • ffgcc
  • 2018-01-30 09:48
  • 51

Codeforces Round #406 (Div. 2):C. Berzerk(记忆化搜索解决博弈问题)

C. Berzerk time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Rick and Morty are...
  • Jaihk662
  • Jaihk662
  • 2017-03-24 15:23
  • 876

【解题报告】Codeforces Round #367 (Div. 2)

简略的解题报告。
  • TRiddle
  • TRiddle
  • 2016-08-12 21:37
  • 740

Codeforces Round #350 (Div. 2)(C)模拟

C. Cinema time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output ...
  • qq_24489717
  • qq_24489717
  • 2016-05-06 15:29
  • 482

Codeforces Round #204 (Div. 1) A. Jeff and Rounding

A. Jeff and Rounding time limit per test 1 second memory limit per test 256 megabytes input standard input output standard out...
  • u010422038
  • u010422038
  • 2013-10-05 08:54
  • 1483

Codeforces Round #254 (Div. 1)C. DZY Loves Colors(线段树经典操作/分块)

题目链接 C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard input output stan...
  • fouzhe
  • fouzhe
  • 2017-02-11 15:17
  • 288

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

大体题意: 给你n 个树,你要给这些树染色,  标号是0 表示这棵树还没有染色,标号不是0 表示这棵树已经染色 不需要再染,这片树的美丽程度是  连续相同颜色的数目! 告诉你指定美丽程度K,和  最多的颜色种类m ,求得最小花费,每一棵树染不同颜色的花费也告诉了你! 思...
  • aozil_yang
  • aozil_yang
  • 2016-09-02 23:32
  • 350

Codeforces Round #214 (Div. 2) C: Dima and Salad 想减肥?吃沙拉

#include #include #include using namespace std; struct abc{ int t, a; }c[110]; int dp[2][200000]; int main(){ freopen("in.txt",&q...
  • u012952004
  • u012952004
  • 2013-11-29 01:27
  • 670

Codeforces Round #116 (Div. 2, ACM-ICPC Rules) C - Letter

#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #ifdef _WIN...
  • zz_1215
  • zz_1215
  • 2012-04-22 17:53
  • 537

Codeforces Round #214 (Div. 2) C. Dima and Salad (背包变形)

C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output...
  • u010228612
  • u010228612
  • 2013-12-10 22:16
  • 1322
    个人资料
    • 访问:311060次
    • 积分:5904
    • 等级:
    • 排名:第5172名
    • 原创:273篇
    • 转载:8篇
    • 译文:0篇
    • 评论:45条
    友情链接
    最新评论