2014ACM/ICPC亚洲区鞍山站-E||HDU - 5074

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5074

Hatsune Miku

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


 

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

 

 

Source

2014 Asia AnShan Regional Contest

 

 

Recommend

liuyiding

 

题意:给你一个m*m的矩阵,然后给你一个序列,序列里相邻的两个音符i,j的值为num(i,j)对应矩阵里面的i行j列,

然后数列里正整数是固定的,-1可以替换成不大于m的数,问你相邻音符的值加起来最大的和是多少       

思路:比赛的时候,画了画想找规律,写着写着就成DP了,

我们设立dp[i][j];i为当前的数链长度,j为当前数链末尾一位的字符,

que[]存给定的数链,如果是-1就认为他不合法,否则就是合法

然后分四种情况,

//两个都是合法数据,直接加,只有这一种情况
dp[i+1][que[i+1]]=dp[i][que[i]]+num(que[i],que[i+1]);
//第一位不合法,第二位合法,我们已知j,遍历一下前一个可能的,取最大值
for (int j = 1; j <= m; ++j)
{
    dp[i+1][que[i+1]]=max(dp[i+1][que[i+1]],dp[i][j]+num(j,que[i+1]));
}
//第一位合法,第二位不合法,已知前一状态,遍历赋值后一状态
for (int j = 1; j <= m; ++j)
{
    dp[i+1][j]=max(dp[i+1][j],dp[i][que[i]]+num(que[i],j));
}
//两个都不是合法数据二重循环方法同理
for (int j = 1; j <= m; ++j)//第一位
{
    for (int k = 1; k <= m; ++k)//第二位
    {
        dp[i+1][k]=max(dp[i+1][k],dp[i][j]+num(j,k));
    }
}

DP完就可以了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>

using namespace std;

#define inf 0x3f3f3f3f

const int maxn=1e9+7;

int data[58][58];
int que[186];
int dp[186][50];

int num(int a, int b)
{
    return data[a][b];
}

int main()
{
    int t;
    int n,m;
    scanf("%d",&t);
    while (t--)
    {
        memset(data,0, sizeof(data));
        memset(que,0, sizeof(que));
        memset(dp,0, sizeof(dp));
        scanf("%d %d",&n,&m);
        for (int i = 1; i <= m; ++i)
        {
            for (int j = 1; j <= m; ++j)
            {
                scanf("%d",&data[i][j]);
            }
        }
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d",&que[i]);
        }
        for (int i = 1; i <= n; ++i)
        {
            if (que[i]!=-1&&que[i+1]!=-1)//两个都是合法数据
            {
                dp[i+1][que[i+1]]=dp[i][que[i]]+num(que[i],que[i+1]);
            } else if (que[i]==-1&&que[i+1]!=-1)//第一位不合法,第二位合法
            {
                for (int j = 1; j <= m; ++j)
                {
                    dp[i+1][que[i+1]]=max(dp[i+1][que[i+1]],dp[i][j]+num(j,que[i+1]));
                }
            } else if (que[i]!=-1&&que[i+1]==-1)//第一位合法,第二位不合法
            {
                for (int j = 1; j <= m; ++j)
                {
                    dp[i+1][j]=max(dp[i+1][j],dp[i][que[i]]+num(que[i],j));
                }
            } else if (que[i]==-1&&que[i+1]==-1)//两个都不是合法数据
            {
                for (int j = 1; j <= m; ++j)//第一位
                {
                    for (int k = 1; k <= m; ++k)//第二位
                    {
                        dp[i+1][k]=max(dp[i+1][k],dp[i][j]+num(j,k));
                    }
                }
            }
        }
        int ans=0;
        for (int l = 1; l <= m; ++l)
        {
            ans=max(ans,dp[n][l]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值