【动态规划】Codeforces 711C Coloring Trees
题目链接:
http://codeforces.com/problemset/problem/711/C
题目大意:
给N棵树,M种颜色,已经有颜色的不能涂色,没颜色为0,可以涂色,每棵树I涂成颜色J花费PIJ。求分成K个颜色段(1112221为3个颜色段)的最小花费。无解输出-1.
题目思路:
【动态规划】
f[i][j][k]表示前i个树分成j段,最后一个颜色是k的花费。
根据当前这棵树是否可以涂色,上一棵树是否可以涂色转移。
直接枚举这个树和上棵树的颜色,N4居然没T。
//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 104
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int c[N];
LL aans;
LL a[N][N];
LL f[N][N][N];
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k,l;
// for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s+1))
while(~scanf("%d",&n))
{
scanf("%d%d",&m,&cas);
for(i=;i<=n;i++)
scanf("%d",&c[i]);
for(i=;i<=n;i++)
for(j=;j<=m;j++)
scanf("%I64d",&a[i][j]);
mem(f,0x7f);
c[]=m+,f[][][m+]=;
for(i=;i<=n;i++)
{
if(c[i]!=)
{
if(c[i-]!=)
{
for(j=;j<=min(i,cas);j++)
{
if(c[i]!=c[i-])
f[i][j][c[i]]=min(f[i][j][c[i]],f[i-][j-][c[i-]]);
else f[i][j][c[i]]=min(f[i][j][c[i]],f[i-][j][c[i-]]);
}
}
else//c[i]!=0 c[i-1]=0
{
for(j=;j<=min(i,cas);j++)
{
for(k=;k<=m;k++)
{
if(k!=c[i])
f[i][j][c[i]]=min(f[i][j][c[i]],f[i-][j-][k]);
else f[i][j][c[i]]=min(f[i][j][c[i]],f[i-][j][k]);
}
}
}
}
else if(c[i-]==)//c[i]=0
{
for(j=;j<=min(i,cas);j++)
{
for(k=;k<=m;k++)
{
for(l=;l<=m;l++)
{
if(l==k)f[i][j][k]=min(f[i][j][k],f[i-][j][l]+a[i][k]);
else f[i][j][k]=min(f[i][j][k],f[i-][j-][l]+a[i][k]);
}
}
}
}
else//c[i]=0,c[i-1]!=0
{
for(j=;j<=min(i,cas);j++)
{
for(k=;k<=m;k++)
{
if(k!=c[i-])
f[i][j][k]=min(f[i][j][k],f[i-][j-][c[i-]]+a[i][k]);
else f[i][j][k]=min(f[i][j][k],f[i-][j][c[i-]]+a[i][k]);
}
}
}
}
aans=f[][][];
for(i=;i<=m;i++)
aans=min(aans,f[n][cas][i]);
if(aans==f[][][])puts("-1");
else printf("%I64d\n",aans);
}
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 (DP)
题意:给定n棵树,其中有一些已经涂了颜色,然后让你把没有涂色的树涂色使得所有的树能够恰好分成k组,让你求最少的花费是多少. 析:这是一个DP题,dp[i][j][k]表示第 i 棵树涂第 j 种颜色恰 ...
- CodeForces 711C Coloring Trees
简单$dp$. $dp[i][j][k]$表示:前$i$个位置染完色,第$i$个位置染的是$j$这种颜色,前$i$个位置分成了$k$组的最小花费.总复杂度$O({n^4})$. #pragma com ...
- 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 Round #369 (Div. 2) C. Coloring Trees 动态规划
C. Coloring Trees 题目连接: http://www.codeforces.com/contest/711/problem/C Description ZS the Coder and ...
- 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 ...
随机推荐
- Oracle修改被占用的临时表结构
这两天在修改临时表的类型时,提示”attempt to create,alter or drop an index on temporary table already in use“的错误,由于临时 ...
- Ant学习笔记(1) 基础知识
Ant Apache Ant 是一个基于 Java的构建工具. 下载Ant google.baidu.Windows用户下载zip格式.解压即可. Windows安装Ant Ant本质上是一个Java ...
- 11g 创建asm磁盘组
[root@Oracle11g ~]# fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes255 heads, 63 sectors/track, 2 ...
- javascript的框架演化
说起javascript不同的人或许有不同的看法,一些资深后台程序员在刚开始的时候根本没有把它当作是一门编程语言,但是随着后面js框架的出现,以及面向对象的程序设计,还有原型,闭包的不断使用,后台程序 ...
- inno setup 多语言安装
之前的安装程序默认语言为英文,现在我们需要将它变成中文,由于InnoSetup安装包中默认没有带中文语言文件,我们需要下载一个先: 到http://www.400gb.com/u/758954/123 ...
- 一个IP支持多个网站实例(Apache2、Ubuntu相关)
http://www.blogjava.net/Andyluo/archive/2009/08/24/21821.html http://blog.csdn.net/zltianhen/article ...
- js 中的流程控制-条件语句
条件语句: if(exp)执行一句代码 <script> var x = 1 ; if(x == 1 ) //当if判断语句结果是true 或者 false 当判断结果等于true的时候, ...
- dedecms由子目录访问修改为根目录访问
现在我需要将原来位于xampp/htdocs/zm下的网站修改到D:/lyh/webhome目录下访问,原来的访问路径为http://localhost/zm,现在为http://www.yihui. ...
- MySQL中删除重复数据只保留一条
用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 SELECT ...
- php获取https下的内容
直接用file_get_contents,会报错: $url = ('https://xxx.com"); file_get_contents($url); 错误: Warning: fil ...