CodeForces 711C - Coloring Trees DP

原创 2016年08月30日 21:32:03
/*

http://codeforces.com/problemset/problem/711/C
http://codeforces.com/blog/entry/46830

官方题解:

We compute the following array : dp[i][j][k] denoting the minimum amount of paint needed to color the first i trees such that it has beauty j and the i-th tree is colored by color k, and initialize all these values to INF. We can compute this dp array easily by considering two cases :

1. When the last color used is equal to the current color, then we should compare it with dp[i - 1][j][k] + cost[i][k] if it was originally uncolored or dp[i - 1][j][k] otherwise, since the beauty of the coloring is the same.

2. When the last color used is different from the current color, then we should compare it with dp[i - 1][j - 1][l] + cost[i][k] or dp[i - 1][j - 1][l] for all 1 <= l <= m except when l is equal to the current color, by similar reasoning.

If the current tree is uncolored, we loop through all the m possible colors to color it.

Naively implementing this dp will give an O(nkm2), which is sufficient to pass for this problem. However, it is possible to optimize it into O(nkm) by avoiding iterating through all colors when considering the last color used and store two global minimums. See the code for more the details.

Time Complexity : O(nkm2) or O(nkm)
*/



#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <algorithm>
#include <functional>
#include <vector>
#include <iomanip>
#include <math.h>
#include <iostream>
#include <sstream>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
const int MAX=105;
const ll INF=(ll)1e18;
ll N,M,K,A[MAX],F[MAX][MAX][MAX],Cost[MAX][MAX];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    for (int i=0; i<MAX; i++)
        for (int j=0; j<MAX; j++)
            for (int k=0; k<MAX; k++)
                F[i][j][k]=INF;
    cin>>N>>M>>K;
    for (int i=1; i<=N; i++)
        cin>>A[i];
    for (int i=1; i<=N; i++)
        for (int j=1; j<=M; j++)
            cin>>Cost[i][j];
    if (A[1]==0)
        for (int i=1; i<=M; i++)
            F[1][1][i]=Cost[1][i];
    else
        F[1][1][A[1]]=0;
    for (int i=2; i<=N; i++)
    { 
        for (int j=1; j<=K; j++)
        {
            if (A[i]==0)
            {
                for (int k=1; k<=M; k++)
                {
                    F[i][j][k]=min(F[i][j][k],F[i-1][j][k]+Cost[i][k]);
                    for (int l=1; l<=M; l++)
                        if (l!=k)
                            F[i][j][k]=min(F[i][j][k],F[i-1][j-1][l]+Cost[i][k]);
                }
            }
            else
            {
                F[i][j][A[i]]=min(F[i][j][A[i]],F[i-1][j][A[i]]);
                for (int l=1; l<=M; l++)
                {
                    if (l!=A[i])
                        F[i][j][A[i]]=min(F[i][j][A[i]],F[i-1][j-1][l]);
                }
            }
        }
    }
    ll Ans=INF;
    for (int i=1; i<=M; i++)
        Ans=min(Ans,F[N][K][i]);
    if (Ans>=INF)
        Ans=-1;
    cout<<Ans;
    return 0;
}

版权声明:转载请注明出处

CodeForces 711C.Coloring Trees【DP】

题目链接http://codeforces.com/problemset/problem/711/C思路设dp[i][x][l]代表涂完前i棵树,美丽值为x,最后一棵树的颜色为l的最小代价。 那么...

Codeforces Problem 711C Coloring Trees(DP)

Codeforces Problem 711C Coloring Trees(DP)

CodeForces - 711C Coloring Trees 三维DP

题目大意输入nn,mm,kk,n表示有多少棵树,m表示有多少中颜色可以给树添加,k表示完美树的个数, 下面的n行表示给第ii棵树染色jj需要的花费,问把树染成k个完美树需要的最小花费。 第二行0表...

Codeforces711C-Coloring Trees(dp)

题目链接http://codeforces.com/contest/711/problem/C思路dp,状态还是比较好表示 **状态表示**d[i][j][k],前i个树都已经染色, 第i棵树颜色为...
  • Lzedo
  • Lzedo
  • 2016年09月23日 20:56
  • 96

【动态规划】Codeforces 711C Coloring Trees

题目链接:   http://codeforces.com/problemset/problem/711/C 题目大意:   给N棵树,M种颜色,已经有颜色的不能涂色,没颜色为0...

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

动态规划
  • sdfzyhx
  • sdfzyhx
  • 2016年08月30日 14:00
  • 147

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

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

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 Round #369 (Div. 2) -- C. Coloring Trees (三维DP)

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

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

[Codeforces Round #369 (Div. 2) C. Coloring Trees] DP题目链接:[Codeforces Round #369 (Div. 2) C. Colorin...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CodeForces 711C - Coloring Trees DP
举报原因:
原因补充:

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