题目链接:

  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. PL/SQL 批量SQL

    批量SQL包括: FORALL语句 BULK COLLECT子句 FORALL语句 FORALL具有如下结构: FORALL loop_counter IN bounds_clause [SAVE E ...

  2. mysql隐藏文件一定要删除彻底

    之前部署自己的服务器机器的时候 机器的mysql密码是不知道的.彻底删除了软件之后还是解决不了问题,而且我把MYSQL在C盘的隐藏文件也给删除了.但是还是不行 最后我偶然发现一个方法去找隐藏问题.我之 ...

  3. 玩javaweb的web.xml编译路径

    有时候能够碰到这样的情况 缓存就是 清不掉 那就可以去寻找编译路径了 <Context docBase="E:\java-workspace\eigyo_com405" pa ...

  4. java 基本类型和包装类的比较

    public class BoxingTest { @Test public void test1(){ String a = new String("1"); String b ...

  5. retain two decimal digits.

    package kju.o; import static kju.print.Printer.*; import java.text.*; class MathDemo { public static ...

  6. org.springframework.beans.factory.BeanCreationException: 求教育!

    2014-11-26 14:05:56 [org.springframework.web.context.support.XmlWebApplicationContext]-[WARN] Except ...

  7. oracle-同义词,又学到东西了

    select  col1  from tab1@db_link1; create or replace synonym test123 as se,ect col1 from tab1@db_link ...

  8. Codeforces 527E Data Center Drama(欧拉回路)

    题意: 给定一个无向图连通图,把这个的无向边变成有向边,并添加最少的有向边使这个图每个结点的出度为偶数. Solution: 题目很长,并且很多条件说的不太直接,确实不太好懂. 首先先看得到的无向图, ...

  9. java 全角、半角字符串转换

    转自:http://www.cnblogs.com/modou/articles/2679815.html     加入了空字符串的验证 半角转全角的方法: /** * @Title: ToSBC * ...

  10. [css][移动设备]禁止横竖屏时内容自动调整

    参考:http://www.kankanews.com/ICkengine/archives/106643.shtml iOS下当竖屏转向横屏的时候,发现内容字体会自动变大,通过各种方法设置字体大小都 ...