关闭

DP-Codeforces Round #369 Div.2-C

204人阅读 评论(0) 收藏 举报
分类:

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.


题意:
有n棵树,m种颜色,其中每个数有一个颜色号码c[i],c[i]为0代表没有颜色,需要涂色,已经有颜色的树不能涂色,对于第i棵树,若要涂成号码为j的颜色,需要p[i][j]升颜料。
相邻的颜色相同的树为一个区间,现在已知n,m,k,c[1~n]和p,求将这n棵树划分为k个区间,最少需要多少升颜料,若不可能,则输出-1。


题解:
很明显的dp。
dp[i][j][k]表示将前j棵树划分为i个区间,且第j棵树的颜色号码为k时,所需要的最少颜料。
则dp[0][0][1:m]=0,任意i>j的dp[i][j][1:m]=INF。
可以得到dp[i][j][k]=min(dp[i][j-1][k]+color[j][k],dp[i-1][j-1][!k]+color[j][k])。
需要注意的是若c[j]不为0,则无法涂色,只能有dp[i][j][c[j]]=min(dp[i][j-1][c[j]],dp[i-1][j-1][!c[j]])。且dp[i][j][!c[j]]=INF。
最后看min(dp[k][n][1:m]),若不为INF则有解,输出即可。
INF一定要足够大,我一开始设了0x7fffffff卡了我20min…


//
//  main.cpp
//  cf-369-2-3
//
//  Created by 袁子涵 on 16/8/29.
//  Copyright © 2016年 袁子涵. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <map>

using namespace std;
const int maxn=110;
const long long int inf=0x6fffffffffffffff;
long long int dp[maxn][maxn][maxn];
long long int n,m,k,tree[maxn],color[maxn][maxn];
int main(int argc, const char * argv[]) {
    cin >> n >> m >> k;
    for (int i=1; i<=n; i++) {
        cin >> tree[i];
    }
    for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            dp[0][i][j]=inf;
            cin >> color[i][j];
        }
    }
    for (int i=1; i<=k; i++) {
        for (int j=1; j<=n; j++) {
            if (j<i) {
                for (int x=1; x<=m; x++) {
                    dp[i][j][x]=inf;
                }
                continue;
            }
            if (tree[j]!=0) {
                dp[i][j][tree[j]]=dp[i][j-1][tree[j]];
                for (int x=1; x<=m; x++) {
                    if (x==tree[j]) {
                        continue;
                    }
                    dp[i][j][tree[j]]=min(dp[i][j][tree[j]],dp[i-1][j-1][x]);
                    dp[i][j][x]=inf;
                }
                continue;
            }
            for (int x=1; x<=m; x++) {
                dp[i][j][x]=dp[i][j-1][x]+color[j][x];
                for (int y=1; y<=m; y++) {
                    if (y==x) {
                        continue;
                    }
                    dp[i][j][x]=min(dp[i][j][x],dp[i-1][j-1][y]+color[j][x]);
                }
            }
        }
    }
    long long int out=inf;
    for (int i=1; i<=m; i++) {
        out=min(out,dp[k][n][i]);
    }
    cout << (out==inf?-1:out) << endl;
    return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:44382次
    • 积分:1889
    • 等级:
    • 排名:千里之外
    • 原创:154篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条
    最新评论