[置顶] Hatsune Miku(较难dp)

Link:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1005&cid=549


Hatsune Miku

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
   
   
   
   
Hatsune 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. [置顶] Hatsune Miku(较难dp)_第1张图片 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 a 1, a 2, . . . , a n, is simply the sum of score(a i, a i+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?
 

Input
   
   
   
   
The 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, a 1, a 2, . . . , a n (-1 ≤ a i ≤ m, a i ≠ 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.
 

Output
   
   
   
   
For each test case, output the answer in one line.
 

Sample Input
   
   
   
   
2 5 3 83 86 77 15 93 35 86 92 49 3 3 3 1 2 10 5 36 11 68 67 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 Output
   
   
   
   
270 625
 



请教了M子学长,以下代码来自他的,觉得自己的动规掌握得不是很好,拿他的代码来借鉴,也在此对他表示感谢!大笑



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
#define SET(a,b) memset(a,b,sizeof(a))
#define MAXN (100+10)

int dp[MAXN],T,n,m,note[MAXN],score[MAXN][MAXN],one,p;


void do11()
{
    dp[note[p]]=dp[one]+score[one][note[p]];
    one=note[p];
    p++;
}

void don1()
{
    int maxx=0;
    for(int i=1;i<=m;i++)maxx=max(maxx,dp[i]+score[i][note[p]]);
    dp[note[p]]=maxx;
    one=note[p];
    p++;
}

void do1n()
{
    int x=dp[note[p-1]];
    for(int i=1;i<=m;i++)dp[i]=x+score[note[p-1]][i];
    p++;
}

void donn()
{
    int dp2[MAXN];
    for(int i=1;i<=m;i++)
    {
        int maxx=0;
        for(int j=1;j<=m;j++)
        {
            maxx=max(maxx,dp[j]+score[j][i]);
        }
        dp2[i]=maxx;
    }
    for(int i=1;i<=m;i++)dp[i]=dp2[i];
    p++;
}

int main(){
#ifdef DEBUG
   freopen("CBin.txt","r",stdin);
   //freopen("CBout.txt","w",stdout);
#endif
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=1;i<=m;i++)for(int j=1;j<=m;j++)cin>>score[i][j];
        for(int i=1;i<=n;i++)cin>>note[i];
        SET(dp,0);
        p=2;
        if(note[1]!=-1)one=note[1];

        if(note[1]!=-1&&note[2]!=-1)do11();
        else if(note[1]!=-1&&note[2]==-1)do1n();
        else if(note[1]==-1&&note[2]!=-1)don1();
        else if(note[1]==-1&&note[2]==-1)donn();

        for(int i=1;i<=n;i++){
            if(note[p-1]!=-1&&note[p]!=-1)do11();
            else if(note[p-1]!=-1&&note[p]==-1)do1n();
            else if(note[p-1]==-1&&note[p]!=-1)don1();
            else if(note[p-1]==-1&&note[p]==-1)donn();
        }
        int sum=0;
        for(int i=1;i<=m;i++)sum=max(sum,dp[i]);
        cout<<sum<<'\n';
    }
    return 0;
}




你可能感兴趣的:(算法,dp,ACM)