zoukankan      html  css  js  c++  java
  • Codeforces 677C. Coloring Trees dp

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

    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.

    题目连接:http://codeforces.com/contest/711/problem/C


    题意:有n棵树,m种染料。从1到n给未染色的树染色。第i棵树染第ci种色。ci=0表示第i棵树未染色。pi,j表示第i棵树染第j种颜色需要花费的价值。求出1到n全部染色需要花费最少的价值,必须保证有k段颜色。

    思路:动态规划。dp[i][j][t]表示前i棵树,第i棵树染第j中染色,有t段颜色的最少花费。如果当前选择的颜色与上一棵树的不同t+1,相同t不变。


    代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 const __int64 INF=1e12;
     5 __int64 c[200],p[200][200],dp[200][200][200];
     6 int main()
     7 {
     8     int i,j,t,h,n,m,k;
     9     scanf("%d%d%d",&n,&m,&k);
    10     for(i=1; i<=n; i++) scanf("%I64d",&c[i]);
    11     for(i=1; i<=n; i++)
    12         for(j=1; j<=m; j++)
    13             scanf("%I64d",&p[i][j]);
    14     if(k>n)
    15     {
    16         cout<<"-1"<<endl;
    17         return 0;
    18     }
    19     for(i=0; i<=n; i++)
    20         for(j=0; j<=m; j++)
    21             for(t=0; t<=n; t++)
    22                 dp[i][j][t]=INF;
    23     if(c[1]==0)
    24         for(j=1; j<=m; j++)
    25             dp[1][j][1]=p[1][j];
    26     else dp[1][c[1]][1]=0;
    27     for(i=2; i<=n; i++)
    28     {
    29         if(c[i]==0)
    30         {
    31             for(j=1; j<=m; j++)
    32                 for(t=1; t<=i; t++)
    33                     for(h=1; h<=m; h++)
    34                     {
    35                         if(j==h)
    36                             dp[i][j][t]=min(dp[i][j][t],dp[i-1][h][t]+p[i][j]);
    37                         else
    38                             dp[i][j][t]=min(dp[i][j][t],dp[i-1][h][t-1]+p[i][j]);
    39                     }
    40         }
    41         else
    42         {
    43             for(t=1; t<=i; t++)
    44                 for(h=1; h<=m; h++)
    45                 {
    46                     if(c[i]==h)
    47                         dp[i][c[i]][t]=min(dp[i][c[i]][t],dp[i-1][h][t]);
    48                     else
    49                         dp[i][c[i]][t]=min(dp[i][c[i]][t],dp[i-1][h][t-1]);
    50                 }
    51         }
    52     }
    53     __int64 ans=INF;
    54     for(j=1; j<=m; j++)
    55         ans=min(ans,dp[n][j][k]);
    56     if(ans==INF) cout<<"-1"<<endl;
    57     else printf("%I64d
    ",ans);
    58     return 0;
    59 }
    View Code
    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    Mysql 主主复制失败恢复【转】
    linux的curl用法【转】
    做Mysql主从时,注意使用replicate_wild_do_table和replicate-wild-ignore-table【转】
    MyEclipse 2014配置Maven
    使用MyEclipse 2014构建Maven项目的两种方法
    Struts2的输入校验
    Java Web之Filter
    Struts2国际化
    Struts2实现登录流程
    Java实现文件MD5加密
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/5854437.html
Copyright © 2011-2022 走看看