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

原创 2016年08月30日 16:06:45

[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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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

大体题意: 给你n 个树,你要给这些树染色,  标号是0 表示这棵树还没有染色,标号不是0 表示这棵树已经染色 不需要再染,这片树的美丽程度是  连续相同颜色的数目! 告诉你指定美丽程度K,和  最多...

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

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

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

ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park where n trees gr...

codeforces round 369div2 C Coloring Trees

ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park where n trees gro...

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 #369(Div.2) C.Coloring Trees【Dp】

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

【Codeforces Round #369 (Div. 2)】Codeforces 711C Coloring Trees

动态规划

codeforces Round_369 C. Coloring Trees

C. Coloring Trees

Codeforces Round #106(Div. 2) 149D. Coloring Brackets 区间DP 记忆化搜索

D. Coloring Brackets time limit per test 2 seconds memory limit per test 256 megabytes ...

CodeForces - 711C Coloring Trees 三维DP

题目大意输入nn,mm,kk,n表示有多少棵树,m表示有多少中颜色可以给树添加,k表示完美树的个数, 下面的n行表示给第ii棵树染色jj需要的花费,问把树染成k个完美树需要的最小花费。 第二行0表...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)