HDU 5074-Hatsune Miku(DP)

Hatsune Miku

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

Total Submission(s): 29    Accepted Submission(s): 25

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[i][k]表示第i个位置放编号为k的物品,则dp[i][k]=max(dp[i][k],dp[i-1][j]+dis[j][k])

枚举第i-1为位置上放置的物品。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#define ll long long
using namespace std;
const int INF=0x3f3f3f3f;
int n,m,dis[55][55],a[110],dp[110][55];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		memset(dp,-1,sizeof(dp));
		scanf("%d%d",&n,&m);
		for(int i=1;i<=m;i++)
			for(int j=1;j<=m;j++)
			scanf("%d",&dis[i][j]);
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		if(a[1]!=-1)
			dp[1][a[1]]=0;
		else
			for(int i=1;i<=m;i++)
			dp[1][i]=0;
		for(int i=2;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(dp[i-1][j]!=-1)
				{

					if(a[i]!=-1)
						dp[i][a[i]]=max(dp[i][a[i]],dp[i-1][j]+dis[j][a[i]]);
					else
						for(int k=1;k<=m;k++)
						dp[i][k]=max(dp[i][k],dp[i-1][j]+dis[j][k]);
				}
			}
		}
		int ans=-INF;
		for(int i=1;i<=m;i++)
			ans=max(ans,dp[n][i]);
		printf("%d\n",ans);

	}
    return 0;
}
时间: 2024-10-20 19:11:41

HDU 5074-Hatsune Miku(DP)的相关文章

HDU 5074 Hatsune Miku(DP)

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

[ACM] HDU 5074 Hatsune Miku (简单DP)

Hatsune Miku 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 compos

hdu 5074 Hatsune Miku(2014 鞍山现场赛)

Hatsune Miku Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 17    Accepted Submission(s): 14 Problem Description Hatsune Miku is a popular virtual singer. It is very popular in both Japan an

HDU 5074 Hatsune Miku (线性dp)

Hatsune Miku Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 654    Accepted Submission(s): 471 Problem Description Hatsune Miku is a popular virtual singer. It is very popular in both Japan

HDU 5074 Hatsune Miku(14鞍山区域赛 E)DP

题意:给定一个序列 有些位数未知,给你如果两个数连续所得到的能量,问你怎么安排数字使得总能量最大 解题思路:dp,只与上一个字母有关. 解题代码: 1 // File Name: e.cpp 2 // Author: darkdream 3 // Created Time: 2014年10月22日 星期三 12时16分10秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set&g

hdu - 5074 Hatsune Miku (简单dp)

有m种不同的句子要组成一首n个句子的歌,每首歌都有一个美丽值,美丽值是由相邻的句子种类决定的,给出m*m的矩阵map[i][j]表示第i种句子和第j种句子的最大得分,一首歌的美丽值是由sum(map[i][i+1],map[i+1][i+2]....) 初始给出n个句子的值,为正就不能改变,为负表示可以替换,输出最大的美丽值. dp[i][j]表示前i个句子且第i个句子种类为j的最大得分.下面分情况讨论. if(p[i]>0) if(p[i-1]>0)  dp[i][p[i]]=dp[i-1]

HDU 4745 Two Rabbits(DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4745 题意:n个数排成一个环.两个人AB初始时各自选定一个位置.每一轮A在顺时针方向选择一个位置,B在逆时针选择一个位置,且这两个人所选位置的数字相等,然后格子跳到新选的位置上.问最多进行多少轮?有一个限制为每次跳跃不能跨过以前自己曾经选过的格子. 思路:主要是分析问题的本质.其实就是求最长回文子列.f[i][j]为[i,j]的最长回文子列,则答案为max(f[1][i],f[i+1][n]). i

HDU 4833 Best Financing (DP)

Best Financing Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 29    Accepted Submission(s): 3 Problem Description 小A想通过合理投资银行理财产品达到收益最大化.已知小A在未来一段时间中的收入情况,描述为两个长度为n的整数数组dates和earnings,表示在第dat

HDU 1260:Tickets(DP)

Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 923    Accepted Submission(s): 467 Problem Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However,

hdu 1421 搬寝室 (dp)

思路分析: dp[i][j] 表示选取到第 i 个   组成了 j 对的最优答案. 当然排序之后 选取相邻两个是更优的. if(i==j*2) dp[i][j] = dp[i-2][j-1] + w[i]-w[i-2]^2.. else if( i> j*2 ) dp[i][j] = min (dp[i-2][j-1] + ...^2   ,    dp[i-1][j]).... #include <cstdio> #include <iostream> #include &