Codeforces Round #369 (Div. 2) C. Coloring Trees(简单dp)
题目:https://codeforces.com/problemset/problem/711/C
题意:给你n,m,k,代表n个数的序列,有m种颜色可以涂,0代表未涂颜色,其他代表已经涂好了,连着一段是相同颜色的是一个连通块,求正好有k个连通块的最小花费
思路:首先每个位置有可能有m中颜色,而且要满足k个,我们我们可以推出所有情况
dp[n][m][k]
n代表前n个数
m代表当前涂m色
k代表满足k个了

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
#include<string>
#include<cstring>
#include<iostream>
#include<vector>
#define mod 1000000007
#define maxn 200005
#define INF 100000000000000+10
using namespace std;
typedef long long ll;
ll n,m,k;
ll a[];
ll c[][];
ll dp[][][];
int main()
{
cin>>n>>m>>k;
for(int i=;i<=n;i++){
cin>>a[i];
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin>>c[i][j];
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
for(int z=;z<=k;z++)
dp[i][j][z]=INF;
}
}
if(a[]){
dp[][a[]][]=;
}
else{
for(int j=;j<=m;j++){
dp[][j][]=c[][j];
}
}
for(int i=;i<=n;i++)
{
if(!a[i])//不是0的时候
{
for(int j=;j<=m;j++)
for(int k=;k<=i;k++)
for(int h=;h<=m;h++)
{
if(j==h)
dp[i][j][k]=min(dp[i][j][k],dp[i-][h][k]+c[i][j]);//颜色相同情况
else
dp[i][j][k]=min(dp[i][j][k],dp[i-][h][k-]+c[i][j]);//颜色不相同
}
}
else
{
for(int k=;k<=i;k++)
for(int h=;h<=m;h++)
{
if(a[i]==h)
dp[i][a[i]][k]=min(dp[i][a[i]][k],dp[i-][a[i]][k]);//同上
else
dp[i][a[i]][k]=min(dp[i][a[i]][k],dp[i-][h][k-]);
}
}
}
ll mn=INF;
for(int i=;i<=m;i++){
mn=min(mn,dp[n][i][k]);
}
if(mn>=INF) printf("-1");
else cout<<mn;
}
Codeforces Round #369 (Div. 2) C. Coloring Trees(简单dp)的更多相关文章
- 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 (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 动态规划
C. Coloring Trees 题目连接: http://www.codeforces.com/contest/711/problem/C Description ZS the Coder and ...
- 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 Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)
题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...
- Codeforces Round #369 (Div. 2)-C Coloring Trees
题目大意:有n个点,由m种颜料,有些点没有涂色,有些点已经涂色了,告诉你每个点涂m种颜色的价格分别是多少, 让你求将这n个点分成k段最少需要多少钱. 思路:动态规划,我们另dp[ i ][ j ][ ...
- Codeforces Round #302 (Div. 2) C. Writing Code 简单dp
C. Writing Code Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544/prob ...
- Codeforces Round #106 (Div. 2) D. Coloring Brackets 区间dp
题目链接: http://codeforces.com/problemset/problem/149/D D. Coloring Brackets time limit per test2 secon ...
- Codeforces Round #106 (Div. 2) D. Coloring Brackets —— 区间DP
题目链接:https://vjudge.net/problem/CodeForces-149D D. Coloring Brackets time limit per test 2 seconds m ...
随机推荐
- TCGA样本命名详解
在TCGA中,一个患者可能会对应多个样本,如TCGA-A6-6650可以得到3个样本数据: TCGA-A6-6650-01A-11R-1774-07TCGA-A6-6650-01A-11R-A278- ...
- hadoop配置项笔记 - streaming
stream.map.input.ignoreKey 作用:是否忽略key,只传递值. 默认值:false 我的设置:按需要更改 stream.map.input.field.separator st ...
- 「Python」数据清洗常用正则
对爬虫数据进行自然语言清洗时用到的一些正则表达式 标签中的所有属性匹配(排除src,href等指定参数) 参考链接 # \b(?!src|href)\w+=[\'\"].*?[\'\&quo ...
- Unity --- 在原目录中,将选中的Texture剥离为rgb和alpha
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; us ...
- 关于python 的http 日常操作
Http,互联网的基础 .net ,java(springboot),node.js,python 都能非常方便的实现http服务. 现在服务器后台跑着pytorch的模型,想着还是用python来处 ...
- Lyrics来源
Lyre 里拉琴,古希腊语,在北欧流行至中世纪. Lyrics in sheet music. This is a homorhythmic (i.e., hymn-style) arrangem ...
- Centos6与Centos7安装和使用htop
Centos6 安装 rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm r ...
- Eggjs 设置跨域请求
1. 安装egg-cors npm install egg-cors --save 2.打开config/plugin.js exports.cors: { enable: true, package ...
- python 两个 list 获取交集,并集,差集的函数
1. 获取两个 list 的交集 a = [1, 2, 3, 4] b = [1, 2, 5] print(list(set(a).intersection(set(b)))) 2. 获取两个 lis ...
- composer的安装方法 以及 ThinkPHP5安装
1.下载installer的文件,使用浏览器开启链接:getcomposer.org/installer,下载如下文件到 E:\xs2016\composer, 将installer重命名为compo ...