CodeForces – 711C

https://www.luogu.com.cn/problem/CF711C

题目描述

ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park where nn trees grow. They decided to be naughty and color the trees in the park. The trees are numbered with integers from 11 to nn from left to right.

Initially, tree ii has color c_{i}ci​ . ZS the Coder and Chris the Baboon recognizes only mm different colors, so 0<=c_{i}<=m0<=ci​<=m , where c_{i}=0ci​=0 means that tree ii is uncolored.

ZS the Coder and Chris the Baboon decides to color only the uncolored trees, i.e. the trees with c_{i}=0ci​=0 . They can color each of them them in any of the mm colors from 11 to mm . Coloring the ii -th tree with color jj requires exactly p_{i,j}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 nn 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,32,1,1,1,3,2,2,3,1,3 , the beauty of the coloring is 77 , since we can partition the trees into 77 contiguous groups of the same color : {2},{1,1,1},{3},{2,2},{3},{1},{3}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 kk . 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.

输入格式

The first line contains three integers, nn , mm and kk ( 1<=k<=n<=1001<=k<=n<=100 , 1<=m<=1001<=m<=100 ) — the number of trees, number of colors and beauty of the resulting coloring respectively.

The second line contains nn integers c_{1},c_{2},…,c_{n}c1​,c2​,…,cn​ ( 0<=c_{i}<=m0<=ci​<=m ), the initial colors of the trees. c_{i}ci​ equals to 00 if the tree number ii is uncolored, otherwise the ii -th tree has color c_{i}ci​ .

Then nn lines follow. Each of them contains mm integers. The jj -th number on the ii -th of them line denotes p_{i,j}pi,j​ ( 1<=p_{i,j}<=10^{9}1<=pi,j​<=109 ) — the amount of litres the friends need to color ii -th tree with color jj . p_{i,j}pi,j​ ‘s are specified even for the initially colored trees, but such trees still can’t be colored.

输出格式

Print a single integer, the minimum amount of paint needed to color the trees. If there are no valid tree colorings of beauty kk , print -1−1 .

题意翻译

题意:

有 nn 棵树, mm 种颜料,要求现在要给这些树涂上颜料,最后涂成 kk 段(连续颜色相同划为一段如 22 , 11 , 11 , 11 , 33 , 22 , 22 , 33 , 11 , 33 是77段),有些树已经涂了,则不涂了只能涂一次,输入nn个数(每个数为00~mm),0表示还没有涂,11~mm表示已经涂了哪种颜料。接下来输入 nn 行 mm 列,表示每棵树涂成每种颜色所要的颜料量。现在要把所有树都涂上颜料涂成 kk 段,求最少要用的颜料量

翻译 byby @Happynewyear

输入输出样例

输入 #1复制

3 2 2
0 0 0
1 2
3 4
5 6

输出 #1复制

10

输入 #2复制

3 2 2
2 1 2
1 3
2 4
3 5

输出 #2复制

-1

输入 #3复制

3 2 2
2 0 0
1 3
2 4
3 5

输出 #3复制

5

输入 #4复制

3 2 3
2 1 2
1 3
2 4
3 5

输出 #4复制

0

说明/提示

In the first sample case, coloring the trees with colors 2,1,12,1,1 minimizes the amount of paint used, which equals to 2+3+5=102+3+5=10 . Note that 1,1,11,1,1 would not be valid because the beauty of such coloring equals to 11 ( {1,1,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 33 , so there is no valid coloring, and the answer is -1−1 .

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


思路:一道线性dp的好题目。

定义:dp[i][j][k]为前i个点分成j段并且最后一段颜色是k的最小代价

当第i个点是有颜色的。

如果和前面一个点颜色一致,dp[i][j][l]=min(dp[i][j][l],dp[i-1][j][l]);

如果和前面一个点的颜色不一致,dp[i][j][col[i]]=min(dp[i][j][col[i]],dp[i-1][j-1][l]);

如果第i个点是无颜色的

如果涂上和前面一个点不一致的颜色。dp[i][j][l]=min(dp[i][j][l],d[i-1][j-1][p]+w[i][l]);

如果涂上和前面一个点一致的颜色。dp[i][j][l]=min(dp[i][j][l],dp[i-1][j][p]+w[i][l]);

注意dp的初始化开大一点。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<cstdio>
#include<algorithm>
#define inf 0x7f7f7f7f7f7f
using namespace std;
typedef long long LL;
const int maxn=200;
LL dp[maxn][maxn][maxn];//dp[i][j][k]:前i棵树分成j段且最后一段颜色为k时的最小花费
LL w[maxn][maxn];
LL col[maxn];
LL n,m,k;
int main(void)
{cin.tie(0);std::ios::sync_with_stdio(false);cin>>n>>m>>k;for(LL i=1;i<=n;i++) cin>>col[i];for(LL i=1;i<=n;i++){for(LL j=1;j<=m;j++)cin>>w[i][j];}memset(dp,inf,sizeof(dp));if(col[1]) dp[1][1][col[1]]=0;else{for(LL i=1;i<=m;i++) dp[1][1][i]=w[1][i];    }for(LL i=2;i<=n;i++){for(LL j=1;j<=i&&j<=k;j++){if(col[i]){//第i个点有颜色for(LL l=1;l<=m;l++){if(col[i]==l){dp[i][j][l]=min(dp[i][j][l],dp[i-1][j][l]);//同样颜色为连续一段 }else dp[i][j][col[i]]=min(dp[i][j][col[i]],dp[i-1][j-1][l]);//不同颜色为新开一段 }}else{for(LL l=1;l<=m;l++){for(LL p=1;p<=m;p++){if(l==p) dp[i][j][l]=min(dp[i][j][l],dp[i-1][j][p]+w[i][l]);//一样颜色就连续一段 else dp[i][j][l]=min(dp[i][j][l],dp[i-1][j-1][p]+w[i][l]);//不一样颜色就另开一段 }}} }  }LL ans=inf;if(col[n]) ans=dp[n][k][col[n]];else {for(LL l=1;l<=m;l++){ans=min(ans,dp[n][k][l]);}} if(!(ans<inf)) cout<<"-1"<<endl;else cout<<ans<<endl;
return 0;
}

Coloring Trees相关推荐

  1. CodeForces 711C - Coloring Trees DP

    /*http://codeforces.com/problemset/problem/711/C http://codeforces.com/blog/entry/46830官方题解:We compu ...

  2. CodeForces 711C.Coloring Trees【DP】

    看上去就是DP的一个题,由于自己太菜了,还是不会做 先给个提交的地方:cf 711C 这个题看到数据,很明显是dp,因为n,m,k的值都不大,我们可以建立矩阵来推理 很明显答案跟dp[n][k]有关 ...

  3. codeforces 711 C. Coloring Trees (dp)

    题目:http://codeforces.com/group/1EzrFFyOc0/contest/711/problem/C 题意:有n棵树,m种颜料,部分树有初始颜色,如为0,则没有初始颜色,给树 ...

  4. CodeForces 711C Coloring Trees (三维DP)

    题目链接:http://codeforces.com/problemset/problem/711/C DP #include<bits/stdc++.h> using namespace ...

  5. CF 711C Coloring Trees(三维dp)

    原题地址: https://codeforces.com/problemset/problem/711/C 题意:有nnn棵树,mmm种颜色,让你分成kkk段,数字000代表该树没没有进行染色,需要你 ...

  6. codeforces 711C Coloring Trees(DP)

    题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...

  7. python基础刷题_数据结构与算法LeetCode刷题(Python)

    参考资料: 一.链表 1.  链表的必备知识要点(包括基础知识.刷题中使用的STL等知识) 2.  链表逆序(LeetCode 92 ,206. Reverse Linked List 1,2) 3. ...

  8. 2016区域赛前冲刺训练

    UPD 2016.10.23 shift-and (2题) Codeforces 训练 现在已经完成了: 191 [Codeforces Round #377] (6/6) Div 2 A Buy a ...

  9. 训练 CF 1700分 题解

    C o d e F o r c e s 264 B CodeForces 264B CodeForces264B G o o d S e q u e n c e s Good Sequences Go ...

  10. 面试算法LeetCode刷题班—BAT面试官带你刷真题、过笔试

    课程名称: <面试算法LeetCode刷题班> --BAT面试官带你刷真题.过笔试 主讲老师: 林老师 BAT资深研发工程师(T7/P8级),致力于搜索引擎及其子系统的研发.迭代与优化,数 ...

最新文章

  1. HBuilder简单入门
  2. gear s3刷android wear,3星gear|3星gear s3自动选择手表刷机办法图文详细教程以及风险介绍...
  3. OC实例变量和属性-@synthesize与@property
  4. idea 添加 VUE 的语法
  5. 微信亿级用户异常检测框架的设计与实践
  6. arm中的.a文件如何产生的_如何在IPFS中Pin一个文件?
  7. 21天Jenkins打卡Day7-打包git代码
  8. 用tinypng插件创建gulp task压缩图片
  9. winform 显示分隔控件_(八十)c#Winform自定义控件-分割线标签-HZHControls
  10. 解析6种常用View 的滑动方法
  11. 广数系统加工中心编程_数控加工中心编程技巧一文通
  12. 元子弹老师-吉他指弹左手技巧
  13. QT-使用QT资源文件添加菜单栏、工具栏图标
  14. 《SysML精粹》学习记录--第三章
  15. 【校园网环境下知网研学下载文献出现“下载文献 当前IP没有获取权限或服务器异常”处理办法】
  16. 在单个虚拟机中搭建DPDK测试环境
  17. 解决启动IIS发生意外错误 0x8ffe2740
  18. 解决_CRT_SECURE_NO_WARNINGS 警告
  19. Java 求一个数的立方根
  20. python财务报表书籍_清华大学出版社-图书详情-《从零开始学看财报(白金版)》...

热门文章

  1. 读研究生的目的之我见
  2. 深入了解Element Form表单动态验证问题
  3. 从0到1的电商架构应该怎么做?有哪些坑?
  4. Android屏幕亮度调节
  5. 镁光闪存颗粒对照表_最全的内存颗粒编码规则说明,教你看穿内存条到底用的什么颗粒...
  6. Linux系统 查看 Vendor id 和Device id
  7. 马云将成全球第11大富豪,很好奇第一位是谁?
  8. C语言键盘控制走迷宫小游戏
  9. Perl的中国镜像网站--下载速度更快
  10. 沉浸式体验娱乐,通往元宇宙之路?