http://codeforces.com/group/1EzrFFyOc0/contest/711/problem/C

https://blog.csdn.net/qq_36368339/article/details/78568585?locationNum=6&fps=1  题解

题意:(真难理解)有n个点,m种颜色,你要给n个点上没有颜色的点染色。每个点i对应染的颜色j有一个颜料消耗,p[i][j]是点i染成j颜色的花费,你必须保证有k段颜色的点,输出最少花费多少颜料。

思路:题意稍微不太好理解。。。 
dp[i][j][v]:位置i染第j种颜料恰好有v段颜色,所花费的颜料数量; 
第i个位置没被染色:i已经确定,但j,v未知,需要暴力枚举。 
第i个位置已被染色:i,j已经确定,只需要暴力枚举v。(具体方程看代码) 
坑点就是初始化,还有注意找最小值。

 #include<iostream>
#include<cstdio>
#include <cctype>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cmath>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
#define se second
#define fi first
const int INF= 0x3f3f3f3f;
const int N=1e6+; ll n,m,k,a[],p[][],dp[][][]; int main()
{
cin>>n>>m>>k;
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++) scanf("%d",&p[i][j]); for(int i=;i<=n;i++)
for(int u=;u<=m;u++)
for(int v=;v<=k;v++) dp[i][u][v]=1e18; if(a[]==){ //初始化 如果第一个点没被涂色
for(int i=;i<=m;i++)
dp[][i][]=p[][i]; //涂上第一个点对应的p[1][j] ,此时v=1
}
else{ //第一个点被涂色了
dp[][a[]][]=; //=0 ,因为这样的点 不计入结果
} for(int i=;i<=n;i++)
{
if(a[i]==)
{
for(int u=;u<=m;u++)
{
for(int v=;v<=k;v++)
{
//下面比较v不变的时候:
dp[i][u][v]=min(dp[i][u][v], dp[i-][u][v]+p[i][u] ); //下面比较v 变的时候:
for(int j=;j<=m;j++)
{
if(j!=u && v>)
dp[i][u][v]=min(dp[i][u][v],dp[i-][j][v-]+p[i][u] );
}
//通过以上 找出:对相同的i,在u在[1,m]和v在[1,k]范围内dp[i][u][v]的最小值
}
}
}
else{
for(int u=;u<=m;u++)
{
for(int v=;v<=k;v++)
{
dp[i][a[i]][v]=min(dp[i][a[i]][v],dp[i-][a[i]][v]);
for(int j=;j<=m;j++)
{
if(j!=a[i] && v>)
dp[i][a[i]][v]=min(dp[i][a[i]][v],dp[i-][j][v-]);
}
}
}
}
}
ll ans=1e18;
for(int i=;i<=m;i++)
ans=min(ans,dp[n][i][k]);
if(ans==1e18) cout<<-;
else cout<<ans<<endl;
}

Codeforces #369 (Div. 2) C. Coloring Trees (3维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 动态规划

    C. Coloring Trees 题目连接: http://www.codeforces.com/contest/711/problem/C Description ZS the Coder and ...

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

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

  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(简单dp)

    题目:https://codeforces.com/problemset/problem/711/C 题意:给你n,m,k,代表n个数的序列,有m种颜色可以涂,0代表未涂颜色,其他代表已经涂好了,连着 ...

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

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

  8. Codeforces 981D Bookshelves(按位贪心+二维DP)

    题目链接:http://codeforces.com/contest/981/problem/D 题目大意:给你n本书以及每本书的价值,现在让你把n本书放到k个书架上(只有连续的几本书可以放到一个书架 ...

  9. codeforces 761 C. Dasha and Password(多维dp)

    题目链接:http://codeforces.com/contest/761/problem/C 题意:给出n行的字符串每一列都从第一个元素开始可以左右移动每一行字符串都是首位相连的. 最后问最少移动 ...

随机推荐

  1. Python3 IO编程之文件读写

    读写文件是最常见的IO操作.python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一个,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序终结操作磁盘, ...

  2. 最新 博盾习言java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿. 博盾习言等10家互联网公司的校招Offer,因为某些自身原因最终选择了 博盾习言.6.7月主要是做系统复习.项目复盘.Le ...

  3. javaFX 整合 maven

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  4. Google BERT

    概述 BERT的全称是Bidirectional Encoder Representation from Transformers,即双向Transformer的Encoder,因为decoder是不 ...

  5. Elasticsearch 全文搜索

    1,匹配查询(match) match查询主要的应用场景是进行全文搜索: // 1,初始化数据 DELETE /my_index PUT /my_index { "settings" ...

  6. LeetCode 674. 最长连续递增序列(Longest Continuous Increasing Subsequence) 18

    674. 最长连续递增序列 674. Longest Continuous Increasing Subsequence 题目描述 给定一个未经排序的整型数组,找到最长且连续的递增序列. Given ...

  7. vw、vh、vmin、vmax 的含义

    像 px.em 这样的长度单位大家肯定都很熟悉,前者为绝对单位,后者为相对单位.CSS3 又引入了新单位:vw.vh.vmin.vmax.下面对它们做个详细介绍.   一.基本说明 1,vw.vh.v ...

  8. 多线程(8) — ThreadLocal

    ThreadLocal是一个线程的局部变量,也就是只有当前线程可以访问,是线程安全的.为每一个线程分配不同的对象,需要在应用层面保证ThreadLocal只起到简单的容器作用. ThreadLocal ...

  9. stm32之中断响应优先级

    1)中断响应分为:自然优先级.抢占优先级.响应优先级. 2)抢占优先级和响应优先级,其实是一个中断所包含的两个优先级,其中前者是抢占优先级之间的级别划分,后者是相同抢占优先级的优先级别的划分. 中断A ...

  10. fpga基础

    1.FPGA 的分类: 根据 FPGA 基本结构,可将其分为基于乘积项(Product-Term)技术的 FPGA 和基于查找表(Look-Up-Table)技术的 FPGA 两种. (1)基于乘积项 ...