HDU 5074 Hatsune Miku

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=5074

Test instructions: give you a matrix score[i][j], and then give you a sequence of numbers, some of which are-1, which means that the number can be replaced by any number of 1~m, and then the maximum value to be asked.

Idea: The two-dimensional dp,dp[i][j] represents the number of I positions for the Max and of J. The maximum sum of this position is pushed by the preceding summation, divided into four cases, (a,-1), (A, B), ( -1,-1), ( -1,b); Dp[i][j]=max (Dp[i][j],dp[i-1][k]+g[k][j]);

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6 intt,n,m;7 intg[ -][ -];8 inta[ the];9 intdp[ +][ +];Ten  One intMain () A { -scanf"%d",&t); -      while(t--) the     { -scanf"%d%d",&n,&m); -          for(intI=1; i<=m; i++) -         { +              for(intj=1; j<=m; J + +) -             { +scanf"%d",&g[i][j]); A             } at         } -          for(intI=1; i<=n; i++) -         { -scanf"%d",&a[i]); -         } -Memset (DP,0,sizeof(DP)); in          for(intI=2; i<=n; i++) -         { to             if(a[i]>0) +             { -                 if(a[i-1]>0) the                 { *dp[i][a[i]]=dp[i-1][a[i-1]]+g[a[i-1]][a[i]]; $                 }Panax Notoginseng                 Else -                 { the                      for(intj=1; j<=m; J + +) +                     { ADp[i][a[i]]=max (dp[i][a[i]],dp[i-1][j]+G[j][a[i]]); the                     } +                 } -             } $             Else $             { -                  for(intj=1; j<=m; J + +) -                 { the                     if(a[i-1]>0) -                     {WuyiDp[i][j]=max (dp[i][j],dp[i-1][a[i-1]]+g[a[i-1]][j]); the                     } -                     Else Wu                     { -                          for(intk=1; k<=m; k++) About                         { $Dp[i][j]=max (dp[i][j],dp[i-1][k]+g[k][j]); -                         } -                     } -                 } A             } +         } the         intans=0; -          for(intI=1; i<=m; i++) ans=Max (ans,dp[n][i]); $printf"%d\n", ans); the     } the     return 0; the}
View Code

HDU 5074 Hatsune Miku

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.