国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

【Codeforces 711 C】+ dp

2019-11-08 02:52:26
字體:
來源:轉載
供稿:網友

C. Coloring Trees time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard 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.

dp[i][j][k] 表示遍歷到第 i 棵樹分成 j 段 最后一棵顏色為 k 的最優解

最后一棵樹顏色為 l ,當前樹涂色為k的時; 若第 i 棵樹已經涂色 : k == l dp[i][j][k] = min(dp[i][j][k],dp[i - 1][j][k]); k != l dp[i][j + 1][k] = min(dp[i][j + 1][k] ,dp[i -1][j][l]; 若還未涂色 : k == l dp[i][j][k] = min(dp[i][j][k],dp[i - 1][j][k] + v[i][k]; k != l dp[i][j + 1][k] = min(dp[i][j + 1][k],dp[i - 1][j][k] + v[i][k]);

AC代碼:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const LL INF = 0x3f3f3f3f3f3f3f3f;const LL inf = (LL) 1 << 62;const int K = 110;LL dp[K][K][K],v[K][K],a[K];int main(){ int N,M,K; scanf("%d %d %d",&N,&M,&K); for(int i = 1; i <= N; i++) scanf("%lld",&a[i]); for(int i = 1; i <= N; i++) for(int j = 1 ; j <= M; j++) scanf("%lld",&v[i][j]); memset(dp,INF,sizeof(dp)); if(a[1]) dp[1][1][a[1]] = 0; else for(int i = 1 ; i <= M; i++) dp[1][1][i] = v[1][i]; for(int i = 2 ; i <= N; i++) if(a[i]){ for(int j = 1 ; j < i; j++) for(int l = 1 ; l <= M; l++) if(l != a[i]) dp[i][j + 1][a[i]] = min(dp[i][j + 1][a[i]],dp[i - 1][j][l]); else dp[i][j][l] = min(dp[i][j][l],dp[i - 1][j][l]); } else{ for(int j = 1 ; j < i; j++) for(int p = 1 ; p <= M; p++) for(int l = 1; l <= M; l++) if(l != p) dp[i][j + 1][p] = min(dp[i][j + 1][p],dp[i - 1][j][l] + v[i][p]); else dp[i][j][l] = min(dp[i][j][l],dp[i - 1][j][l] + v[i][p]); } LL ans = dp[N][K][1]; for(int i = 2 ; i <= M; i++) ans = min(ans,dp[N][K][i]); if(ans == INF) printf("-1/n"); else printf("%lld/n",ans); return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武宁县| 肥西县| 西贡区| 嵊泗县| 财经| 松原市| 和龙市| 宝应县| 阳高县| 吉安县| 介休市| 汝南县| 弥渡县| 南漳县| 迁西县| 新余市| 嘉善县| 广丰县| 祁阳县| 万州区| 界首市| 红安县| 嘉善县| 治多县| 竹山县| 泰宁县| 若尔盖县| 乐至县| 青冈县| 札达县| 桂林市| 孟津县| 宜兰市| 淮南市| 利辛县| 徐水县| 班玛县| 湖南省| 秦皇岛市| 东港市| 甘德县|