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. linux课程总结

    linux课程总结 --20125111 李冰清 转眼间,为期十六周的linux课程已进入尾声,回想起这十六周的课程,不断浮现在脑海里的是娄老师的笑容以及这十六周以来的点点滴滴. 第一次听到娄老师说将 ...

  2. WebSlides - 轻松制作漂亮的 HTML 幻灯片(演讲稿)

    WebSlides 是一个开源的 HTML 幻灯片项目,能够帮助熟悉前端语言的开发者快速制作出效果精美的幻灯片.页面中的每个 <section> 都是一个独立的幻灯片,只需要很少的 CSS ...

  3. HDU 1251 统计难题 字符匹配

    题目描述:先说明此题只有一个测试实例,然后输入一系列的单词,以一个回车为结束符,然后输入一个字符串,要你查找以这个字符串为前缀的单词的个数,处理到文件结束. 解题报告:一开始看到这题,竟然直接用暴力去 ...

  4. rsync更改端口后的同步办法

    rsync有两种常用的认证方式,一种为rsync-daemon方式,另外一种则是ssh. 在一些场合,使用rsync-daemon方式会比较缺乏灵活性,ssh方式则成为首选.但是今天实际操作的时候发现 ...

  5. Coins in a Line I & II

    Coins in a Line I There are n coins in a line. Two players take turns to take one or two coins from ...

  6. Linux6.5 安装Python3.X(转载)

    1.获取Python 3.6.3 通过官网https://www.python.org/downloads/下载Python 3.4.3源码: 源码获取命令如下:wget https://www.py ...

  7. springcloud使用Hystrix实现微服务的容错处理

    使用Hystrix实现微服务的容错处理 容错机制 如果服务提供者相应非常缓慢,那么消费者对提供者的请求就会被强制等待,知道提供者相应超时.在高负载场景下,如果不作任何处理,此类问题可能会导致服务消费者 ...

  8. JavaEE-案例1-网站信息展示

    案例1-网站信息展示 需求: 在页面上展示一些文字信息,需要排版. 技术分析: html:超文本 标签 语言 作用:展示 超文本:超越了一般文本,描述文本的字体.颜色.图片 标签:标记 html书写规 ...

  9. Java 基本语法---Java数组

    Java 基本语法---Java数组 0. 概述 数组:相同类型的数据 按照顺序 组成的一种 引用数据类型 . 数据类型:基本数据类型 + 引用数据类型: 引用数据类型:类 + 接口 + 数组 : 一 ...

  10. 一个浏览器Fuzzing框架的学习

    一个浏览器Fuzzing框架的学习 关于框架 之前是LCatro师傅在小密圈分享的他写的这个Fuzzing框架(不过我以前翻github时好像就看到过),但是之前一直没啥时间搞这方面,这两天研究学习了 ...