您的位置:首页 > 其它

HDU5074 ACM-ICPC亚洲区域赛鞍山赛区现场赛E题 Hatsune Miku 二维DP

2014-10-27 18:18 465 查看



Hatsune Miku

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 318 Accepted Submission(s): 238

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


这个题是鞍山赛区的第二个可做题,其实就是一个很裸的二维DP,意思是每种数字的转换分值给你,然后-1位置代表可以填数,问你填完以后全部转换最大可以获得的分值是多少??这个题其实就是一个DP过程,先把所有初始化为-1,表示无法到达的位置,然后对于每次转换,如果此处出现为-1,那么就可以枚举起点终点相互间进行转移,即dp[i][j]=max(dp[i][j],dp[i-1][k]+score[j][k]),如果此处不为-1,那么就是枚举起点向固定终点转移,即dp[i][line[i]]=max(dp[i][line[i]],dp[i-1][j]+score[j][line[i]]),得到递推过程后直接噼里啪啦拍一拍就过了,但是现场赛时候,我真的醉了,我的思路完全被带入题目当中,用了一个很复杂的思路,就是枚举每一段相邻的已知数字中间段,然后对此区间内所有数字进行上面所说的第一种dp过程,然后处理一下首尾问题,思路复杂造成代码很繁,最后代码的确这样写比较难debug,最后的确90+分钟以后才通过此题,通过后才想到的确前面思路可搞,唉~~说来有点小悲伤啊~~最后简单思路AC代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
const int MAXN=105;
int dp[MAXN][MAXN],score[MAXN][MAXN],line[MAXN];
int maxx(int a,int b)
{
if(a>b)return a;
return b;
}
int main()
{
//   freopen("in.txt","r",stdin);
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=m;j++)
cin>>score[i][j];
}
for(int i=0;i<n;i++)
cin>>line[i];
memset(dp,-1,sizeof(dp));
if(line[0]==-1)
{
for(int i=1;i<=m;i++)
dp[0][i]=0;
}
else
dp[0][line[0]]=0;
for(int i=1;i<n;i++)
{
if(line[i]==-1)
{
for(int j=1;j<=m;j++)
{
if(dp[i-1][j]==-1)
continue;
for(int k=1;k<=m;k++)
dp[i][k]=maxx(dp[i][k],dp[i-1][j]+score[j][k]);
}
}
else
{
for(int j=1;j<=m;j++)
{
if(dp[i-1][j]!=-1)
dp[i][line[i]]=maxx(dp[i][line[i]],dp[i-1][j]+score[j][line[i]]);
}
}
}
int res=0;
if(line[n-1]==-1)
{
for(int i=1;i<=m;i++)
res=maxx(res,dp[n-1][i]);
}
else
res=dp[n-1][line[n-1]];
cout<<res<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐