codeforces Round_369 C. Coloring Trees

原创 2016年08月30日 16:10:33
                             C. Coloring Trees
                        time limit per test2 seconds
                     memory limit per test256 megabytes
                            inputstandard input
                           outputstandard output

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.

Examples
input
3 2 2
0 0 0
1 2
3 4
5 6
output
10
input
3 2 2
2 1 2
1 3
2 4
3 5
output
-1
input
3 2 2
2 0 0
1 3
2 4
3 5
output
5
input
3 2 3
2 1 2
1 3
2 4
3 5
output
0
Note
In the first sample case, coloring the trees with colors 2, 1, 1 minimizes the amount of paint used, which equals to 2 + 3 + 5 = 10. Note that 1, 1, 1 would not be valid because the beauty of such coloring equals to 1 ({1, 1, 1} is a way to group the trees into a single group of the same color).

In the second sample case, all the trees are colored, but the beauty of the coloring is 3, so there is no valid coloring, and the answer is  - 1.

In the last sample case, all the trees are colored and the beauty of the coloring matches k, so no paint is used and the answer is 0.

题目意思是:n棵树编号1~n, 依次输入n棵树的颜色(如果tree[i] == 0 表示该树无颜色)
然后有m种颜料,标号为1 ~ m, 以下 n 行 m 列为把第 i 棵树涂成颜色 j 所需的花费。
你的任务是把这n棵树用m种颜色涂成k段,每一段颜色相同,问你最少花费多少颜料。

因为被第二题卡成傻逼,我居然觉得我能在比赛中把这道题做出来。。。
我知道是dp,也知道怎么表示状态(太明显了),1分钟不到我也想出了状态转移方程,但是我细节没想好,就是在第 k 段和第k - 1 段转移的时候, 怎么区别第一个和不是第一个的不同操作。对,就是这个细节。我的确没有那个能力在很短的时间就想通。这不是小问题,这是致命的问题,我认了。所以我今天花了很长时间硬是活生生的搞出来了。
先说状态表示:
题目意思很直观,状态只和第几棵树,分成几段,涂成什么颜色有关,而这些数据的范围都不大于100,那么我定义三元组 (i, j, seg) 的意义是把第 i 棵树涂成第 j 种颜色加入到第 seg 段中,那么毫无疑问状态转移方程就出来了:

if(!tree[i])
    dp(i, j, seg) = Min(dp(i - 1, k, seg - 1), dp(i - 1, j, seg)); (k : 1 -> m && k != j)
else
    dp(i, j, seg) = Min(dp(i - 1, k, seg - 1), dp(i - 1, tree[i], seg)); (k : 1 -> m && k != tree[i])

根据转移方程能够确定要求出空间中一个点的值,必须先求出某些面的值,从而确定循环的顺序。
比如要确定(i, j, seg)的值就必须先确定(i - 1, k(1~m), seg - 1)的值。
所以我改变了一下状态表示,连同细节一起注释在代码里:


/*
 *
 *  Author : Triose
 *  Email  : Triose@163.com
 *  Update_time : 2016.6.12
 *
 */


//#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<map>
#include<set>
using namespace std;
//#define ONLINE_JUDGE
#define eps 1e-8
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define enter putchar(10)
#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define repe(i,a,b) for(int i = (a); i <= (b); ++i)
#define mem(a,b) (memset((a),b,sizeof(a)))
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sfs(a) scanf("%s",a)
#define pf(a) printf("%d\n",a)
#define pfd(a,b) printf("%d %d\n",a,b)
#define pfP(a) printf("%d %d\n",a.fi,a.se)
#define pfs(a) printf("%s\n",a)
#define pfI(a) printf("%I64d\n",a)
#define PR(a,b) pair<a,b>
#define fi first
#define se second
#define LL long long
#define DB double
#define ds(t) int t; sf(t)
const double PI = acos(-1.0);
const double E = exp(1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
int n, m, seg;

#define N 110
LL tree[N];             //tree[i] : 树的颜色 [1, n]
LL pay[N][N];           //pay[i][j] : 第i棵树刷成第j种颜色需要的花费[1, n] , [1, m]
LL dp[N][N][N];         //dp[i][k][j] : 第i棵树加入第k段涂成第j种颜色需要的最小花费

void Init() {
    repe(i, 1, n) cin >> tree[i];
    repe(i, 1, n) repe(j, 1, m) cin >> pay[i][j];
    repe(i, 0, n) repe(k, 0, seg) repe(j, 0, m) dp[i][k][j] = INFL; //结合下面的dp[0][0][0] = 0 来保证 dp 过程中每一步求得的值都是正确的, 同时也保证前i棵树不能分成k段的话(实际中),有dp[i][k][any_color] = INFL
}

LL solve() {
    dp[0][0][0] = 0;            //保证dp[i][1][colors]的值正确,即i == 1时, dp[i][1][colors] = dp[0][0][0] ( = 0 ) + pay[i][colors], 而 i != 1时, dp[i][1][colors] == dp[i][0][j] (==INFL) + pay[i][colors].
    repe(i, 1, n) {
        if(!tree[i]) {          //无颜色
            repe(k, 1, seg) {
                repe(j, 1, m) {
                    dp[i][k][j] = dp[i - 1][k][j] + pay[i][j];
                    repe(l, 0, m) if(l != j) dp[i][k][j] = Min(dp[i][k][j], dp[i - 1][k - 1][l] + pay[i][j]);
                }
            }
        }
        else {                  //有颜色,不能涂
            repe(k, 1, seg) {
                dp[i][k][tree[i]] = dp[i - 1][k][tree[i]];
                repe(j, 0, m) if(j != tree[i]) dp[i][k][tree[i]] = Min(dp[i][k][tree[i]], dp[i - 1][k - 1][j]);
            }
        }
    }
    LL ans = INFL;
    repe(i, 1, m) ans = Min(ans, dp[n][seg][i]);
    return ans == INFL ? -1 : ans;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//  freopen("out.txt", "w", stdout);
#endif
    while(cin >> n >> m >> seg) {
        Init();
        cout << solve() << endl;
    }
    return 0;
}

心累。

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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 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 Round #369 (Div. 2)】Codeforces 711C Coloring Trees

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

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 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 Round_369 C. Coloring Trees
举报原因:
原因补充:

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