HDU5115 Dire Wolf (interval dp)

Source: Internet
Author: User

Topic Links:

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


Test instructions

There are n wolves, each wolf has two kinds of properties, a kind of attack value, we did not kill a wolf, then we receive a damage value of

The wolf's attack value is the same as that of the two wolves next to it, and the minimum damage value for all wolves to be killed.


Analysis:

Naked Interval DP

DP[I][J] represents the smallest damage to any wolf in the range i,j.

The state transition equation is

Dp[i][j]=min{dp[i][k]+dp[k+1][j]-b[k]+b[i+1],dp[i][k]+dp[k+1][j]-b[k+1]+b[j+1]} (I<=K<J) We're talking about killing the left first, or the right.

Note that the initial value of a[0],b[0],a[n+1],b[n+1] is 0 when dealing with it.


The code is as follows:

#include <iostream> #include <cstring> #include <cstdio>using namespace std;const int maxn = 210;int dp[    Maxn][maxn];int A[maxn],b[maxn];int Main () {int n,t,cas=1;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        for (int i=1;i<=n;i++) scanf ("%d", &a[i]);        for (int i=1;i<=n;i++) scanf ("%d", &b[i]);        a[0]=a[n+1]=0;        b[0]=b[n+1]=0;        Memset (Dp,0,sizeof (DP));        for (int i=1;i<=n;i++) dp[i][i]=a[i]+b[i-1]+b[i+1];        for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) dp[i][j]=999999999;                for (int len = 2;len<=n;len++) {for (int i=1;i+len-1<=n;i++) {int j=i+len-1;                    for (int k=i;k<j;k++) {dp[i][j]=min (dp[i][j],dp[i][k]+dp[k+1][j]+b[i-1]-b[k]);                Dp[i][j]=min (dp[i][j],dp[i][k]+dp[k+1][j]+b[j+1]-b[k+1]); }}} printf ("Case #%d:%d\n ", Cas++,dp[1][n]); } return 0;}


HDU5115 Dire Wolf (interval dp)

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.