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 walked in the park where n trees grow. They decided to be naughty and color the trees in the park. The trees are numbered with integers from 1 to n from left to right.
Initially, tree i has color ci. ZS the Coder and Chris the Baboon recognizes only m different colors, so 0 ≤ ci ≤ m, where ci = 0 means that tree i is uncolored.
ZS the Coder and Chris the Baboon decides to color only the uncolored trees, i.e. the trees with ci = 0. They can color each of them them in any of the m colors from 1 to m. Coloring the i-th tree with color j requires exactly pi, j litres of paint.
The two friends define the beauty of a coloring of the trees as the minimum number of contiguous groups (each group contains some subsegment of trees) you can split all the n trees into so that each group contains trees of the same color. For example, if the colors of the trees from left to right are 2, 1, 1, 1, 3, 2, 2, 3, 1, 3, the beauty of the coloring is 7, since we can partition the trees into 7 contiguous groups of the same color : {2}, {1, 1, 1}, {3}, {2, 2}, {3}, {1}, {3}.
ZS the Coder and Chris the Baboon wants to color all uncolored trees so that the beauty of the coloring is exactly k. They need your help to determine the minimum amount of paint (in litres) needed to finish the job.
Please note that the friends can't color the trees that are already colored.
Input:
The first line contains three integers, n, m and k (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of trees, number of colors and beauty of the resulting coloring respectively.
The second line contains n integers c1, c2, ..., cn (0 ≤ ci ≤ m), the initial colors of the trees. ci equals to 0 if the tree number i is uncolored, otherwise the i-th tree has color ci.
Then n lines follow. Each of them contains m integers. The j-th number on the i-th of them line denotes pi, j (1 ≤ pi, j ≤ 109) — the amount of litres the friends need to color i-th tree with color j. pi, j's are specified even for the initially colored trees, but such trees still can't be colored.
Output:
Print a single integer, the minimum amount of paint needed to color the trees. If there are no valid tree colorings of beauty k, print - 1.
Sample Input:
3 2 2
0 0 0
1 2
3 4
5 6
Sample Output:
10
这是后来补上的,为什么我做dp的时候就是想不到公式呢?还应该多做dp啊
【题目链接】Codeforces 711C
【题目类型】dp
&题意:
先给你n颗树,有m种颜色,你要把所以的数都涂色,色是[1,m],0代表没有色,只有当这颗树是0的时候你才可以涂色,并且你涂完色的树必须要满足正好是k段,涂每个树的颜色都有不同的花费,给你这些数据,你要输出最小花费,不可行是输出-1。
&题解:
首先看范围100,因为这是cf评测的,所以n^4可过,并且能开三维数组,那么就可以dp了。
dp[i][j][k]:表示考虑第i棵树涂第j种颜色,当前分为k组的最小花费
写dp,先写初始条件,之后找方程,判断状态,最后选最小值输出就好了
【时间复杂度】O(n^4)
&代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
#define cle(a,val) memset(a,(val),sizeof(a))
#define SI(N) scanf("%d",&(N))
#define SI2(N,M) scanf("%d %d",&(N),&(M))
#define SI3(N,M,K) scanf("%d %d %d",&(N),&(M),&(K))
#define rep(i,b) for(int i=0;i<(b);i++)
#define rez(i,a,b) for(int i=(a);i<=(b);i++)
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 100 + 5 ;
int n, m, k, p[MAXN][MAXN], a[MAXN];
ll dp[MAXN][MAXN][MAXN];
void Solve() {
while (~SI3(n, m, k)) {
rez(i, 1, n)SI(a[i]);
rez(i, 1, n) rez(j, 1, m)SI(p[i][j]);
cle(dp, 0x3f);
if (a[1]) dp[1][a[1]][1] = 0;
else rez(i, 1, m) dp[1][i][1] = p[1][i];
rez(i, 2, n) rez(j, 1, m) rez(u, 1, k) if (dp[i - 1][j][u] < LINF) {
if (a[i])dp[i][a[i]][u + (a[i] != j)] = min(dp[i][a[i]][u + (a[i] != j)], dp[i - 1][j][u]);
else rez(v, 1, m) dp[i][v][u + (v != j)] = min(dp[i][v][u + (v != j)], dp[i - 1][j][u] + p[i][v]);
}
ll re = LINF;
rez(i, 1, m)
re = min(re, dp[n][i][k]);
if (re == LINF) re = -1;
cout << re << endl;
}
}
int main() {
Solve();
return 0;
}
Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)的更多相关文章
- 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)
题目:https://codeforces.com/problemset/problem/711/C 题意:给你n,m,k,代表n个数的序列,有m种颜色可以涂,0代表未涂颜色,其他代表已经涂好了,连着 ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- 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题)
题目链接 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
题目大意:有n个点,由m种颜料,有些点没有涂色,有些点已经涂色了,告诉你每个点涂m种颜色的价格分别是多少, 让你求将这n个点分成k段最少需要多少钱. 思路:动态规划,我们另dp[ i ][ j ][ ...
- Codeforces Round #245 (Div. 1) B. Working out (dp)
题目:http://codeforces.com/problemset/problem/429/B 第一个人初始位置在(1,1),他必须走到(n,m)只能往下或者往右 第二个人初始位置在(n,1),他 ...
- Codeforces Round #260 (Div. 1) 455 A. Boredom (DP)
题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory l ...
随机推荐
- Draw a Border around any C# Winform Control
public class MyGroupBox : GroupBox { protected override void OnPaint(PaintEventArgs e) { base.OnPain ...
- jquery轻松操作CSS样式
$(this).click(function(){ if($(this).hasClass(“zxx_fri_on”)){ $(this).removeClass(“zxx_fri_on”); ...
- 如何才能将Faster R-CNN训练起来?
如何才能将Faster R-CNN训练起来? 首先进入 Faster RCNN 的官网啦,即:https://github.com/rbgirshick/py-faster-rcnn#installa ...
- 出现Fatal IO error 11 (资源暂时不可用) on X server :0.0.的可能原因及解决方案
我在使用python的过程当中发现了这个有这样的错误,后来看了下面这篇文档才知道原因所在. 最近在编写一个局域网的聊天工具,在编写客户端时,我把界面部分和事件处理函数写好后,准备再开一个线程用于接收服 ...
- lua for循环
<转自网络,仅供学习> for循环是一个循环控制结构,可以有效地编写需要执行的特定次数的循环. 语法 Lua编程语言的for循环的语法是: for init,max/min value, ...
- Linux信号量详解
1.什么是信号量信号量是一种特殊的变量,访问具有原子性.只允许对它进行两个操作:1)等待信号量当信号量值为0时,程序等待:当信号量值大于0时,信号量减1,程序继续运行.2)发送信号量将信号量值加1. ...
- html之texteara
定义多行的文本输入控件,所有浏览器都支持,可容纳无限的文本,等宽的字体. 用css的height和width来设置其框框的大小是个很好的办法,其中的文本换行符为%OD/%OA(回车换行) html5中 ...
- android虚拟机(ROOT)权限
自己找的一个比较好用的pc端安卓模拟器,蓝手指总所周知吧,这个是较高版本但不是最新的一个版本,关键是自带root功能,对于破解安卓存档类游戏还是有用的.安卓版本4.4.2 BlueStacks 0.9 ...
- selenium+python自动化之CSS定位
一.css:属性定位 1.css可以通过元素的id.class.标签这三个常规属性直接定位到 2.如下是百度输入框的的html代码: <input id="kw" class ...
- java中 == 与 equal区别 转
java中的数据类型,可分为两类:1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean 他们之间的比较,应用双等号(==) ...