Hatsune mikutime limit:1000msmemory limit:262144kbthis problem would be judged onHDU. Original id:5074
64-bit integer IO format: %i64d Java class name: Main Hatsune Miku is a popular virtual singer. It is very popular in both Japan and China. Basically it is a computer software this allows you to compose a song on your own using the vocal package.
Today you want to compose a song, which is just a sequence of notes. There is only m different notes provided in the package. And you want the song with n notes.
Also, know that there are a system to evaluate the beautifulness of a song. For each of the consecutive notes A and B, if B comes after a, then the beautifulness for these the notes are evaluated as SCO Re (A, b).
So the total beautifulness for a song consisting of notes A1, A2, ..., an, was simply the sum of score (AI, ai+1) for 1≤ I≤n-1.
Now, your find that at some positions, the notes has to be some specific ones, but at other positions can decide what Notes to use. You want to maximize your song ' s beautifulness. What's the maximum beautifulness you can achieve?InputThe first line contains an integer T (t≤10), denoting the number of the the test cases.
For each test case, the first line contains the integers n (1≤n≤100) and M (1≤m≤50) as mentioned above. Then M lines follow, each of them consisting of M space-separated integers, the j-th integer in the i-th line for score (I, j) (0≤score (i, J) ≤100). The next line contains n integers, a1, A2, ..., an ( -1≤ai≤m, ai≠0), where positive integers stand for the notes Y OU cannot change, while negative integers is what you can replace with arbitrary notes. The notes is named from 1 to M.OutputFor each test case, the output of the answer in one line.Sample Input
25 383 86 7715 93 3586 92 493 3 3 1 210 536 11 68 67 2982 30 62 23 6735 29 2 22 5869 67 93 56 1142 29 73 21 19-1-1 5-1 4 -1-1-1 4-1
Sample Output
270625
SourceAnshan Asia Regional Contest problem solving: Dynamic planning and messing up
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN = the;4 intDP[MAXN][MAXN],SCORE[MAXN][MAXN],SONG[MAXN];5 intMain () {6 intn,m,kase;7scanf"%d",&Kase);8 while(kase--) {9scanf"%d%d",&n,&m);Ten for(inti =1; I <= m; ++i) One for(intj =1; J <= M; ++j) Ascanf"%d", Score[i] +j); - for(inti =1; I <= N; ++i) scanf ("%d", Song +i); -memset (dp,-1,sizeofDP); thedp[0][0] =0; - for(inti =1; I <= N; ++i) { - for(intj =0; J <= M; ++j) { - if(dp[i-1][J] = =-1)Continue; + if(Song[i] = =-1) { - for(intK =1; K <= m; ++k) +Dp[i][k] = max (dp[i][k],dp[i-1][J] +score[j][k]); A}ElseDp[i][song[i]] = max (dp[i][song[i]],dp[i-1][J] +Score[j][song[i]]); at } - } - intRET =0; - if(Song[n] = =-1) { - for(inti =1; I <= m; ++i) ret =Max (ret,dp[n][i]); -}ElseRET =Dp[n][song[n]]; inprintf"%d\n", ret); - } to return 0; +}View Code
HDU 5074 Hatsune Miku