题目: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. c#异步学习笔记

    如下代码.只需要异步的处理一个数据,不需要等处理完成后,进行后继的操作.可以不用Async来修饰方法 static void Main(string[] args) { Test(); Console ...

  2. h5软键盘弹起 底部按钮被顶起问题解决

    解决思路: 当键盘弹起时隐藏掉按钮,键盘隐藏时按钮显示 监测键盘是否弹起(浏览器页面是否发生变化) 代码: 1.定义一个底部按钮 <div class="returnbtn" ...

  3. Maven 插件打包部署项目

    clean install -Dmaven.test.skip=true:打包工具 clean package  

  4. vue整理

    安装 vue ui axios import axios from 'axios' // let curWwwPath = window.document.location.href // let p ...

  5. 读javascript高级程序设计-目录

    javascript高级编程读书笔记系列,也是本砖头书.感觉js是一种很好上手的语言,不过本书细细读来发现了很多之前不了解的细节,受益良多.<br/>本笔记是为了方便日后查阅,仅作学习交流 ...

  6. 高可用性的负载均衡方案之lvs+keepalived和haproxy+heartbeat区别

    高可用性的负载均衡方案 目前使用比较多的就是标题中提到的这两者,其实lvs和haproxy都是实现的负载均衡的作用,keepalived和heartbeat都是提高高可用性的,避免单点故障.那么他们为 ...

  7. GNOME 系统设置

    详细的GNOME系统设置全文,参见这里. 以下摘录使用到的部分. 1. 在任务栏上显示日期或周几(二选一).秒数 $ gsettings set org.gnome.desktop.interface ...

  8. linux php5.6 安装Redis扩展

    wget http://pecl.php.net/get/redis-4.2.0.tgz tar -zxvf redis-.tgz cd redis- /usr/local/php5./bin/php ...

  9. linux 查找指定进程并kill

    ps -ef | grep  php | grep -v 'grep' | awk '{print $2}'| xargs kill -9

  10. java-猜数字

    package com.jijy.circle; import java.util.Scanner; import java.util.Random; public class demo5 { pub ...