Codeforces Round #369 (Div. 2) C. Coloring Trees 动态规划
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 动态规划的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees(简单dp)
题目:https://codeforces.com/problemset/problem/711/C 题意:给你n,m,k,代表n个数的序列,有m种颜色可以涂,0代表未涂颜色,其他代表已经涂好了,连着 ...
- Codeforces Round #369 (Div. 2)-C Coloring Trees
题目大意:有n个点,由m种颜料,有些点没有涂色,有些点已经涂色了,告诉你每个点涂m种颜色的价格分别是多少, 让你求将这n个点分成k段最少需要多少钱. 思路:动态规划,我们另dp[ i ][ j ][ ...
- 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 ...
- 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 ...
- Codeforces Round #106 (Div. 2) D. Coloring Brackets 区间dp
题目链接: http://codeforces.com/problemset/problem/149/D D. Coloring Brackets time limit per test2 secon ...
随机推荐
- 阿里云centos7.3安装lamp环境
参考文档:http://www.jb51.net/article/96649.htm http://m.blog.csdn.net/qq_33813365/article/details/766337 ...
- Gnucash数据库结构
- phpStorm 8.0.3 设置
phpstorm 8 license key Learn Programming===== LICENSE BEGIN =====63758-1204201000000Ryqh0NCC73lpRm!X ...
- spring Mvc + Maven + 拷贝插件 (十一)
maven-antrun-plugin:可用于在项目编译打包时,把文件指定的文件拷贝到指定的位置,我们打包一般都是打包到 项目 的target 文件下; <groupId>org.apac ...
- Guava BiMap
BiMap主要的就是用于key,value的互相映射,获取相互的结果,还保证值value是唯一的,key相同覆盖原来值. 举例: BiMap<Integer, String> empIDN ...
- 20145234黄斐《Java程序设计》第八周
教材学习内容总结 第十四章-NIO与NIO2 NIO与IO的区别 NIO Channel继承框架 想要取得Channel的操作对象,可以使用Channels类,它定义了静态方法newChannel() ...
- 从字节码的角度看Java内部类与外部类的互相访问
Java中non-static内部类为何可以访问外部类的变量?Java中外部类又为何可以访问内部类的private变量?这两个问题困扰过我一段时间,查了一些网上的答案,大多从“闭包”概念入手,理解起来 ...
- CasperJS断言
特征 CasperJS具有一系列特征. 它具有一些列功能与断言,都是你期望一个好的测试API所具有的,包括: * assertTextExists (文本存在断言)* assertTitle (标题断 ...
- Python查找算法之 -- 列表查找和二分查找
一.列表查找:从列表中查找指定元素 输入:列表.待查找元素 输出:元素下标或未查找到元素 二.列表查找方式 顺序查找 : 从列表的第一个元素开始遍历,知道找到为止.时间复杂度O(n) 二分查找 :从有 ...
- Chrome插件 postman的使用方法详解!最全面的教程
一 简介 Postman 是一款功能超级强大的用于发送 HTTP 请求的 Chrome插件 .做web页面开发和测试的人员应该是无人不晓无人不用!其主要特点 特点: 创建 + 测试:创建和发送任何的H ...