CodeForces 711C Coloring Trees
简单$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的更多相关文章
- codeforces 711C Coloring Trees(DP)
题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...
- 【动态规划】Codeforces 711C Coloring Trees
题目链接: http://codeforces.com/problemset/problem/711/C 题目大意: 给N棵树,M种颜色,已经有颜色的不能涂色,没颜色为0,可以涂色,每棵树I涂成颜色J ...
- CodeForces 711C Coloring Trees (DP)
题意:给定n棵树,其中有一些已经涂了颜色,然后让你把没有涂色的树涂色使得所有的树能够恰好分成k组,让你求最少的花费是多少. 析:这是一个DP题,dp[i][j][k]表示第 i 棵树涂第 j 种颜色恰 ...
- Codeforces 677C. Coloring Trees dp
C. Coloring Trees time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- Code Forces 711C Coloring Trees
C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Coloring Trees CodeForces - 711C
Coloring Trees CodeForces - 711C 题意:有n个点,每个点有一个c值,如果为0表示它没有被染色,否则表示它被染成了c值的颜色.颜色有1到m.把第i棵树染成颜色j所需要的代 ...
- 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 ...
随机推荐
- 用Haproxy给MySQL做负载均衡
1.安装 # wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz # tar zcvf haproxy-1.4.24.t ...
- UNIX基础知识--<<UNIX 环境编程>>读书笔记
1 shell程序就是位于应用软件与系统调用之间的程序 每个用户登录系统,系统就会为用户分配shell (用户的登录的口令文件 在 /etc/passwd 2 ls filename 运行原理 ...
- 应用facebook .net sdk
1.本博客主要介绍如何应用facebook .net SDK,实现发帖.点赞.上传照片视频等功能,更多关于facebook API,请参考:https://developers.facebook.co ...
- TOGAF架构能力框架之架构委员会和架构合规性
TOGAF架构能力框架之架构委员会和架构合规性 3. 架构委员会 正如前面所说,一个用来对架构治理策略的实现进行监督的跨组织的架构委员会是架构治理策略成功的主要要素之一.架构委员会应该能够代表所有主要 ...
- Object-c学习之路四(oc内存管理autorelease)
再以Student和Book为例作为展示: 1.主函数: // main.m // MemoryManagement2 // // Created by WildCat on 13-7-24. // ...
- jQuery特殊符号转义
我们在使用jquery选择器的时候 对一些ID属性中有特殊符号的地方需要进行转义. 列举部分如下: <input id="entity.username" type=&quo ...
- Spring Data Rest 支持Put请求
最近在用 Spring Data Rest 写WebApi 遇到Put请求后报415 Mieda Type 的错误,GG了下 找到个解决办法,下面是针对Put请求的过滤. 1 public class ...
- Castle Windsor 项目中快速使用
Castle Windsor 项目中快速使用 新建项目如下: 一个模型类,一个接口,一个实现方法.我的目的很明确就是在UI层通过Castle 调用数据访问层的方法. 添加项目引用 CastleDemo ...
- DotNet加密方式解析--对称加密
离过年又近了一天,回家已是近在咫尺,有人欢喜有人愁,因为过几天就得经历每年一度的装逼大戏,亲戚朋友加同学的各方显摆,所以得靠一剂年终奖来装饰一个安稳的年,在这里我想起了一个题目“论装逼的技术性和重要性 ...
- WPF 自定义数字文本框:NumericBox
由于项目需要,最近写了一个数字输入文本框,在此作个备忘. 1.代码调用 <controls:NumericBox Height="32" Width="80&quo ...