###### codeforces 711C Coloring Trees（DP）

O（n^4）的复杂度，以为会超时的

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf=1e15;
int c[105];
int p[105][105];
ll dp[105][105][105];
int main()
{
int n,m,b;
scanf("%d%d%d",&n,&m,&b);
for(int i=1;i<=n;i++)
scanf("%d",&c[i]);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
scanf("%d",&p[i][j]);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=b;j++)
{
for(int k=1;k<=m;k++)
dp[i][j][k]=inf;
}
}
for(int i=1;i<=n;i++)
{
if(c[i])
{
for(int j=1;j<=b&&j<=i;j++)
{
int k=c[i];
ll min1=inf,min2=inf;
if(k>1&&j>1)
min1=*min_element(dp[i-1][j-1]+1,dp[i-1][j-1]+k);
if(k<m&&j>1)
min2=*min_element(dp[i-1][j-1]+k+1,dp[i-1][j-1]+m+1);
dp[i][j][k]=min(dp[i-1][j][k],min(min1,min2));
}
}
else
{
for(int j=1;j<=b&&j<=i;j++)
{
for(int k=1;k<=m;k++)
{
ll min1=inf,min2=inf;
if(k>1&&j>1)
min1=*min_element(dp[i-1][j-1]+1,dp[i-1][j-1]+k);
if(k<m&&j>1)
min2=*min_element(dp[i-1][j-1]+k+1,dp[i-1][j-1]+m+1);
dp[i][j][k]=p[i][k]+min(dp[i-1][j][k],min(min1,min2));
}
}
}
}
ll ans=*min_element(dp[n][b]+1,dp[n][b]+m+1);
if(ans<inf)
printf("%I64d\n",ans);
else
printf("-1\n");
return 0;
}

#### 【动态规划】Codeforces 711C Coloring Trees

2016-08-30 14:52:47

#### CodeForces 711C.Coloring Trees【DP】

2016-08-30 01:03:50

#### Codeforces Round #369 (Div. 2) [C] Coloring Trees

2016-08-30 12:58:40

#### Codeforces 369 C. Coloring Trees (dp)

2016-09-02 22:34:23

#### CodeForces 711C Coloring Trees(DP)

2016-09-22 16:12:55

#### Codeforces 711C Coloring Trees DP

2017-01-06 11:28:45

#### CodeForces 711C - Coloring Trees DP

2016-08-30 21:32:03

#### Codeforces 711C Coloring Trees

2016-09-05 00:53:20

#### codeforces 711C - Coloring Trees

2016-08-30 22:09:47

#### CodeForces - 711C Coloring Trees 三维DP

2017-03-11 21:41:12

## 不良信息举报

codeforces 711C Coloring Trees（DP）