简单$dp$。

$dp[i][j][k]$表示:前$i$个位置染完色,第$i$个位置染的是$j$这种颜色,前$i$个位置分成了$k$组的最小花费。总复杂度$O({n^4})$。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-; LL INF=1e17;
LL dp[][][];
LL p[][];
int c[],n,m,k; int main()
{
scanf("%d%d%d",&n,&m,&k); for(int i=;i<=n;i++) scanf("%d",&c[i]); for(int i=;i<=n;i++)
for(int j=;j<=m;j++) scanf("%lld",&p[i][j]); for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
for(int s=;s<=k;s++)
{
dp[i][j][s]=INF;
}
}
} if(c[]!=) dp[][c[]][]=;
else
{
for(int j=;j<=m;j++) dp[][j][]=p[][j];
} for(int i=;i<=n;i++)
{
if(c[i]==)
{
for(int j=;j<=m;j++)
{
for(int s=;s<=i;s++)
{
LL tmp=INF;
tmp=min(tmp,dp[i-][j][s]);
for(int t=;t<=m;t++)
{
if(t==j) continue;
tmp=min(tmp,dp[i-][t][s-]);
}
if(tmp==INF) continue;
else dp[i][j][s]=tmp+p[i][j];
}
}
} else
{
for(int s=;s<=i;s++)
{
LL tmp=INF;
tmp=min(tmp,dp[i-][c[i]][s]);
for(int t=;t<=m;t++)
{
if(t==c[i]) continue;
tmp=min(tmp,dp[i-][t][s-]);
} if(tmp==INF) continue;
dp[i][c[i]][s]=tmp;
}
}
} LL ans=INF;
for(int j=;j<=m;j++) ans=min(ans,dp[n][j][k]); if(ans==INF) printf("-1\n");
else printf("%lld\n",ans); return ;
}

CodeForces 711C Coloring Trees的更多相关文章

  1. codeforces 711C Coloring Trees(DP)

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

  2. 【动态规划】Codeforces 711C Coloring Trees

    题目链接: http://codeforces.com/problemset/problem/711/C 题目大意: 给N棵树,M种颜色,已经有颜色的不能涂色,没颜色为0,可以涂色,每棵树I涂成颜色J ...

  3. CodeForces 711C Coloring Trees (DP)

    题意:给定n棵树,其中有一些已经涂了颜色,然后让你把没有涂色的树涂色使得所有的树能够恰好分成k组,让你求最少的花费是多少. 析:这是一个DP题,dp[i][j][k]表示第 i 棵树涂第 j 种颜色恰 ...

  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. Code Forces 711C Coloring Trees

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

  6. Coloring Trees CodeForces - 711C

    Coloring Trees CodeForces - 711C 题意:有n个点,每个点有一个c值,如果为0表示它没有被染色,否则表示它被染成了c值的颜色.颜色有1到m.把第i棵树染成颜色j所需要的代 ...

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

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

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

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

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

随机推荐

  1. POJ 1002 UVA 755 487--3279 电话排序 简单但不容易的水题

    题意:给你许多串字符串,从中提取电话号码,输出出现复数次的电话号码及次数. 以下是我艰难的AC历程:(这题估计是我刷的题目题解次数排前的了...) 题目不是很难理解,刚开始想到用map,但stl的ma ...

  2. C++构造 下一个排列 的函数

    今天围观刘汝佳神犇的白书发现了一个好用的函数: next_permutation(); 可以用于可重, 或者不可重集, 寻找下一个排列. 时间复杂度尚不明. //适用于不可重和可重集的排列. # in ...

  3. 栈和队列简单的STL模板

    栈的使用,可以想象成是只有一个出口,最后进来的那个最先出去: #include <stack> 队列:是有两个出口,但是进来了之后只能从前门出去,也就是最先进来的那个最后出去: #incl ...

  4. Oracle常用操作

      比较时间 select * from up_date where update < to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss' ...

  5. 强制卸载VS2013

    最近因为VS2013闹许可证过期问题,加之又发现新版本update5,所以就想卸掉重装,但是按照正常的卸载,发现卸载不了,再后来force强制卸载,OK了. force强制卸载: 首先如果安装了中文包 ...

  6. CSS中文字体的英文名称 – 前台开发必备

    做什么用的?写过CSS的都晓得,一般用在font-family后面——为什么不用中文呢?有过一定开发经验的都晓得CSS里面用中文也是会乱码的,特别是没有中文字符集的浏览器,直接成了框框,用英文就可以解 ...

  7. iOS7 Sprite Kit 学习

    iOS7 Sprite Kit 学习 iOS 7有一个新功能 Sprite Kit 这个有点类似cocos2d 感觉用法都差不多.下面简单来介绍下Sprite Kit About Sprite Kit ...

  8. DAS,NAS,SAN在数据库存储上的应用

    DAS,NAS,SAN在数据库存储上的应用 一. 硬盘接口类型1. 并行接口还是串行接口(1) 并行接口,指的是并行传输的接口,比如有0~9十个数字,用10条传输线,那么每根线只需要传输一位数字,即可 ...

  9. Windows Store 手势编程小结

    Windows Store 手势编程小结 最近完成了一个Windows Store上面的手势操作的页面.在这里总结了一下经验和心得,希望能和大家一起分享和讨论一下. 首先,要纠正一个误区,在Windo ...

  10. 通过NSURL获取网络标准时间

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...