HDU 5074 Hatsune Miku

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5074


Surface:

Hatsune Miku Time limit:2000/1000 MS (java/others) Memory limit:262144/262144 K (java/others)
Total submission (s): 810 Accepted Submission (s): 577


Problem Descriptionhatsune 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, output 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

Source2014 Asia Anshan Regional Contest



Solving:

DP[I][J] represents the maximum value of J selected at position I. The outermost loop I represents the current position, if I is not fixed, a double-layer cycle, respectively cyclic Dp[i+1][j]=max (dp[i+1][j],dp[i][k]+val[k][j]); If the current point is fixed, the single-layer loop K,dp[i+1][j]=max ( DP[I+1][J],DP[I][K]+VAL[K][J]). Also consider whether the subsequent points have been fixed.


Code:

#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib>using namespace std; int val[55][55],dp[105][55],pos[105],p,ans;int t,n,m;int max (int a,int b) {return  a>b?a:b;} int main () {    int t;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&m); Memset (Val,0,sizeof (Val)); memset (Dp,0,sizeof (DP)); for (int. i=1;i<=m;i++) {for (int j=1;j<=m;j++) scanf ("%d", &val[i][j]);} for (int i=1;i<=n;i++) {scanf ("%d", &pos[i]);} for (int i=2;i<=n;i++) {p=pos[i];if (pos[i]>0) {if (pos[i-1]>0)    dp[i][p]=dp[i-1][pos[i-1]]+val[pos[i-1] ][p];elsefor (int j=1;j<=m;j++) Dp[i][p]=max (Dp[i][p],dp[i-1][j]+val[j][p]);} Else{if (pos[i-1]>0) for    (int j=1;j<=m;j++) dp[i][j]=dp[i-1][pos[i-1]]+val[pos[i-1]][j];else{for (int j=1; j<=m;j++) for (int k=1;k<=m;k++) Dp[i][j]=max (Dp[i][j],dp[i-1][k]+val[k][j]);}} ans=0;for (int i=1;i<=m;i++) Ans=max (Ans,dp[n][i]);p rintf ("%d\n", ans);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5074 Hatsune Miku

Related Keywords:

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.