codeforces 711C Coloring Trees(DP)
题目链接:http://codeforces.com/problemset/problem/711/C
O(n^4)的复杂度,以为会超时的
思路:dp[i][j][k]表示第i棵数用颜色k涂完后beauty为j
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf=1e15;
int c[105];
int p[105][105];
ll dp[105][105][105];
int main()
{
int n,m,b;
scanf("%d%d%d",&n,&m,&b);
for(int i=1;i<=n;i++)
scanf("%d",&c[i]);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
scanf("%d",&p[i][j]);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=b;j++)
{
for(int k=1;k<=m;k++)
dp[i][j][k]=inf;
}
}
for(int i=1;i<=n;i++)
{
if(c[i])
{
for(int j=1;j<=b&&j<=i;j++)
{
int k=c[i];
ll min1=inf,min2=inf;
if(k>1&&j>1)
min1=*min_element(dp[i-1][j-1]+1,dp[i-1][j-1]+k);
if(k<m&&j>1)
min2=*min_element(dp[i-1][j-1]+k+1,dp[i-1][j-1]+m+1);
dp[i][j][k]=min(dp[i-1][j][k],min(min1,min2));
}
}
else
{
for(int j=1;j<=b&&j<=i;j++)
{
for(int k=1;k<=m;k++)
{
ll min1=inf,min2=inf;
if(k>1&&j>1)
min1=*min_element(dp[i-1][j-1]+1,dp[i-1][j-1]+k);
if(k<m&&j>1)
min2=*min_element(dp[i-1][j-1]+k+1,dp[i-1][j-1]+m+1);
dp[i][j][k]=p[i][k]+min(dp[i-1][j][k],min(min1,min2));
}
}
}
}
ll ans=*min_element(dp[n][b]+1,dp[n][b]+m+1);
if(ans<inf)
printf("%I64d\n",ans);
else
printf("-1\n");
return 0;
}
codeforces 711C Coloring Trees(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 (DP)
C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 【CF711C】Coloring Trees(DP)
题意:给你n个数字,一共有m种,如果某数为0则该数为空,空的地方可以填任意种类数,但每填一个数字都要花费一定的费用, 从头到尾,所有相邻且相同的数字看作一个集合,求使n个数字的集合数为k所需的最小费用 ...
- Codeforces Gym101341K:Competitions(DP)
http://codeforces.com/gym/101341/problem/K 题意:给出n个区间,每个区间有一个l, r, w,代表区间左端点右端点和区间的权值,现在可以选取一些区间,要求选择 ...
- CodeForces 711C Coloring Trees (DP)
题意:给定n棵树,其中有一些已经涂了颜色,然后让你把没有涂色的树涂色使得所有的树能够恰好分成k组,让你求最少的花费是多少. 析:这是一个DP题,dp[i][j][k]表示第 i 棵树涂第 j 种颜色恰 ...
- 【Codeforces】CF 9 D How many trees?(dp)
题目 传送门:QWQ 分析 用$ dp[i][j] $表示用i个节点,有多少深度小于等于j的二叉树. 答案是$ dp[n][n] - dp[n][h-1] $ 转移时枚举左子树的节点数量,就可以知道右 ...
- codeforces#1154F. Shovels Shop (dp)
题目链接: http://codeforces.com/contest/1154/problem/F 题意: 有$n$个物品,$m$条优惠 每个优惠的格式是,买$x_i$个物品,最便宜的$y_i$个物 ...
- Codeforces 1051 D.Bicolorings(DP)
Codeforces 1051 D.Bicolorings 题意:一个2×n的方格纸,用黑白给格子涂色,要求分出k个连通块,求方案数. 思路:用0,1表示黑白,则第i列可以涂00,01,10,11,( ...
- Codeforces 1207C Gas Pipeline (dp)
题目链接:http://codeforces.com/problemset/problem/1207/C 题目大意是给一条道路修管道,相隔一个单位的管道有两个柱子支撑,管道柱子高度可以是1可以是2,道 ...
随机推荐
- nyoj 115------城市平乱( dijkstra // bellman )
城市平乱 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 南将军统领着N个部队,这N个部队分别驻扎在N个不同的城市. 他在用这N个部队维护着M个城市的治安,这M个城市 ...
- Simplify Path [LeetCode]
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- Objective-C:@property参数详解
格式:@property(param1, param2) 类型 属性名 参数可有可无 三类参数: 1.读写属性:readwrite/readonly readwrite:产生setter\getter ...
- 关于C#迭代器
>1 IEnumerator与IEnumerable IEnumerator与IEnumerable两个接口是用于实现迭代器的接品只要实现了IEnumerable就可以用foreach,linq ...
- SQL语句技巧(上个样式太差了)
以下并非本人整理,但是看后感觉相当不错,特此分享. 1.应用程序中,保证在实现功能的基础上,尽量减少对数据库的访问次数:通过搜索参数,尽量减少对表的访问行数,最小化结果集,从而减轻网络负担:能够分 ...
- 241. Different Ways to Add Parentheses——本质:DFS
Given a string of numbers and operators, return all possible results from computing all the differen ...
- [转]Arrays.sort()你应该知道的事
以下内容转自: 原文链接: programcreek 翻译: ImportNew.com- 刘志军 译文链接: http://www.importnew.com/8952.html --------- ...
- BZOJ1393 [Ceoi2008]knights
题意...上ceoi官网看吧... 首先打一下sg函数发现必胜态和必败态的分布位置是有规律的 于是我们只要知道最长步数的必胜态和最长步数的必败态哪个更长就可以了 然后再打一下步数的表...发现必败态的 ...
- PDF 补丁丁 0.4.1 版:新增嵌入中文字库、替换文档字库的功能
PDF 补丁丁 0.4.1 版新增了嵌入中文字库.替换文档字库的功能. 嵌入汉字字库 历史上有一批黄底黑字的 PDF 文档.这批文档都具有相同的问题:没有嵌入字库.在一些设备上阅读时显示乱码.复制文本 ...
- HBase High Level Architecutre