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的更多相关文章

  1. codeforces 711C C. Coloring Trees(dp)

    题目链接: C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  2. 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 ...

  3. Codeforces Round #369 (Div. 2) C. Coloring Trees 动态规划

    C. Coloring Trees 题目连接: http://www.codeforces.com/contest/711/problem/C Description ZS the Coder and ...

  4. Codeforces 677C. Coloring Trees dp

    C. Coloring Trees time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  5. 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 ...

  6. CodeForces #369 C. Coloring Trees DP

    题目链接:C. Coloring Trees 题意:给出n棵树的颜色,有些树被染了,有些没有.现在让你把没被染色的树染色.使得beauty = k.问,最少使用的颜料是多少.   K:连续的颜色为一组 ...

  7. Code Forces 711C Coloring Trees

    C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. 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 ...

  9. C. Coloring Trees DP

    传送门:http://codeforces.com/problemset/problem/711/C 题目: C. Coloring Trees time limit per test 2 secon ...

随机推荐

  1. jmeter3.0_bodydata中存在中文乱码

    jmeter3.0_bodydata中存在中文乱码 1.进入jmeter.properties配置文件 找到#jsyntaxtextarea.font.family=Hack ,并将“#”取消并重启j ...

  2. android keyEvent

    http://developer.android.com/reference/android/view/KeyEvent.html

  3. 利用PHP判断iPhone、iPad、Android、PC设备

    首页那张大图确实是一个比较头疼的问题 在PC上显示是没问题的,可是到手机上就会超出页面一大截,如果做自适应,图片会被强制压缩 无奈只能用wp_is_mobile()函数在手机上隐藏了这张图,可是这函数 ...

  4. STL review:vector & string & map & struct

    I.vector 1.头文件:#include<vector>                        //容器vector是一个能实现随机存取.插入删除的动态数组,还可以当栈使. ...

  5. NullpointerException真的一定要被预防?

    毫无疑问,空指针NullpointerException是我们最常遇到异常,没有之一! 在刚进入编程职业时,我想,大部分进入的同学肯定会受到前辈们的叮咛:一定要防止空指针,这是个低级错误.你们不是?好 ...

  6. Why there are no job running on hadoop

    Using hadoop1.3.0. I ran the example WordCount correctly in eclipse. But when I enter localhost:5003 ...

  7. strncpy和strlen的可能的实现

    #include <stdio.h> #include <stdlib.h>//为避免与标准库中的函数发生混淆,我将它们命名为stringNCopy和stringLength ...

  8. JSON与localStorage的爱恨情仇

    在使用localStorage时,我们会给一个key存取一个value,这个value可以是一个普通的字符串,也可以是一个对象,如果是一个字符串,我们就需要通过JSON.stringify来转化为JS ...

  9. ios很好的开源库

    Tim9Liu9/TimLiu-iOS 自己总结的iOS.mac开源项目及库,持续更新.. 目录 UI 下拉刷新 模糊效果 AutoLayout 富文本 图表 表相关与Tabbar 隐藏与显示 HUD ...

  10. lovelygallery_popup(卡哇依相册)

    /*************************** 相册 ***************************/LovelyGallery 功能特点:超过200个令人惊叹的3D&2D硬 ...