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 ...
随机推荐
- LAMT基于mod_jk方式的负载均衡集群
一.系统环境 1.apache服务器 系统环境:CentOS release 6.5 (Final) ip地址:192.168.1.203 2.tomcat1服务器 系统环境:CentOS relea ...
- linux笔记_磁盘分区
一.分区的意义 1.不同操作系统往往不可以同时装载在同一个分区,分区解决了不同操作系统装载在同一个物理硬盘的兼容性问题 2.机械硬盘盘片外圈读写速度相对内圈要快,分区可以把常用数据限制在读写速度较快的 ...
- DAG上的动态规划之嵌套矩形
题意描述:有n个矩形,每个矩形可以用两个整数a.b描述,表示它的长和宽, 矩形(a,b)可以嵌套在矩形(c,d)当且仅当a<c且b<d, 要求选出尽量多的矩形排成一排,使得除了最后一个外, ...
- 《Java程序设计》第6周学习总结
学号20145220 <Java程序设计>第6周学习总结 教材学习内容总结 InputStream与OutputStream 10.1.1串流设计的概念 Java将输入/输出抽象化为串流, ...
- spark1.5引进内置函数
在Spark 1.5.x版本,增加了一系列内置函数到DataFrame API中,并且实现了code-generation的优化.与普通的函数不同,DataFrame的函数并不会执行后立即返回一个结果 ...
- hdu1074 状压DP、栈实现记录路径
题意:给了几门学科作业.它们的截止提交期限(天数).它们的需要完成的时间(天数),每项作业在截止日期后每拖延一天扣一学分,算最少扣的学分和其完成顺序. 一开始做的时候,只是听说过状态压缩这个神奇的东西 ...
- 越狱Season 1-Episode 6: Riots, Drills and the Devil: Part 1
Season 1, Episode 6: Riots, Drills and the Devil: Part 1 - Diamond: Just a few more rides. 就再多玩几次吧 O ...
- 《剑指Offer》之替换空格
1.题目描述 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 2.代码实 ...
- PHP-线程一直不释放调试
一.现象 1.查看进程是否存在 ps -ef | grep -v 'grep' |grep -E 'shell/cron/bonus/cash' www 2624 1 0 Oct24 ...
- 引用POPUI来实现弹窗效果,且弹窗中的内容可以点击事件
seajs.use(['../js/ui/dialog'],function(){ $('.center-button').bind('click',function(){ var $dlg = $. ...