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行的字符串每一列都从第一个元素开始可以左右移动每一行字符串都是首位相连的. 最后问最少移动 ...
随机推荐
- Docker - 在CentOS7.5中升级Docker版本
1 - 检查当前版本 [root@localhost ~]# uname -a Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu ...
- Tuner工作原理详解
1.TV自动搜台原理:https://wenku.baidu.com/view/3b771f8b84868762caaed514 2.彩电自动搜台的原理与维修:http://tv.baoxiu.c ...
- javaFX 整合 maven
pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- pycharm 提示:this license **** has been cancelled(2)
pycharm安装激活过程中,提示 this license **** has been cancelled .这个问题并不是你的激活码不对,而是需要修改系统的hosts文件,下面详细讲解下如何修改h ...
- C++ 配置文件解析类 ParseConfig
依赖项: 依赖于 ProcessString 类,可从该篇博客获取「字符串处理类 ProcessString (包含常用字符串处理函数)」 ParseConfig.h //Linux & C+ ...
- Linux基础-15-samba服务
1. samba的功能: samba是一个网络服务器,用于Linux和Windows之间共享文件. 2. samba服务的启动.停止.重启 service smb start|stop|restart ...
- python 之 数据库(多表查询之连接查询、子查询、pymysql模块的使用)
10.10 多表连接查询 10.101 内连接 把两张表有对应关系的记录连接成一张虚拟表 select * from emp,dep: #连接两张表的笛卡尔积 select * from emp,de ...
- 湖北校园网PC端拨号算法逆向
湖北校园网PC端拨号算法逆向 前言 上一文 PPPoE中间人拦截以及校园网突破漫谈我们谈到使用 PPPoE 拦截来获取真实的账号密码. 在这个的基础上,我对我们湖北的客户端进行了逆向,得到了拨号加密算 ...
- Elastic Search快速上手(2):将数据存入ES
前言 在上手使用前,需要先了解一些基本的概念. 推荐 可以到 https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.htm ...
- jmeter命令行执行脚本_动态参数设置
从04月换公司开始,就没静下来心来学习,其中发生了比较多的事情吧,不过不管如何,没坚持学习还是因为懒.本周交接完,下周去入职新公司,该静下心来学点什么了. ---------------------- ...