Algorithm
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.
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.
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.
3 2 2
0 0 0
1 2
3 4
5 6
10
3 2 2
2 1 2
1 3
2 4
3 5
-1
3 2 2
2 0 0
1 3
2 4
3 5
5
3 2 3
2 1 2
1 3
2 4
3 5
0
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.
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int n, m, k, a[101], p[101][101];
long long dp[101][105][101];
long long rec(int idx, int lst, int cur) {
if(idx == n) {
if(cur == k)
return 0;
return 2e14;
}
long long &ret = dp[idx][lst + 1][cur];
if(ret != -1)
return ret;
ret = 2e14;
if(idx == 0) {
if(a[idx] != -1)
return ret = rec(idx + 1, a[idx], cur + 1);
for(int i = 0; i < m; ++i)
ret = min(ret, rec(idx + 1, i, cur + 1) + p[idx][i]);
return ret;
}
if(a[idx] != -1)
return ret = rec(idx + 1, a[idx], cur + (lst != a[idx]));
for(int i = 0; i < m; ++i)
ret = min(ret, rec(idx + 1, i, cur + (lst != i)) + p[idx][i]);
return ret;
}
void build_path(int idx, int lst, int cur) {
if(idx == n) {
return;
}
long long opt = rec(idx, lst, cur);
if(idx == 0) {
if(a[idx] != -1) {
build_path(idx + 1, a[idx], cur + 1);
return;
}
for(int i = 0; i < m; ++i) {
if(rec(idx + 1, i, cur + 1) + p[idx][i] == opt) {
cout << i +1 << ' ';
build_path(idx + 1, i, cur + 1);
return;
}
}
}
if(a[idx] != -1) {
cout << a[idx] + 1 << ' ';
build_path(idx + 1, a[idx], cur + (lst != a[idx]));
return;
}
for(int i = 0; i < m; ++i) {
if(opt == rec(idx + 1, i, cur + (lst != i)) + p[idx][i]) {
cout << i+ 1 << ' ';
build_path(idx + 1, i, cur + (lst != i));
return;
}
}
}
int main() {
cin >> n >> m >> k;
int zeros = 0;
for(int i = 0; i < n; ++i) {
cin >> a[i];
zeros += (a[i] == 0);
}
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
cin >> p[i][j];
for(int i = 0; i < n; ++i)
--a[i];
memset(dp, -1, sizeof dp);
long long ans = rec(0, a[0], 0);
if(ans >= 2e14)
cout << -1 << endl;
else
cout << ans << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
0 0 0
1 2
3 4
5 6
Output
Demonstration
Codeforces Solution-Coloring Trees-Solution in C, C++, Java, Python