题目链接:

  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的更多相关文章

  1. codeforces 711C Coloring Trees(DP)

    题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...

  2. CodeForces 711C Coloring Trees (DP)

    题意:给定n棵树,其中有一些已经涂了颜色,然后让你把没有涂色的树涂色使得所有的树能够恰好分成k组,让你求最少的花费是多少. 析:这是一个DP题,dp[i][j][k]表示第 i 棵树涂第 j 种颜色恰 ...

  3. CodeForces 711C Coloring Trees

    简单$dp$. $dp[i][j][k]$表示:前$i$个位置染完色,第$i$个位置染的是$j$这种颜色,前$i$个位置分成了$k$组的最小花费.总复杂度$O({n^4})$. #pragma com ...

  4. Codeforces 677C. Coloring Trees dp

    C. Coloring Trees time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  5. Code Forces 711C Coloring Trees

    C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. Coloring Trees CodeForces - 711C

    Coloring Trees CodeForces - 711C 题意:有n个点,每个点有一个c值,如果为0表示它没有被染色,否则表示它被染成了c值的颜色.颜色有1到m.把第i棵树染成颜色j所需要的代 ...

  7. Codeforces Round #369 (Div. 2) C. Coloring Trees 动态规划

    C. Coloring Trees 题目连接: http://www.codeforces.com/contest/711/problem/C Description ZS the Coder and ...

  8. codeforces 711C C. Coloring Trees(dp)

    题目链接: C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. 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 ...

随机推荐

  1. Java-Android 之电话拨号源码

    file:///F:/workspace3/Android_ver2.4/src/cn/szy/com/MainActivity.java package cn.szy.com; import and ...

  2. WisDom.Net 框架设计(二) 服务总线

    WisDom.Net 框架设计--服务总线 1.Soa 简介     soa 就是面向服务的体系结构 是一个组件模型,不同的组件之间通过定义良好的接口联系起来.就像盖房子一块砖头一块砖头的砌墙,一片一 ...

  3. Registry 类

    提供表示 Windows 注册表中的根项的 RegistryKey 对象,并提供访问项/值对的 static 方法. 继承层次结构 System.Object   Microsoft.Win32.Re ...

  4. &amp与&

       

  5. 比较两份文件的异同-beyond compare

    比较两份文件的异同-beyond compare 今天又学到一个知识,比较两次文件的 异同.  哈哈 谢谢领导

  6. Typescript 团队合作的利器

    前言 在介绍Typescript 之前,我需要隆重介绍一个人: 安德斯·海尔斯伯格(Anders Hejlsberg,1960.12~),丹麦人,Turbo Pascal编译器的主要作者,Delphi ...

  7. C# RSA加密/解密

    RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力的公钥加密算法, ...

  8. idea配置tomcat.md

    [toc] 1.打开Edit Configurations,可以通过万能搜索快速进入!!! 2.添加服务器,在左上角找到Tomcat并添加 3.配置发布路径,Server标签页中填写完名称和路径,在D ...

  9. 深入理解ReentrantLock

    在Java中通常实现锁有两种方式,一种是synchronized关键字,另一种是Lock.二者其实并没有什么必然联系,但是各有各的特点,在使用中可以进行取舍的使用.首先我们先对比下两者. 实现: 首先 ...

  10. linux 部分命令简单使用介绍-ssh、scp、less、tail、find、grep(持续添加)

    ssh 加密的网络协议,提供客户-服务模式. 登录                        ssh username@ip                        ssh ip #不提供用 ...