hdu Hatsune Miku(DP)
来源:互联网 发布:阿里云服务器访问外网 编辑:程序博客网 时间:2024/05/16 18:01
Hatsune Miku
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 162 Accepted Submission(s): 129
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?
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.
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,还是特别不熟悉,这里说一个需要注意的地方:
在进行dp[i][j]时,如果dp[i-1][j]为-1,此时应该注意,计算dp[i][j]不应将其计算在内。(这里可能存在dp[i-1][j] + max(score[j][k]) 最大的情况,故需要一个if来约束一下!)
状态dp[i][k]表示第i个位置放编号为k的物品
Code:
1 //============================================================================ 2 // Name : 1.cpp 3 // Author : 4 // Version : 5 // Copyright : Your copyright notice 6 // Description : Hello World in C++, Ansi-style 7 //============================================================================ 8 9 #include <iostream>10 #include <algorithm>11 #include <stdio.h>12 #include <string.h>13 #include <math.h>14 #include <stdlib.h>15 #include <queue>16 using namespace std;17 18 int score[110][110],f[110];19 int dp[110][110];20 int t,n,k;21 22 int main() {23 //freopen("in.txt","r",stdin);24 25 scanf("%d",&t);26 while(t--){27 scanf("%d %d",&n,&k);28 for(int i=1;i<=k;i++) for(int j=1;j<=k;j++) scanf("%d",&score[i][j]);29 for(int i=1;i<=n;i++) scanf("%d",&f[i]);30 31 memset(dp,-1,sizeof(dp));32 if(f[1]==-1)33 for(int i=1;i<=k;i++) dp[1][i]=0;34 else dp[1][f[1]]=0;35 for(int i=2;i<=n;i++){36 if(f[i]==-1){37 for(int z=1;z<=k;z++){38 int maxc = -1;39 for(int j=1;j<=k;j++)40 if(dp[i-1][j] + score[j][z] > maxc && dp[i-1][j]!=-1) maxc = dp[i-1][j] + score[j][z];41 dp[i][z] = maxc;42 }43 }44 else{45 //为特定数字的时候,比较特殊!46 int maxc = -1;47 int z = f[i];48 if(f[i-1]!=-1) {49 maxc = 0;50 for(int l=1;l<=k;l++){51 if(dp[i-1][l]==-1) maxc = maxc + 1;52 maxc += dp[i-1][l];53 }54 maxc += score[f[i-1]][z];55 }56 else57 for(int j=1;j<=k;j++)58 if(dp[i-1][j] +score[j][z] > maxc && dp[i-1][j]!=-1) maxc = dp[i-1][j] +score[j][z];59 dp[i][z] = maxc;60 for(int zz=1;zz<=k;zz++)61 if(zz!=z) dp[i][zz] = -1;62 }63 }64 // for(int i=1;i<=n;i++){65 // for(int j=1;j<=k;j++)66 // printf("%d ",dp[i][j]);67 // printf("\n");68 // }69 int maxc = -1;70 for(int i=1;i<=k;i++)71 if(dp[n][i] >maxc) maxc = dp[n][i];72 printf("%d\n",maxc);73 }74 75 //cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!76 return 0;77 }
0 0
- hdu Hatsune Miku(DP)
- HDU 5074 Hatsune Miku(DP)
- hdu 5074 Hatsune Miku(dp)
- HDU 5074 Hatsune Miku DP
- hdu 5074 Hatsune Miku【DP】
- HDU 5074-Hatsune Miku(DP)
- HDU 5074 Hatsune Miku(dp)
- hdu 5074 Hatsune Miku(dp)
- HDU 5074 Hatsune Miku(简单二维dp)
- HDU 5074 Hatsune Miku (线性dp)
- HDU 5074 Hatsune Miku 暴力dp
- HDU 5074 Hatsune Miku (dp)
- HDU 5074 Hatsune Miku(暴力+DP)
- HDU 5074 Hatsune Miku(DP)
- hdu 5074 Hatsune Miku 简单dp
- HDU 5074 Hatsune Miku(DP)
- HDU 5074 Hatsune Miku 【dp思维】
- hdu5074 Hatsune Miku dp
- 线段树---分析 && 模板总结
- 最短路(转)模板
- vs2012 error c4996: 'fopen': This function or variable may be unsafe
- 高斯消元法---模板(转自kuangbin)
- 单源最短路模板(转自 海子)
- hdu Hatsune Miku(DP)
- hdu Galaxy(数学公式)
- Java IO流学习
- 81Widget小控件
- POJ题目推荐(转载)
- C++ 优先队列(转)
- POJ 1011 递归 & 回溯
- 2014-11-20动态规划:顺推法与逆推法中递推公式的不同!
- POJ:1050(枚举 + DP)