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 ...
随机推荐
- 【bzoj1433】[ZJOI2009]假期的宿舍
按要求连边,跑匈牙利 #include<algorithm> #include<iostream> #include<cstdlib> #include<cs ...
- python dictionary的遍历
d = {'x':1, 'y':3, 'z':2} for k in d: print d[k] 直接遍历k in d的话,遍历的是dictionary的keys. 2 字典的键可以是任何不可变 ...
- 51nod 1600 Simple KMP
又被机房神犇肉丝哥哥和glory踩爆了 首先这个答案的输出方式有点套路,当前的答案=上一个答案+每一个后缀的f值=上一个答案+上一次算的每个后缀的f值+当前每个后缀的深度 这个题意给了个根深度为-1有 ...
- oracle:数据库版本问题导致的bug
公司开发出来的系统,由于各现场oracle数据库版本有10.2.0.4.11.2.0.1.11.2.0.3.11.2.0.4: 进而会导致版本不一导致错误问题.下面列举2个: 1.wm_concat ...
- Oracle中如何用SQL检测字段是否包括中文字符
用Oracle的编码转换的函数Convert实现,测试后可行. SQL> select * 2 from (select 'abcd' c1 from dual 3 ...
- 内部类 final变量的生命周期
(1).内部类是外部类的一个成员,就像外部类的成员方法一样,所以内部类有权限访问外部类的所有成员,包括private的. (2).内部类不能访问外部类方法中的局部变量,除非变量是final的(一般发生 ...
- 书写优雅的shell脚本(插曲)- /proc
1. /proc目录 Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...
- ssl原理及应用
今天学习网络通信,看到使用ssl(Secure Sockets Layer)进行加密,由于对ssl只是有些概念上的了解,对于具体应用原理.过程和如何使用不慎了解,于是学习了一番,总结如下: 1. 为什 ...
- 90年代经典“手游”—拼图板小游戏Opencv实现
80后可能还对儿时玩过的一种经典木质的拼图板游戏记忆犹新,一般是一种4*4或5*5规格的手持活动板,通过挪动每个小板子的位置,拼出来板子上完整的图像,那时候还没有网吧,手机也还是大哥大的天下,所以这也 ...
- bzoj1207 [HNOI2004]打鼹鼠——LIS
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1207 这题和求LIS有点像,打这一只鼹鼠一定可以从打上一只鼹鼠转移过来: 所以不用考虑机器人 ...