简单$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. 个人计算机安装hadoop全分布

    一.工具说明 设备:实体主机一台 校园网络 虚拟设备:VMware下安装的ubuntu12.04版本三台,分别是master,slave1,slave2 二.ubuntu安装篇 前提知识:在windo ...

  2. iOS国际化和genstrings所有子目录本地化字符串

    最近在忙着一个国外项目,天天加班,没时间更新博客.就简单讲讲国际化的知识. 首先,我们使用字符串,必须用NSLocalizedString(key,comment),这样我们在不同的Localizab ...

  3. BFS和DFS详解

    BFS和DFS详解以及java实现 前言 图在算法世界中的重要地位是不言而喻的,曾经看到一篇Google的工程师写的一篇<Get that job at Google!>文章中说到面试官问 ...

  4. iOS app 发布错误 ERROR ITMS-90167: "No .app bundles found in the package"

    今天iOS 上传 APP 突然发生了这个错误,在排查非证书错误后感到非常奇怪, 因为昨天刚刚上传了另一个APP,一切正常. 仔细回忆了下 昨天和今天唯一的不同就是我升级了 电脑操作系统至 macOS ...

  5. IceMx.Mvc 我的js MVC 框架六、完善植物大战僵尸(向日葵登场)

    有图有真相,废话不多说上图 看到园友的支持很受鼓舞,更觉得应该做下去,虽然自己是个菜鸟,但也应该共享自己的心得,只要有人获益那就是值得的. 我的下载需要csdn论坛的1个积分,之所以不完全免费出去是因 ...

  6. 工具条OutLookBar

    工具条OutLookBar 灰姑娘本身也有自已的优点,但是却可能因为外貌不讨人喜欢,要变成白雪公主却需要有很多勇气和决心去改变自已: 有一颗善良的心 讨人喜爱的外貌 我这里讲的是一个工具条的蜕变过程, ...

  7. hdu 1559 最大子矩阵(DP)

    题目链接:点击链接 #include<stdio.h> #include<string.h> #define max(a,b) a>b?a:b int d[1005][1 ...

  8. linux iptable 设置实践

    下面是设置网络时的基本状况: 主机3个网卡: eth0 192.168.0.1/24   内网 eth1 192.168.20.1/24  外网 eth2 192.168.50.1/24  会议室网络 ...

  9. OC之类与对象

    1.面向过程与面向对象. 1). 完成需求1 将大象放进冰箱. a. 把冰箱门打开. b. 把大象放进去. c. 把冰箱门关上. 这是面向过程的思路. 找1个冰箱,要求这个冰箱可以自己开门,自己把大象 ...

  10. [ios2] 获取mac地址 等唯一标识

    - (NSString *) macaddress{        int                 mib[6];    size_t              len;    char    ...