HDU 5074 Hatsune Miku [dp] ——2014鞍山现场赛E题

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.


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?
 

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, 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.
 

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×m的矩阵score,再给n个数a[n],第i-1和第i个数对应着矩阵中的一个分值score[a[i-1]][a[i]],如果a中某个元素是-1表示该元素可以取1~m的任意值。求最大可得的分值和。

d[i][j]表示第i个元素是j则有四种情况:

1. a[i]定,a[i-1]定

2. a[i]定,a[i-1]不定

3. a[i]不定,a[i-1]定

4. a[i]不定,a[i-1]不定

不定的情况跑个循环就够了


#include<iostream>
#include<cassert>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<iterator>
#include<cstdlib>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define debug(x) cout<<"debug "<<x<<endl;
#define rep(i,f,t) for(int i = (f),_end_=(t); i <= _end_; ++i)
#define rep2(i,f,t) for(int i = (f),_end_=(t); i < _end_; ++i)
#define dep(i,f,t) for(int i = (f),_end_=(t); i >= _end_; --i)
#define dep2(i,f,t) for(int i = (f),_end_=(t); i > _end_; --i)
#define clr(c, x) memset(c, x, sizeof(c) )
typedef long long int64;
const int INF = 0x5f5f5f5f;
const double eps = 1e-8;


//*****************************************************

int s[200][200];
int d[200][200];
int a[200];

int main()
{
    int T;
    int n,m;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        rep(i,1,m)rep(j,1,m)scanf("%d",&s[i][j]);
        rep(i,1,n)scanf("%d",&a[i]);
        clr(d,0);
        rep(i,2,n)
        {
            if(a[i] > 0){
                int j = a[i];
                if(a[i-1] > 0){
                    int k = a[i-1];
                    d[i][j] = d[i-1][k] + s[k][j];
                }else{
                    rep(k,1,m)
                        d[i][j] = max(d[i][j], d[i-1][k]+s[k][j]);
                }
            }else{
                rep(j,1,m)
                {
                    if(a[i-1] > 0){
                        int k = a[i-1];
                        d[i][j] = d[i-1][k] + s[k][j];
                    }else{
                        rep(k,1,m)
                            d[i][j] = max(d[i][j], d[i-1][k]+s[k][j]);
                    }
                }
            }
        }
        int ans = 0;
        if(a[n]>0)ans = d[n][a[n]];
        else{
            rep(i,1,m)ans = max(ans,d[n][i]);
        }

        printf("%d
",ans);
    }

    return 0;
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/DSChan/p/4861989.html