简单$dp$。

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

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-; LL INF=1e17;
LL dp[][][];
LL p[][];
int c[],n,m,k; int main()
{
scanf("%d%d%d",&n,&m,&k); for(int i=;i<=n;i++) scanf("%d",&c[i]); for(int i=;i<=n;i++)
for(int j=;j<=m;j++) scanf("%lld",&p[i][j]); for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
for(int s=;s<=k;s++)
{
dp[i][j][s]=INF;
}
}
} if(c[]!=) dp[][c[]][]=;
else
{
for(int j=;j<=m;j++) dp[][j][]=p[][j];
} for(int i=;i<=n;i++)
{
if(c[i]==)
{
for(int j=;j<=m;j++)
{
for(int s=;s<=i;s++)
{
LL tmp=INF;
tmp=min(tmp,dp[i-][j][s]);
for(int t=;t<=m;t++)
{
if(t==j) continue;
tmp=min(tmp,dp[i-][t][s-]);
}
if(tmp==INF) continue;
else dp[i][j][s]=tmp+p[i][j];
}
}
} else
{
for(int s=;s<=i;s++)
{
LL tmp=INF;
tmp=min(tmp,dp[i-][c[i]][s]);
for(int t=;t<=m;t++)
{
if(t==c[i]) continue;
tmp=min(tmp,dp[i-][t][s-]);
} if(tmp==INF) continue;
dp[i][c[i]][s]=tmp;
}
}
} LL ans=INF;
for(int j=;j<=m;j++) ans=min(ans,dp[n][j][k]); if(ans==INF) printf("-1\n");
else printf("%lld\n",ans); 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

    题目链接: http://codeforces.com/problemset/problem/711/C 题目大意: 给N棵树,M种颜色,已经有颜色的不能涂色,没颜色为0,可以涂色,每棵树I涂成颜色J ...

  3. CodeForces 711C Coloring Trees (DP)

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

  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 711C C. Coloring Trees(dp)

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

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

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

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

随机推荐

  1. iOS证书深究

    iOS证书深究 iOS的系列证书很令人头痛,但是也提供了完整的保护. 在开发过程中,遇到的基本的证书有Xcode真机调试或者打包用证书,也有消息推送证书等:每种证书分为开发版(development) ...

  2. 关于Grunt可视化的尝试

    关于Grunt可视化的尝试 使用Grunt遇到的问题? 必须要安装NodeJS 必须安装grunt-cli 需要编写复杂的Gruntfile.js规则 每个项目中必须存在nodejs的grunt模块 ...

  3. DOM处理

    DOM处理 这几天整理了一下思路,本来觉得DOM部分会有很多东西,但是忽然发现频繁使用的其实并不太多 class class处理部分主要有四个 hasClass:检查元素是否包含某个class add ...

  4. RTB撕开黑盒子 Part 0:Pacing: is everyone doing it wrong?

    曾尝试为我们的RTB客户解决过Pacing问题,Pacing问题要解决的问题是:如果一个客户给你一笔预算,让你去运营一个广告推广计划,在一定的时间内投放广告,将这笔预算在指内的时间内,比较均匀地将预算 ...

  5. Cookie 操作工具类

    import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet ...

  6. SVN 在 Xcode中的状态说明

    最近同事总是问我关于SVN状态的问题,‘C’是什么意思啦?‘A’是什么意思啦?等等一系列问题. 为了方便以后查阅,以及新同事的快速融入,特在此记录一下^_^. 当然了大家也可以google一下,一搜一 ...

  7. Arduino 各种模块篇 震动模块 vibrator

    vibrator is a good thing. it has multi-funtionality . :) Now the  vibrator we choose is the one whic ...

  8. java链接mysql数据库

    package com.DateSystem; import java.sql.Connection; import java.sql.DriverManager; import java.sql.S ...

  9. 方法object面试题分析:7JAVA中Object的clone方法详解-克隆-深克隆

    时间紧张,先记一笔,后续优化与完善.     每日一道理 翻开早已发黄的页张,试着寻找过去所留下的点点滴滴的足迹.多年前的好友似乎现在看来已变得陌生,匆忙之间,让这维持了多年的友谊变淡,找不出什么亲切 ...

  10. 服务器如何防ssh服务暴力破解??

     如图: 当我们遭到暴力破解ssh服务该怎么办 内行看门道 外行看热闹  下面教大家几招办法: 1 密码足够的复杂,密码的长度要大于8位最好大于20位.密码的复杂度是密码要尽可能有数字.大小写字母和特 ...