zoukankan      html  css  js  c++  java
  • Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)

    Coloring Trees

    Problem Description:

    ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park where n trees grow. They decided to be naughty and color the trees in the park. The trees are numbered with integers from 1 to n from left to right.

    Initially, tree i has color ci. ZS the Coder and Chris the Baboon recognizes only m different colors, so 0 ≤ ci ≤ m, where ci = 0 means that tree i is uncolored.

    ZS the Coder and Chris the Baboon decides to color only the uncolored trees, i.e. the trees with ci = 0. They can color each of them them in any of the m colors from 1 to m. Coloring the i-th tree with color j requires exactly pi, j litres of paint.

    The two friends define the beauty of a coloring of the trees as the minimum number of contiguous groups (each group contains some subsegment of trees) you can split all the n trees into so that each group contains trees of the same color. For example, if the colors of the trees from left to right are 2, 1, 1, 1, 3, 2, 2, 3, 1, 3, the beauty of the coloring is 7, since we can partition the trees into 7 contiguous groups of the same color : {2}, {1, 1, 1}, {3}, {2, 2}, {3}, {1}, {3}.

    ZS the Coder and Chris the Baboon wants to color all uncolored trees so that the beauty of the coloring is exactly k. They need your help to determine the minimum amount of paint (in litres) needed to finish the job.

    Please note that the friends can't color the trees that are already colored.

    Input:

    The first line contains three integers, n, m and k (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of trees, number of colors and beauty of the resulting coloring respectively.

    The second line contains n integers c1, c2, ..., cn (0 ≤ ci ≤ m), the initial colors of the trees. ci equals to 0 if the tree number i is uncolored, otherwise the i-th tree has color ci.

    Then n lines follow. Each of them contains m integers. The j-th number on the i-th of them line denotes pi, j (1 ≤ pi, j ≤ 109) — the amount of litres the friends need to color i-th tree with color j. pi, j's are specified even for the initially colored trees, but such trees still can't be colored.

    Output:

    Print a single integer, the minimum amount of paint needed to color the trees. If there are no valid tree colorings of beauty k, print  - 1.

    Sample Input:

    3 2 2
    0 0 0
    1 2
    3 4
    5 6

    Sample Output:

    10
    这是后来补上的,为什么我做dp的时候就是想不到公式呢?还应该多做dp啊

    【题目链接】Codeforces 711C

    【题目类型】dp

    &题意:
    先给你n颗树,有m种颜色,你要把所以的数都涂色,色是[1,m],0代表没有色,只有当这颗树是0的时候你才可以涂色,并且你涂完色的树必须要满足正好是k段,涂每个树的颜色都有不同的花费,给你这些数据,你要输出最小花费,不可行是输出-1。

    &题解:

    首先看范围100,因为这是cf评测的,所以n^4可过,并且能开三维数组,那么就可以dp了。
    dp[i][j][k]:表示考虑第i棵树涂第j种颜色,当前分为k组的最小花费
    写dp,先写初始条件,之后找方程,判断状态,最后选最小值输出就好了

    【时间复杂度】O(n^4)

    &代码:

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int INF = 0x3f3f3f3f;
    #define cle(a,val) memset(a,(val),sizeof(a))
    #define SI(N) scanf("%d",&(N))
    #define SI2(N,M) scanf("%d %d",&(N),&(M))
    #define SI3(N,M,K) scanf("%d %d %d",&(N),&(M),&(K))
    #define rep(i,b) for(int i=0;i<(b);i++)
    #define rez(i,a,b) for(int i=(a);i<=(b);i++)
    const ll LINF = 0x3f3f3f3f3f3f3f3f;
    const int MAXN = 100 + 5 ;
    int n, m, k, p[MAXN][MAXN], a[MAXN];
    ll dp[MAXN][MAXN][MAXN];
    void Solve() {
    	while (~SI3(n, m, k)) {
    		rez(i, 1, n)SI(a[i]);
    		rez(i, 1, n) rez(j, 1, m)SI(p[i][j]);
    		cle(dp, 0x3f);
    		if (a[1]) dp[1][a[1]][1] = 0;
    		else rez(i, 1, m) dp[1][i][1] = p[1][i];
    		rez(i, 2, n) rez(j, 1, m) rez(u, 1, k) if (dp[i - 1][j][u] < LINF) {
    			if (a[i])dp[i][a[i]][u + (a[i] != j)] = min(dp[i][a[i]][u + (a[i] != j)], dp[i - 1][j][u]);
    			else rez(v, 1, m) dp[i][v][u + (v != j)] = min(dp[i][v][u + (v != j)], dp[i - 1][j][u] + p[i][v]);
    		}
    		ll re = LINF;
    		rez(i, 1, m)
    		re = min(re, dp[n][i][k]);
    		if (re == LINF) re = -1;
    		cout << re << endl;
    	}
    }
    int main() {
    	Solve();
    	return 0;
    }
    
  • 相关阅读:
    3、二进制的秘闻和不同进制间的转换
    Hello World!
    HDU5883 The Best Path(欧拉回路 | 通路下求XOR的最大值)
    Codeforces 722C(并查集 + 思维)
    Floyd 算法求多源最短路径
    vim 配置
    STL容器 -- Vector
    STL容器 -- Bitset
    HDU 5707 Combine String(动态规划)
    HDU 5876 Sparse Graph(补图上BFS)
  • 原文地址:https://www.cnblogs.com/s1124yy/p/5827428.html
Copyright © 2011-2022 走看看