关闭

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

标签: dpcodeforces
219人阅读 评论(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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:213577次
    • 积分:4772
    • 等级:
    • 排名:第6066名
    • 原创:245篇
    • 转载:8篇
    • 译文:0篇
    • 评论:32条
    最新评论