题目: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)的更多相关文章

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

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

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

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

  6. Codeforces Round #369 (Div. 2)-C Coloring Trees

    题目大意:有n个点,由m种颜料,有些点没有涂色,有些点已经涂色了,告诉你每个点涂m种颜色的价格分别是多少, 让你求将这n个点分成k段最少需要多少钱. 思路:动态规划,我们另dp[ i ][ j ][ ...

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

  8. Codeforces Round #106 (Div. 2) D. Coloring Brackets 区间dp

    题目链接: http://codeforces.com/problemset/problem/149/D D. Coloring Brackets time limit per test2 secon ...

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

随机推荐

  1. 近期wxss总结

    最近有一些需要点击才能实现的样式切换,我用的方法有2种 1 wxml 中 对class给一个判断式 class="变量?变化后的类:变化前的类" 这样在js中设一个变量,我是设成布 ...

  2. ECharts导出word 图表模糊失真

    在项目中会有这样的需求,echars生成图表导入到word中 在项目中用的插件 博主有一篇文章将的是  vue使用jquery的三方插件jquery.wordexport.js   https://b ...

  3. 数据库连接超时:“The last packet successfully received from the server was xxx milliseconds ago”

    产生的原因:应用方的数据库连接有效期时间,大于数据库自己设置的有效期. 解决方案: 一.修改druid配置(如果使用druid的话) spring.datasource.druid.validatio ...

  4. System.InvalidOperationException:“线程间操作无效: 从不是创建控件“btnSearch”的线程访问它。

    System.InvalidOperationException:“线程间操作无效: 从不是创建控件“btnSearch”的线程访问它.”这个问题属于跨线程问题 在Form1重载中写上一行代码 Sys ...

  5. eee

    package demo; public class Phone { String phonexinghao; String phonepinpai; public void shouji (){ S ...

  6. php正则匹配

    在PHP中,有两套正则表达式函数库,两者功能相似,只是执行效率上有所不同, 一套是有"preg_"为前缀命名的函数,一套有"ereg_"命名的函数的函数, 一个 ...

  7. reactiveCocoa使用注意点

    @RACSubject信号 注意点:1如果一个页面需要多次发送这个消息,那么似乎会暴露一个bug,信号不会被销毁,等到发送第二个信号 时,第一个信号仍然会被发送,导致错误,比如一个tableView的 ...

  8. Matlab的BP神经网络工具箱及其在函数逼近中的应用

    1.神经网络工具箱概述 Matlab神经网络工具箱几乎包含了现有神经网络的最新成果,神经网络工具箱模型包括感知器.线性网络.BP网络.径向基函数网络.竞争型神经网络.自组织网络和学习向量量化网络.反馈 ...

  9. tortoisegit里的cleanup坑

    tortoisegit的clean up功能(以下红框部分)真的不要去点啊,你所有被ignore(忽略)的本地文件会被全部删除,而且是无法恢复的,因为远程仓库里根本就没有这些文件. 血的教训啊,本以为 ...

  10. 每日算法---Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...