C. Coloring Trees

题目连接:

http://www.codeforces.com/contest/711/problem/C

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

Hint

题意

给你\(n\)棵树,如果\(c_i\)为0的话,那么这棵树就没有上色,否则这棵树就是\(c_i\)颜色的。

相同颜色的树会被当成一段,现在你要恰好刷漆刷成k段,问你最小花费是多少。

把第i棵树刷漆刷成j颜色的花费为\(p[i][j]\)

题解:

dp[i][j][k]表示第i棵树,刷成了j颜色,当前有k段的最小花费是多少。

然后好礼n4转移就好了,很容易就能够优化成空间n2,时间n^3的。

不优化也能过。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
const long long inf = 1000000000000000LL;
long long dp[2][maxn][maxn];
int n,m,K;
int c[maxn],p[maxn];
int now,pre=1;
int main()
{
scanf("%d%d%d",&n,&m,&K);
for(int i=1;i<=n;i++)
scanf("%d",&c[i]);
for(int j=0;j<=m;j++)
for(int k=0;k<=n;k++)
dp[now][j][k]=inf;
for(int j=1;j<=m;j++)
scanf("%d",&p[j]);
if(c[1]==0)
{
for(int j=1;j<=m;j++)
dp[now][j][1]=p[j];
}
else
dp[now][c[1]][1]=0;
for(int i=2;i<=n;i++)
{
swap(pre,now);
for(int j=0;j<=m;j++)
for(int k=0;k<=n;k++)
dp[now][j][k]=inf;
for(int j=1;j<=m;j++)
scanf("%d",&p[j]);
if(c[i]==0)
{
for(int j=1;j<=m;j++)
for(int k=1;k<=n;k++)
{
dp[now][j][k]=min(dp[now][j][k],dp[pre][j][k]+p[j]);
for(int t=1;t<=m;t++)
{
if(t==j)continue;
dp[now][j][k]=min(dp[now][j][k],dp[pre][t][k-1]+p[j]);
}
}
}
else
{
for(int k=1;k<=n;k++)
{
dp[now][c[i]][k]=min(dp[now][c[i]][k],dp[pre][c[i]][k]);
for(int t=1;t<=m;t++)
{
if(t==c[i])continue;
dp[now][c[i]][k]=min(dp[now][c[i]][k],dp[pre][t][k-1]);
}
}
}
}
long long ans = inf;
for(int i=1;i<=m;i++)
ans=min(ans,dp[now][i][K]);
if(ans>=inf)printf("-1\n");
else printf("%lld\n",ans);
}

Codeforces Round #369 (Div. 2) C. Coloring Trees 动态规划的更多相关文章

  1. 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 ...

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

    C. Coloring Trees   ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the pa ...

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

    C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)

    题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...

  5. Codeforces Round #369 (Div. 2) C. Coloring Trees(简单dp)

    题目:https://codeforces.com/problemset/problem/711/C 题意:给你n,m,k,代表n个数的序列,有m种颜色可以涂,0代表未涂颜色,其他代表已经涂好了,连着 ...

  6. Codeforces Round #369 (Div. 2)-C Coloring Trees

    题目大意:有n个点,由m种颜料,有些点没有涂色,有些点已经涂色了,告诉你每个点涂m种颜色的价格分别是多少, 让你求将这n个点分成k段最少需要多少钱. 思路:动态规划,我们另dp[ i ][ j ][ ...

  7. Codeforces Round #369 (Div. 2) C 基本dp+暴力

    C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. Codeforces #369 (Div. 2) C. Coloring Trees (3维dp

    http://codeforces.com/group/1EzrFFyOc0/contest/711/problem/C https://blog.csdn.net/qq_36368339/artic ...

  9. Codeforces Round #106 (Div. 2) D. Coloring Brackets 区间dp

    题目链接: http://codeforces.com/problemset/problem/149/D D. Coloring Brackets time limit per test2 secon ...

随机推荐

  1. 20155314 2016-2017-2 《Java程序设计》第7周学习总结

    20155314 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 了解Lambda语法 了解方法引用 了解Fucntional与Stream API 掌握Da ...

  2. Android利用LocalSocket实现Java端进程与C端进程之间的IPC

    Android是建立在Linux之上的OS,在涉及到安全.网络协议.文件加密等功能时,往往需要通过C语言调用底层API来实现,而如何发出指令让C端执行我们想要的功能,并且在执行之后有返回结果呢,这就需 ...

  3. HDU 4506 小明系列故事——师兄帮帮忙(二分快速幂)

    题意:就是输入一个数组,这个数组在不断滚动,而且每滚动一次后都要乘以一个数,用公式来说就是a[i] = a[i-1] * k;然后最后一位的滚动到第一位去. 解题报告:因为题目中的k要乘很多次,达到了 ...

  4. 第9月第3天 uilabel contentscale

    1. http://blog.csdn.net/u012703795/article/details/43706449

  5. HBase笔记之namespace

    一.什么是namespace 在RDBMS中有database的概念,用来对table进行分组,那么在HBase中当表比较多的时候如何对表分组呢,就是namespace,可以简单的把namespace ...

  6. ListView position

    在使用listview的时候,我们经常会在listview的监听事件中,例如OnItemClickListener(onItemClick)中,或listview的adapter中(getView.g ...

  7. 【Python】Flask系列-URL和视图笔记

    1.学习目标 熟悉Flask相关知识. 熟悉web开发流程. 能独立开发Flask项目. 2.环境配置 Python虚拟环境安装 因为python的框架更新迭代太快了,有时候需要在电脑上存在一个框架的 ...

  8. python3之memcached

    1.memcached介绍 Memcached是一个自由开源的,高性能,分布式内存对象缓存系统. Memcached是以LiveJournal旗下Danga Interactive公司的Brad Fi ...

  9. linux下简单的备份的脚本 2 【转】

    转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=26807463&id=4577034 之前写过linux下简单的 ...

  10. LCT摘要

    介绍.用途 LCT是树链剖分中的一种,又叫实链剖分.动态树,常用于维护动态的树.森林. 维护方式 LCT并不直接维护原树,而是用一堆splay作为辅助树来维护.原树中的一条实链上的点在一棵splay中 ...