题目: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. 让小乌龟可以唱歌——对Python turtle进行拓展

    在Scratch中,小猫是可以唱歌的,而且Scratch的声音木块有着丰富的功能,在这方面Python turtle略有欠缺,今天我们就来完善一下. Python声音模块 Python处理声音的模块很 ...

  2. centos7——mysql5.7忘记root密码的处理方式

    参考资料:https://www.cnblogs.com/nangch/p/5521193.html 解决方法: 一.通过编辑/etc/my.cnf文件在[mysqld]下面加上skip-grant- ...

  3. day_其他操作符的重载

    #include <iostream> #include <stdlib.h> using namespace std; class A{ public: A(void) { ...

  4. [C#]将数据写入已存在的excel文件

    测试如下(xls/xlsx): //将数据写入已存在Excel public static void writeExcel(string result, string filepath) { //1. ...

  5. android------个人项目(歆语气象通新版)

    歆语气象通: 歆语气象伴随你的身边,便捷生活. 包含了以下功能: 1. 天气预报数据覆盖中国城市和地区:2. 提供一周天气预报及最低最高温度,时刻关注天气,轻松计划出行:3. 各种指数详细信息,如太阳 ...

  6. iphone上mitmproxy证书设置

    PC端安装mitmproxy并添加证书后,基本问题不大,都能正常运行起来 手机端iphone上下载安装mitmproxy证书: 1.手机和PC在同一个局域网中,设置wifi代理为PC端的ip,端口为m ...

  7. 交换机telnet配置

    新开箱交换机开机配置Telnet需要三个步骤: 1.开启telnet是能:系统视图模式下输入命令: telnet server enable #开启telnet功能# 2.Telnet创建账号:aaa ...

  8. 课堂小练习(complex类)

    定义一个复数类Complex,使得下面的代码能够工作: Complex c1(3,5);     //用复数3+5i初始化c1: Compex c2=4.5;      //用实数4.5初始化c2 c ...

  9. requests库/爬取zhihu表情包

    先学了requests库的一些基本操作,简单的爬了一下. 用到了requests.get()方法,就是以GET方式请求网页,得到一个Response对象.不加headers的话可能会400error所 ...

  10. PHP反射学习总结

    反射(Reflection) PHP的反射机制提供了一套反射API,用来访问和使用类.方法.属性.参数和注释等,比如可以通过一个对象知道这个对象所属的类,这个类包含哪些方法,这些方法需要传入什么参数, ...