hdoj 5074 Hatsune Miku 【DP】
2015-08-05 08:22
197 查看
Hatsune Miku
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 886 Accepted Submission(s): 622
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.
![](http://acm.hdu.edu.cn/data/images/C549-1005-1.jpg)
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
题意:题意:给你一个
的矩阵score[i][j],然后给你一个数列
,数列中有一些是-1,代表这个数可以换成1~m的任意一个数,然后求
的最大值。
思路:用dp[ i ][ j ]表示序列b[]第i个数取j时的总和,当dp[ i ][ j ] == -1时表示序列b[]第i个数取j时不合法。当dp[
i ][ j ]是合法的(即满足题目要求),我们可以得到状态转移方程dp[ i + 1][ k ] = max(dp[i+1] [ k ],dp[ i ][ j ] + score [ j ][ k ])。
注意:由上述方程,我们可以由dp[ i ] 求出了dp[ i + 1]的所有情况,当然也有不合法的情况,因此在使用dp[i + 1]的状态求dp[i+2]之前需要判断当前状态是否合法。判断方法:当b[i+1]
== -1 时没有限制; 当b[i+1] == a (a >= 1)时要求b[[i + 1]必须取a,因此dp[i+1][j]中j != a的情况都是非法的。
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int score[60][60]; int b[110]; int dp[110][60];//dp[i][j]表示序列第i个数取j时的总和 int main() { int t; int N, M; scanf("%d", &t); while(t--) { scanf("%d%d", &N, &M); for(int i = 1; i <= M; i++) { for(int j = 1; j <= M; j++) scanf("%d", &score[i][j]); } for(int i = 1; i <= N; i++) scanf("%d", &b[i]); memset(dp, -1, sizeof(dp));//初始化全部位置不可取 for(int i = 1; i <= M; i++) dp[1][i] = 0;//假想序列第一个数取1-M任意值都是合法的 当然它的和为0 for(int i = 1; i <= N-1; i++) { for(int j = 1; j <= M; j++) { if(b[i] > 0 && j != b[i])//判断合法性 dp[i][j] = -1;//不可取 if(dp[i][j] == -1) continue;//不可取 //序列第i个数取j是合法的 由当前状态推序列i+1个数取值的情况 for(int k = 1; k <= M; k++) dp[i+1][k] = max(dp[i+1][k], dp[i][j] + score[j][k]); //这里不用管序列第i+1个数能不能取k,我们假想可以取 //因为在下一个层循环首先判断了序列第i+1个数取值的合法性 } } int ans = 0; for(int i = 1; i <= M; i++)//更新结果 ans = max(ans, dp [i]); printf("%d\n", ans); } return 0; }
相关文章推荐
- cisco syslog配置
- swift protocol
- HTML网页跳转
- SCNetworkReachability 使用
- 利用tcpdump抓包工具监控TCP连接的三次握手和断开连接的四次挥手
- svchost.exe占用cpu过高怎么处理(原因及解决办法)
- ORACLE AUDIT
- OA开发很简单,OA实施很复杂!
- Android 平台下使用 i2c-tools
- 嵌入式系统(Linux&Android)学习路线
- [Hdu 1520] (DP_树形DP) 入门训练
- 想起了老朋友
- 别在领域模型迷失了自己
- 11214 - Guarding the Chessboard(暴力搜索)
- HDOJ2844Coins【多重背包+二进制优化】
- 我是如何向老婆解释MapReduce的?
- 20150802 Day06课程——数组
- 如何在 Ubuntu 中管理和使用逻辑卷管理 LVM
- 如何在 Ubuntu 中管理和使用逻辑卷管理 LVM
- Scala学习笔记-12