Codeforces #369 (Div. 2) C. Coloring Trees (3维dp
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的更多相关文章
- 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 动态规划
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)
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 (很妙的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(简单dp)
题目:https://codeforces.com/problemset/problem/711/C 题意:给你n,m,k,代表n个数的序列,有m种颜色可以涂,0代表未涂颜色,其他代表已经涂好了,连着 ...
- Codeforces Round #369 (Div. 2)-C Coloring Trees
题目大意:有n个点,由m种颜料,有些点没有涂色,有些点已经涂色了,告诉你每个点涂m种颜色的价格分别是多少, 让你求将这n个点分成k段最少需要多少钱. 思路:动态规划,我们另dp[ i ][ j ][ ...
- Codeforces 981D Bookshelves(按位贪心+二维DP)
题目链接:http://codeforces.com/contest/981/problem/D 题目大意:给你n本书以及每本书的价值,现在让你把n本书放到k个书架上(只有连续的几本书可以放到一个书架 ...
- codeforces 761 C. Dasha and Password(多维dp)
题目链接:http://codeforces.com/contest/761/problem/C 题意:给出n行的字符串每一列都从第一个元素开始可以左右移动每一行字符串都是首位相连的. 最后问最少移动 ...
随机推荐
- Yii2性能优化
https://www.yiiframework.com/doc/guide/2.0/zh-cn/tutorial-performance-tuning 性能优化 有许多因素影响你的 Web 应用程序 ...
- Word中如何加载EndNote
在百度中搜索了很多解决方案,都不尽相同忙了一上午也没解决,然后搁浅,吃过午饭回来在安装目录下找到如下exe文件三四步点击搞定 所以,有时候问题出现了长时间没解决,可以换下环境出去溜达一圈说不定就茅塞顿 ...
- 【GStreamer开发】GStreamer基础教程05——集成GUI工具
目标 本教程展示了如何在GStreamer集成一个GUI(比如:GTK+).最基本的原则是GStreamer处理多媒体的播放而GUI处理和用户的交互. 在这个教程里面,我们可以学到: 如何告诉GStr ...
- Django 操作已经存在的数据库
反向操作数据库 何为反向操作.即是数据库在项目之前已经存在,不需要新建表,操作已经存在的表 # 进入站点目录下执行 python manage.py inspectdb #可以看到settings中连 ...
- linux中matlab的中文乱码问题
ref:http://hi.baidu.com/36470902/item/47f3b133c419d989f5e4ade1 将系统的jre拷贝到matlab去 . sudo mv $MATLAB/s ...
- git将代码提交到多个远程仓库
在项目目录下执行: git remote set-url --add origin http://mayun.cn/xxx/adsdsdsdcelery-demo.git 即可添加一个远程仓库. 再执 ...
- Ribbon学习笔记
微服务的概念: Ribbon默认的是轮询的算法: @LoadBalanced @EnableEurekaClient Irule是根据 Ribbon默认(轮询)的7中负载均衡的算法: 修改默认的R ...
- LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...
- InnoDB一定会在索引中加上主键吗?
先回答问题,是的. 故事, 如下图,今天查看慢sql发现同事在建索引的时候竟然包含了主键,第一感觉这种姿势不对. 我记得很多文章都说过二级索引会自动带上主键,为什么还有人这么操作,后来差了些资料,如下 ...
- mysql数据库语言分类
MySQL的sql语言分类DML.DQL.DDL.DCL. MySQL的sql语言分类DML.DQL.DDL.DCL. SQL语言一共分为4大类:数据定义语言DDL,数据操纵语言DML,数据查询语 ...