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 ...
随机推荐
- scrollTo(String text) and scrollToExact(String text) method of Android Driver not working
Using the scrollTo(String text) and scrollToExact(String text) method of Android Driver. However the ...
- android自己定义开关控件
近日在android项目要使用开关控件.可是android中自带的开关控件不太惬意,所以就打算通过自己定义View写一个开关控件 ios的开关控件当然就是我要仿照的目标. 先上图: waterma ...
- 使用std::mutex取代QMutex
为了保证对某个资源的操作是原子性的(对资源读写时,只有当前的操作结束,才允许另外线程对其操作,这里有个理解误区,资源操作原子性不是说,当前某个线程获得了某个资源使用权,然后线程执行时间完毕,要切换线程 ...
- 微信小程序template使用
当您的项目需要多次使用同一个布局和样式的时候,您就可以考虑使用template(模板)来减少冗余代码. 使用方式: 1.新建一个template文件夹来存放您的通用模板: 2.在文件夹里面新建一个wx ...
- (linux)SD卡初始化-mmc_sd_init_card函数
为了学习SD/SDIO协议,看了一下linux中初始化SD卡的流程,结合代码更容易SD初始化是怎么做的. 下面图截自:"SD Specifications Part 1 Physical ...
- ADB结构及代码分析【转】
本文转载自:http://blog.csdn.net/happylifer/article/details/7682563 最近因为需要,看了下adb的源代码,感觉这个作者很牛,设计的很好,于是稍微做 ...
- chmod更改文件的权限
#include "apue.h" int main(int argc,char *argv[]) { struct stat stabuf; ) err_sys("st ...
- HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流
题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others) ...
- Spring Boot2.0之 整合Redis集群
项目目录结构: pom: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...
- 【Selenium】测试流程和框架
流程: 分析自动化测试需求→制定自动化测试计划→设计自动化测试用例→搭建环境→编写脚本→分析结果→维护脚本 框架: 线性测试.模块化测试.数据驱动.关键字驱动