Coloring Trees CodeForces - 711C
Coloring Trees CodeForces - 711C
题意:有n个点,每个点有一个c值,如果为0表示它没有被染色,否则表示它被染成了c值的颜色。颜色有1到m。把第i棵树染成颜色j所需要的代价是p[i][j]。求最小的代价,使得将每棵树都染色,且如果将连续的一串同色的树视为一个集合,共有k个集合。输出代价,如果不可能达到要求输出-1。
方法:
$ans[i][j][k]$表示把前i棵树染好,并且最后一种颜色是j,并且总共有k段的最小费用。
首先初始化ans全部值为一个很大的树(方便min),然后$ans[0][1..m][0]=0$
c[i]=0时:更新每个j
$ans[i][j][k]=min(ans[i-1][j][k]+p[i][j],min\{ans[i-1][q][k-1]+p[i][j] | 1<=q<=m\})$
也就是$ans[i][j][k]=min(ans[i-1][j][k],min\{ans[i-1][q][k-1] | 1<=q<=m\})+p[i][j]$
c[i]=1时:只更新$j=c[i]$
$ans[i][j][k]=min(ans[i-1][j][k],min\{ans[i-1][q][k-1] | 1<=q<=m\})$
注意:i=1也就是第一棵树需要特判。可以在循环里加条件/在0加值/把第一棵树拉出来单独处理。
#include<cstdio>
#include<cstring>
typedef long long LL;
LL ans[][][];
LL p[][];
LL c[];
LL n,m,k2,minans=0x3f3f3f3f3f3f3f3f;
LL min(LL a,LL b)
{
return a>b?b:a;
}
int main()
{
LL i,j,k,q;
scanf("%I64d%I64d%I64d",&n,&m,&k2);
for(i=;i<=n;i++)
scanf("%I64d",&c[i]);
for(i=;i<=n;i++)
for(j=;j<=m;j++)
scanf("%I64d",&p[i][j]);
memset(ans,0x3f,sizeof(ans));
for(i=;i<=m;i++)
ans[][i][]=;
for(i=;i<=n;i++)
{
if(c[i]==)
{
for(j=;j<=m;j++)
for(k=;k<=k2;k++)
{
ans[i][j][k]=ans[i-][j][k];
for(q=;q<=m;q++)
if(q!=j||i==)
ans[i][j][k]=min(ans[i][j][k],ans[i-][q][k-]);
ans[i][j][k]+=p[i][j];
}
}
else
{
j=c[i];
for(k=;k<=k2;k++)
{
ans[i][j][k]=ans[i-][j][k];
for(q=;q<=m;q++)
if(q!=j||i==)
ans[i][j][k]=min(ans[i][j][k],ans[i-][q][k-]);
}
}
}
for(i=;i<=m;i++)
minans=min(ans[n][i][k2],minans);
if(minans==0x3f3f3f3f3f3f3f3f)
printf("-1");
else
printf("%I64d",minans);
return ;
}
Coloring Trees CodeForces - 711C的更多相关文章
- codeforces 711C C. Coloring Trees(dp)
题目链接: C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 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 动态规划
C. Coloring Trees 题目连接: http://www.codeforces.com/contest/711/problem/C Description ZS the Coder and ...
- Codeforces 677C. 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
C. Coloring Trees ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the pa ...
- CodeForces #369 C. Coloring Trees DP
题目链接:C. Coloring Trees 题意:给出n棵树的颜色,有些树被染了,有些没有.现在让你把没被染色的树染色.使得beauty = k.问,最少使用的颜料是多少. K:连续的颜色为一组 ...
- Code Forces 711C Coloring Trees
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)
C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- C. Coloring Trees DP
传送门:http://codeforces.com/problemset/problem/711/C 题目: C. Coloring Trees time limit per test 2 secon ...
随机推荐
- C/C++语言中的位运算
在计算机程序中,数据的位是可以操作的最小数据单位,理论上可以用“位运算”来完成所有的运算和操作. 一般的位操作是用来控制硬件的,或者做数据变换使用,但是,灵活的位操作可以有效地提高程序运行的效率.C语 ...
- Thread Runnable 区别
[线程的并发与并行] 在单CPU系统中,系统调度在某一时刻只能让一个线程运行,虽然这种调试机制有多种形式(大多数是时间片轮巡为主),但无论如何,要通过不断切换需要运行的线程让其运行的方式就叫并发(co ...
- Hadoop如何计算map数和reduce数
阅读本文可以带着下面问题: 1.map和reduce的数量过多会导致什么情况? 2.Reduce可以通过什么设置来增加任务个数? 3.一个task的map数量由谁来决定? 4.一个task的reduc ...
- Jmeter代理服务器录制请求
1.文档前提说明 1)本文使用jmeter的版本为 apache-jmeter-2.13 及以上版本 2)java版本要求在 1.8.0 以上 注:jmeter版本一般和java相应的版本一起使用,如 ...
- js中一些常见写法的含义
1. 常见格式:(function() { /* code */ })(); 解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括号 ...
- POJ2752 Seek the Name, Seek the Fame —— KMP next数组
题目链接:https://vjudge.net/problem/POJ-2752 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Li ...
- DedeCms如何调用Discuz论坛主题等数据方法总结
DedeCms如何调用Discuz论坛主题等数据方法总结 同时使用Dedecms和Discuz论坛的朋友,难免要在网站内调用论坛的内容.使用Discuz论坛的JS调用方式,对搜索引擎不够友好,下面我们 ...
- INFO: Ignoring response <403 https://movie.douban.com/top250>: HTTP status code is not handled or not allowed
爬取豆瓣电影top250,出现以下报错: 2018-08-11 22:02:16 [scrapy.core.engine] INFO: Spider opened 2018-08-11 22:02:1 ...
- DBA之RMAN备份
13:00 backup database backup db :3h 3h: 产生了10 archive log file 16:00 finish restore database; 13 ...
- 【opencv】opencv在图片、视频嵌中英文字符的方法
转自:http://www.cnblogs.com/hujingshuang/p/5119015.html 说明:本博文是根据前人已有的成果并结合自己的理解而成的.为了避免让读者感到繁琐,我将运用小学 ...