HDU Hatsune Miku (DP)

Source: Internet
Author: User
Hatsune Miku

Time Limit: 2000/1000 MS (Java/others) memory limit: 262144/262144 K (Java/Others)
Total submission (s): 162 accepted submission (s): 129


Problem descriptionhatsune Miku is a popular virtual singer. It is very popular in both Japan and China. Basically it is a computer software that 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 are only m different notes provided in the package. And you want to make a song with N notes.

Also, you know that there is a system to evaluate the beautifulness of a song. for each two consecutive notes a and B, if B comes after a, then the beautifulness for these two notes is evaluated as score (A, B ).

So the total beautifulness for a song consisting of notes A1, A2 ,..., an, is simply the sum of score (AI, AI + 1) for 1 ≤ I ≤ n-1.

Now, you find that at some positions, the notes have to be some specific ones, but at other positions you can decide what notes to use. you want to maximize your song's beautifulness. what is the maximum beautifulness you can achieve?

 

Inputthe first line contains an integer T (T ≤ 10), denoting the number of the test cases.

For each test case, the first line contains two 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 you cannot change, while negative integers are what you can replace with arbitrary notes. the notes are named from 1 to M.

 

Outputfor each test case, output the answer in one line.

 

Sample input2 5 3 83 86 77 15 93 35 86 92 49 3 3 3 1 2 10 5 36 11 68 29 82 30 62 23 67 35 29 2 22 58 69 67 93 56 11 42 29 73 21 19-1-1 5-1 4-1-1-1 4-1

 

Sample output270 625 is a common two-dimensional DP, which is still very unfamiliar. Here we should note that during DP [I] [J, if DP [I-1] [J] is-1, note that calculation of DP [I] [J] should not take it into consideration. (Here there may be the maximum DP [I-1] [J] + max (score [J] [k]), so we need an if to constrain it !) Status DP [I] [k] indicates the code of the item with the id k placed in the I-th position:
1 // ============================================== =======================================================2 // Name: 1.cpp 3 // Author: 4 // version: 5 // copyright: Your copyright notice 6 // Description: Hello world in C ++, ANSI-style 7 // ======================================== ========================================================== = 8 9 # include <iostream> 10 # include <algorithm> 11 # include <stdio. h> 12 # include <string. h> 13 # include <m Ath. h> 14 # include <stdlib. h> 15 # include <queue> 16 using namespace STD; 17 18 int score [110] [110], F [110]; 19 int DP [110] [110]; 20 int t, n, k; 21 22 int main () {23 // freopen ("in.txt", "r", stdin); 24 25 scanf ("% d ", & T); 26 while (t --) {27 scanf ("% d", & N, & K); 28 for (INT I = 1; I <= K; I ++) for (Int J = 1; j <= K; j ++) scanf ("% d ", & score [I] [J]); 29 for (INT I = 1; I <= N; I ++) scanf ("% d ", & F [I]); 30 31 memset (DP,-1, sizeof (DP); 32 If (F [1] =-1) 33 for (INT I = 1; I <= K; I ++) DP [1] [I] = 0; 34 else DP [1] [f [1] = 0; 35 for (INT I = 2; I <= N; I ++) {36 IF (F [I] =-1) {37 for (INT z = 1; Z <= K; Z ++) {38 int maxc =-1; 39 For (Int J = 1; j <= K; j ++) 40 if (DP [I-1] [J] + score [J] [Z]> maxc & DP [I-1] [J]! =-1) maxc = DP [I-1] [J] + score [J] [Z]; 41 DP [I] [Z] = maxc; 42} 43} 44 else {45 // special when it is a specific number! 46 int maxc =-1; 47 int z = f [I]; 48 if (F [I-1]! =-1) {49 maxc = 0; 50 for (int l = 1; L <= K; l ++) {51 if (DP [I-1] [l] =-1) maxc = maxc + 1; 52 maxc + = DP [I-1] [l]; 53} 54 maxc + = score [f [I-1] [Z]; 55} 56 else57 for (Int J = 1; j <= K; j ++) 58 If (DP [I-1] [J] + score [J] [Z]> maxc & DP [I-1] [J]! =-1) maxc = DP [I-1] [J] + score [J] [Z]; 59 DP [I] [Z] = maxc; 60 for (int zz = 1; ZZ <= K; ZZ ++) 61 If (zz! = Z) DP [I] [zz] =-1; 62} 63} 64 // For (INT I = 1; I <= N; I ++) {65 // For (Int J = 1; j <= K; j ++) 66 // printf ("% d", DP [I] [J]); 67 // printf ("\ n"); 68 //} 69 int maxc =-1; 70 for (INT I = 1; I <= K; I ++) 71 If (DP [N] [I]> maxc) maxc = DP [N] [I]; 72 printf ("% d \ n", maxc ); 73} 74 75 // cout <"!!! Hello world !!! "<Endl; // prints !!! Hello world !!! 76 return 0; 77}

 

HDU Hatsune Miku (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.