CodeForces 711C - Coloring Trees DP

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


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()
    for (int i=0; i<MAX; i++)
        for (int j=0; j<MAX; j++)
            for (int k=0; k<MAX; k++)
    for (int i=1; i<=N; i++)
    for (int i=1; i<=N; i++)
        for (int j=1; j<=M; j++)
    if (A[1]==0)
        for (int i=1; i<=M; i++)
    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++)
                    for (int l=1; l<=M; l++)
                        if (l!=k)
                for (int l=1; l<=M; l++)
                    if (l!=A[i])
    ll Ans=INF;
    for (int i=1; i<=M; i++)
    if (Ans>=INF)
    return 0;


Codeforces 711C Coloring Trees DP

点击打开链接 //题意:n个数,给数字0填上(1~m),使得minimum number of contiguous groups为k的代价最小  //2,1,1,1,3,2,2,3,1,3 k=...
  • Jeremy1149
  • Jeremy1149
  • 2017年01月06日 11:28
  • 126

CodeForces 711C Coloring Trees(DP)

传送门:题目大意: 有若干个点,每个点开始有一个颜色值,如果有颜色,则用1~m表示涂有的颜色,如果没颜色用...
  • cmershen
  • cmershen
  • 2016年09月22日 16:12
  • 233

codeforces 711C Coloring Trees(DP)

题目链接: O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后b...
  • West___wind
  • West___wind
  • 2016年08月30日 12:15
  • 189

codeforces 711C - Coloring Trees

题目链接题意: 有n课树,存在m种颜色,希望能分成k段。 然后给出每棵树的初始颜色。 然后给出每棵树染成1~m颜色各自的花费 问最终变成k端花费最少是多少钱。 注意,初始有颜色的树不能...
  • naipp
  • naipp
  • 2016年08月30日 22:09
  • 106

Codeforces 711C Coloring Trees

C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input stan...
  • LuRiCheng
  • LuRiCheng
  • 2016年09月05日 00:53
  • 3776

CodeForces - 711C Coloring Trees(DP)(思维)

Coloring TreesZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park whe...
  • blessLZH0108
  • blessLZH0108
  • 2017年03月23日 08:53
  • 155

Codeforces Problem 711C Coloring Trees(DP)

Codeforces Problem 711C Coloring Trees(DP)
  • queuelovestack
  • queuelovestack
  • 2016年08月30日 15:02
  • 839

CodeForces 711C Coloring Trees【dp*好题】

ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park where n trees gro...
  • qq_36368339
  • qq_36368339
  • 2017年11月18日 14:24
  • 81

CodeForces - 711C Coloring Trees 三维DP

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

【动态规划】Codeforces 711C Coloring Trees

题目链接: 题目大意:   给N棵树,M种颜色,已经有颜色的不能涂色,没颜色为0...
  • u010568270
  • u010568270
  • 2016年08月30日 14:52
  • 571
您举报文章:CodeForces 711C - Coloring Trees DP