CodeForces 711C Coloring Trees
简单$dp$。
$dp[i][j][k]$表示:前$i$个位置染完色,第$i$个位置染的是$j$这种颜色,前$i$个位置分成了$k$组的最小花费。总复杂度$O({n^4})$。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-; LL INF=1e17;
LL dp[][][];
LL p[][];
int c[],n,m,k; int main()
{
scanf("%d%d%d",&n,&m,&k); for(int i=;i<=n;i++) scanf("%d",&c[i]); for(int i=;i<=n;i++)
for(int j=;j<=m;j++) scanf("%lld",&p[i][j]); for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
for(int s=;s<=k;s++)
{
dp[i][j][s]=INF;
}
}
} if(c[]!=) dp[][c[]][]=;
else
{
for(int j=;j<=m;j++) dp[][j][]=p[][j];
} for(int i=;i<=n;i++)
{
if(c[i]==)
{
for(int j=;j<=m;j++)
{
for(int s=;s<=i;s++)
{
LL tmp=INF;
tmp=min(tmp,dp[i-][j][s]);
for(int t=;t<=m;t++)
{
if(t==j) continue;
tmp=min(tmp,dp[i-][t][s-]);
}
if(tmp==INF) continue;
else dp[i][j][s]=tmp+p[i][j];
}
}
} else
{
for(int s=;s<=i;s++)
{
LL tmp=INF;
tmp=min(tmp,dp[i-][c[i]][s]);
for(int t=;t<=m;t++)
{
if(t==c[i]) continue;
tmp=min(tmp,dp[i-][t][s-]);
} if(tmp==INF) continue;
dp[i][c[i]][s]=tmp;
}
}
} LL ans=INF;
for(int j=;j<=m;j++) ans=min(ans,dp[n][j][k]); if(ans==INF) printf("-1\n");
else printf("%lld\n",ans); return ;
}
CodeForces 711C Coloring Trees的更多相关文章
- codeforces 711C Coloring Trees(DP)
题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...
- 【动态规划】Codeforces 711C Coloring Trees
题目链接: http://codeforces.com/problemset/problem/711/C 题目大意: 给N棵树,M种颜色,已经有颜色的不能涂色,没颜色为0,可以涂色,每棵树I涂成颜色J ...
- CodeForces 711C Coloring Trees (DP)
题意:给定n棵树,其中有一些已经涂了颜色,然后让你把没有涂色的树涂色使得所有的树能够恰好分成k组,让你求最少的花费是多少. 析:这是一个DP题,dp[i][j][k]表示第 i 棵树涂第 j 种颜色恰 ...
- Codeforces 677C. Coloring Trees dp
C. Coloring Trees time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- Code Forces 711C Coloring Trees
C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Coloring Trees CodeForces - 711C
Coloring Trees CodeForces - 711C 题意:有n个点,每个点有一个c值,如果为0表示它没有被染色,否则表示它被染成了c值的颜色.颜色有1到m.把第i棵树染成颜色j所需要的代 ...
- codeforces 711C C. Coloring Trees(dp)
题目链接: C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 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 ...
随机推荐
- queue,指针求最短路的区别
这里以spfa为例://都用邻接表存边: 指针: int h=1,t=1; q[h]=x; while(h<=t){ int u=q[h]; vis[u]=0; for(int i=head[u ...
- C#奇葩关键字
C#奇葩关键字——忐忑 那就认识认识吧,可是又太多,所以也只能是想到哪里是哪里,我们这就让思绪自由飞翔一会吧! 1.@ 这个东东看似和邮件有关啊,但是在C#的世界里,可跟邮件没有一毛钱关系,它是str ...
- struts2文件上传大小限制问题
struts2默认文件上传大小为2M,如需修改默认大小,解决方法如下: <struts> <constant name="struts.multipart.maxSize& ...
- 给Amazon ec2 增加卷(Volume)并挂载到系统
给Amazon ec2 增加卷(Volume)并挂载到系统 前言 导师让师弟把实验的网站挂到亚马逊的EC2云服务器上.师弟对linux不太熟悉.就跑过来问我.于是花了一个小时搞定.问题主要是将EC2的 ...
- 解决WP7的32位图像渐变色色阶问题
做游戏时发现背景图色阶现象严重,想了想会不会是显卡色深问题,于是加了下面一段代码,结果解决这个问题. graphics.PreferredBackBufferFormat = Microsoft.Xn ...
- CSS样式基础知识
CSS样式基础知识 CSS样式概述 CSS是Cascading Style Sheet 的缩写.译作“层叠样式表单”.是用于(增强)控制网页样式并允许将样式信息与网页内容分离的一种标记性语言. 引用位 ...
- Linux下SSH+Firefox
Linux下SSH+Firefox 简明FQ攻略 FQ的软件有很多,楼主原来在Windows下用过Tor(洋葱头).Puff.freegate等,一般只需要打开FQ软件,简单的设置后就可以FQ浏览了. ...
- Django入门实践(三)
Django入门实践(三) Django简单应用 前面简单示例说明了views和Template的工作过程,但是Django最核心的是App,涉及到App则会和Model(数据库)打交道.下面举的例子 ...
- C++输入一个字符串,把其中的字符按照逆序输出的两种方法
用字符数组方法: 基本思路是,先判断字符的结束标志'\0',然后从该位置向前输出. 实现代码: #include<iostream> using namespace std; int ma ...
- POJ 1033 Defragment
根据http://hi.baidu.com/algorithm/item/d51b15f7a8ea1c0a84d278be这个开始练习ac,刚开始接触这道题时以为是道搜索题,读完之后深思了一下,感觉不 ...