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; }
相关文章推荐
- HDU5073 ACM-ICPC亚洲区域赛鞍山赛区现场赛D题 Galaxy 贪心+数学推导
- ZOJ3822 ACM-ICPC 2014 亚洲区域赛牡丹江赛区现场赛D题Domination 概率DP(两种解法)
- HDU5078 2014 ACM-ICPC亚洲区域赛鞍山赛区现场赛I题 Osu! 签到题
- ZOJ3822 ACM-ICPC 2014 亚洲区域赛牡丹江赛区现场赛D题Domination 概率DP
- ZOJ 3822 Domination 概率DP 2014年ACM_ICPC亚洲区域赛牡丹江现场赛D题
- 2014 ACM-ICPC 亚洲区域赛 鞍山赛区 比赛小结 & 部分题解
- [置顶] 2017年第42届ACM-ICPC亚洲区域赛青岛赛区(现场赛)
- ZOJ3829 ACM-ICPC 2014 亚洲区域赛牡丹江赛区现场赛K题 Known Notation 贪心
- ZOJ3822 ACM-ICPC 2014 亚洲杯赛事现场牡丹江司D称号Domination 可能性DP
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H (简单DP)
- [置顶] 2017 ACM/ICPC 亚洲区域赛 青岛赛区
- 2013 ACM/ICPC 亚洲区域赛长春赛区 总结
- HDU5024 2014 ACM-ICPC亚洲区域赛广州赛区网络赛C题 Wang Xifeng's Little Plot
- 2017 ACM-ICPC 亚洲区(北京赛区)网络赛C.Matrix (DP)
- 2011-第36届ACM/ICPC亚洲区中国大陆5个赛区主办方网络赛和现场赛时间安排
- 2014ACM/ICPC亚洲区鞍山赛区现场赛——题目重现
- ZOJ3827 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江I称号 Information Entropy 水的问题
- HDU 5073 Galaxy (2014 ACM/ICPC 鞍山赛区现场赛D题)
- 2013 ACM-ICPC 亚洲区域赛 成都现场赛I 解题报告
- 【DP】【2012 ACM/ICPC 成都赛区现场赛】【I.Count】