【动态规划】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 ...
随机推荐
- html px em pt长度单位(像素 相对长度 点)知识(转)
html px em pt单位区 一.PX\EM\PT单位介绍 px单位名称为像素,相对长度单位,像素(px)是相对于显示器屏幕分辨率而言的国内推荐:em单位名称为相对长度单位.相对于当前对象内文本的 ...
- gulp完成javascript压缩合并,css压缩
最近需要对项目进行优化,主要是对js的压缩合并和css文件的压缩,查找相关资料之后发现gulp可以实现相关的功能,特此分享一下使用心得. 1.安装gulp gulp是基于Node.js的前端构建工具. ...
- 类 Array Arraylist List Hashtable Dictionary
总结C# 集合类 Array Arraylist List Hashtable Dictionary Stack Queue 我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashT ...
- linq学习笔记:将List<T> 转换为 Dictionary<T Key,T Value>
运用Linq,将List<T> 转换为 Dictionary<T Key,T Value> 即:List<T> ToDictionary<T Key,T V ...
- Nginx配置http强制跳转到https
目的:访问http://sdk.open.test.com/时强制自动跳转到https://sdk.open.test.com/ 修改nginx站点配置文件sdk.open.test.com.conf ...
- oracle批量导入数据
关键代码 OracleDataAdapter da=new OracleDataAdapter(); string sql_select = string.Format("select id ...
- IOS学习--UILable使用手册(20150120)
第一步:创建一个UILable对象 UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(, , , )]; 第二步:设置对象的各种属性 ...
- 【转】 IOS,objective_C中用@interface和 @property 方式声明变量的区别
原文: http://blog.csdn.net/ganlijianstyle/article/details/7924446 1.在 @interface :NSObject{} 的括号中,当然N ...
- 使用OC开发phonegp 组件
使用OC开发phonegp 组件 1. 使用oc 对phonegp中的组件近些开发,首先具体的pgonegp跟nativecode之间的一些优劣就不说了,开发phonegp 对应的组件主要就是使用na ...
- c++primerplus(第六版)编程题——第5章(循环和关系表达式)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...