关闭

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

标签: codeforces
34人阅读 评论(0) 收藏 举报
分类:

http://codeforces.com/problemset/problem/711/C

#include<iostream>
#include<algorithm>
#define LL long long
#define inf 1e17+1
using namespace std;

const int maxn = 100 + 10;
LL p[maxn][maxn];
LL dp[maxn][maxn][maxn];
int c[maxn];

int main()
{
    int n, m, kk;
    cin >> n >> m >> kk;
    for (int i = 1; i <= n; i++)
        cin >> c[i];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> p[i][j];
    for (int i = 0; i <= 100; i++)
        for (int j = 0; j <= 100; j++)
            for (int k = 0; k <= 100; k++)
                dp[i][j][k] = inf;
    dp[0][0][0] = 0;
    for (int i = 1; i <= n; i++)
        if (c[i] != 0)
            for (int j = 0; j <= m; j++)
                for (int k = 0; k <= i; k++)
                    if (c[i] == j)
                        dp[i][c[i]][k] = min(dp[i - 1][j][k], dp[i][c[i]][k]);
                    else
                        dp[i][c[i]][k + 1] = min(dp[i - 1][j][k], dp[i][c[i]][k + 1]);
        else
            for (int t = 1; t <= m; t++)
                for (int j = 0; j <= m; j++)
                    for (int k = 0; k <= i; k++)
                        if (t == j)
                            dp[i][t][k] = min(dp[i - 1][j][k] + p[i][t], dp[i][t][k]);
                        else
                            dp[i][t][k + 1] = min(dp[i - 1][j][k] + p[i][t], dp[i][t][k + 1]);
    LL ans = inf;
    for (int i = 0; i <= m; i++)
        ans = min(dp[n][i][kk], ans);
    if (ans == inf)
        cout << "-1" << endl;
    else
        cout << ans << endl;
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:226次
    • 积分:40
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档