您的位置:首页 > 其它

CodeForces - 711C Coloring Trees(DP)(思维)

2017-03-23 08:53 387 查看

Coloring Trees

ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park where n trees grow. They decided to be naughty and color the trees in the park. The trees are numbered with integers from 1 to n from left to right.

Initially, tree i has color ci. ZS the Coder and Chris the Baboon recognizes only m different colors, so 0 ≤ ci ≤ m, where ci = 0 means that tree i is uncolored.

ZS the Coder and Chris the Baboon decides to color only the uncolored trees, i.e. the trees with ci = 0. They can color each of them them in any of the m colors from 1 to m. Coloring the i-th tree with color j requires exactly pi, j litres of paint.

The two friends define the beauty of a coloring of the trees as the minimum number of contiguous groups (each group contains some subsegment of trees) you can split all the n trees into so that each group contains trees of the same color. For example, if the colors of the trees from left to right are 2, 1, 1, 1, 3, 2, 2, 3, 1, 3, the beauty of the coloring is 7, since we can partition the trees into 7 contiguous groups of the same color : {2}, {1, 1, 1}, {3}, {2, 2}, {3}, {1}, {3}.

ZS the Coder and Chris the Baboon wants to color all uncolored trees so that the beauty of the coloring is exactly k. They need your help to determine the minimum amount of paint (in litres) needed to finish the job.

Please note that the friends can’t color the trees that are already colored.

Input

The first line contains three integers, n, m and k (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of trees, number of colors and beauty of the resulting coloring respectively.

The second line contains n integers c1, c2, …, cn (0 ≤ ci ≤ m), the initial colors of the trees. ci equals to 0 if the tree number i is uncolored, otherwise the i-th tree has color ci.

Then n lines follow. Each of them contains m integers. The j-th number on the i-th of them line denotes pi, j (1 ≤ pi, j ≤ 109) — the amount of litres the friends need to color i-th tree with color j. pi, j’s are specified even for the initially colored trees, but such trees still can’t be colored.

Output

Print a single integer, the minimum amount of paint needed to color the trees. If there are no valid tree colorings of beauty k, print  - 1.

Examples

input

3 2 2

0 0 0

1 2

3 4

5 6

output

10

input

3 2 2

2 1 2

1 3

2 4

3 5

output

-1

input

3 2 2

2 0 0

1 3

2 4

3 5

output

5

input

3 2 3

2 1 2

1 3

2 4

3 5

output

0

Note

In the first sample case, coloring the trees with colors 2, 1, 1 minimizes the amount of paint used, which equals to 2 + 3 + 5 = 10. Note that 1, 1, 1 would not be valid because the beauty of such coloring equals to 1 ({1, 1, 1} is a way to group the trees into a single group of the same color).

In the second sample case, all the trees are colored, but the beauty of the coloring is 3, so there is no valid coloring, and the answer is  - 1.

In the last sample case, all the trees are colored and the beauty of the coloring matches k, so no paint is used and the answer is 0.

ps:大牛们都说这是一道简单dp,WTF?为什么我觉得这么难,整整看了四五个小时才把题解看懂,这根本是我做过的最难的dp了好不好。。。虽然我只做过几道dp 0.0

参考博客:http://www.cnblogs.com/oscar-cnblogs/p/6435536.html

求到达某个状态的最小值,考虑用动态规划来求解

考虑到第i棵树的决策,它的颜色对答案及状态的贡献受两个因素的影响

1.上一棵树颜色的选取

2.考虑前i-1棵树已经分成了几段

定义dp[i][j][l]:表示当第i棵树涂完第j种颜色,形成l段时的最小消耗

如果第i棵树已经有了颜色

dp[i][color[i]][l]=min(dp[i-1][w!=color[i]][l-1],dp[i-1][color[i]][l]);

如果还未涂色,那么就有m种涂色的可能

dp[i][j][l]=min(dp[i-1][w!=j][l-1],dp[i-1][j][l])+cost[i][j]

其实主要思想就是如果当前涂色跟上一个涂色一样,那么dp[i][j][k]就由dp[i-1][j][k]得到,如果不一样,则由dp[i-1][w!=j][k-1]得到

另外注意初始化

代码:

#include<stdio.h>

#define min(a,b) (a<b?a:b)
#define LL long long int
#define maxn 105
const LL INF=0x3f3f3f3f3f3f3f3fL;

LL dp[maxn][maxn][maxn];
LL cost[maxn][maxn],color[maxn];
int n,m,k;

int main()
{
scanf("%d%d%d",&n,&m,&k);
int i,j,l;
for(i=1; i<=n; i++)
scanf("%I64d",&color[i]);
for(i=1; i<=n; i++)
for(int j=1; j<=m; j++)
scanf("%I64d",&cost[i][j]);
for(i=0; i<=n; i++)
for(j=0; j<=m; j++)
for(l=0; l<=k; l++)
dp[i][j][l]=INF;
dp[0][0][0]=0;//初始状态为0
for(i=1; i<=n; i++)
{
if(color[i])//已经涂过色了
{
for(l=1; l<=k; l++)
{
dp[i][color[i]][l]=min(dp[i][color[i]][l],dp[i-1][color[i]][l]);//当前颜色和上一个颜色一样
LL minn=INF;
for(j=0; j<=m; j++)//注意这个地方要从0开始,因为要保持所有状态都由初始状态得到
if(j!=color[i])
minn=min(minn,dp[i-1][j][l-1]);//当前颜色和上一个颜色不一样
dp[i][color[i]][l]=min(dp[i][color[i]][l],minn);
}
}
else//还未涂色
{
for(l=1; l<=k; l++)
for(j=1; j<=m; j++)//还未涂过色,要涂第j种颜色
{
dp[i][j][l]=min(dp[i][j][l],dp[i-1][j][l]+cost[i][j]);//当前颜色和上一个颜色一样
LL minn=INF;
for(int z=0; z<=m; z++)//同理
if(z!=j)
minn=min(minn,dp[i-1][z][l-1]);//当前颜色和上一个颜色不一样
dp[i][j][l]=min(dp[i][j][l],minn+cost[i][j]);
}
}
}
LL ans=INF;
for(i=1; i<=m; i++)
ans=min(ans,dp
[i][k]);
if(ans==INF)
printf("-1\n");
else
printf("%I64d\n",ans);
return 0;
}


总结:

dp的特征:

1.数值不是很大

2.求最优解

3.每一步的最优解都可以通过上一步的状态来得到
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: