【动态规划】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 ...
随机推荐
- centos6.5 64位 openvpn安装配置
1 查看系统版本 2 cat /etc/redhat-release 3 CentOS release 6.5 (Final) 4 5 查看内核和cpu架构 6 uname -rm 7 2.6.32- ...
- poj 3565 ants
/* poj 3565 递归分治 还有用KM的做法 这里写的分治 按紫书上的方法 不过那里说的有点冗杂了 可以简化一下 首先为啥可以分治 也就是分成子问题解决 只要有一个集合 黑白的个数相等 就一定能 ...
- HttpContext.Current
HttpContext. Response 直接这样写会报错 是因为 httpcontext没有提供response 这个静态的方法. 通过这样写就可以 ASP.NET还为它提供了一个静态属性Http ...
- 【转】iOS-Core-Animation-Advanced-Techniques(二)
原文: http://www.cocoachina.com/ios/20150104/10816.html 视觉效果和变换 (四)视觉效果 嗯,园和椭圆还不错,但如果是带圆角的矩形呢? 我们现在能做到 ...
- [转]PHP echo, print, printf, sprintf函数的区别和使用
1. echo函数: 输出函数,是命令,不能返回值.echo后面可以跟很多个参数,之间用分号隔开,如: echo $myvar1; echo 1,2,$myvar,"<b>bol ...
- 51nod建设国家
小C现在想建设一个国家.这个国家中有一个首都,然后有若干个中间站,还有若干个城市. 现在小C想把国家建造成这样的形状:选若干(可以是0个)的中间站把他们连成一条直线,然后把首都连在这一条直线的左端.然 ...
- javascript删除目标div tr 等
var delTr = document.getElementById("要删除的位置"); // 获取要删除的位置”对象“ delTr.parentNode.removeChil ...
- 【技术宅6】把一个无限级分类循环成tree结构
function list_to_tree($list,$root=0,$pk='cid',$pid = 'pid',$child = '_child'){ if(is_array($list)) { ...
- 用Web Picasa API搭建站内相册
在flickr时代,为了专门把站内嵌入相册,还专门写了一篇文章把Flickr相册搬回家.flickr被墙之后,我就把个人相册转到了Web Picasa上.用Picasa Web就简单多了,官方提供了S ...
- CentOS 6.8安装Python2.7.13
查看当前系统中的 Python 版本 python --version 返回 Python 2.6.6 为正常. 检查 CentOS 版本 cat /etc/redhat-release 返回 Cen ...