Dire Wolf---hdu5115 (interval dp)

Source: Internet
Author: User

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

Test Instructions: There is a row of wolves, each wolf has a damage a, and a damage B. When you kill a wolf, you will be harmed by the wolf's damage A and the wolf on both sides of the wolf. If a wolf in a position is killed, the wolf on the left will receive B from the right wolf, because the two wolves are adjacent. The minimum cost of killing a row of wolves.

Solution: set DP[I][J] to eliminate the cost of the I to J Wolf, enumeration K as the last killed Wolf, at this time will be affected by a[k] and B[i-1] b[j+1] To minimize the damage the transfer equation can be listed: Dp[i][j]=min (Dp[i][j], dp[i][k-1]+dp[k+1][j]+a[k]+b[i-1]+b[j+1]) dp[i][i]=a[i]+b[i-1]+b[j+1];

#include <stdio.h>#include<iostream>#include<string.h>#include<algorithm>#include<math.h>using namespacestd;#defineN 220#defineINF 0XFFFFFFFintMain () {intT, N, A[n], b[n], dp[n][n], t=1; scanf ("%d", &T);  while(t--) {memset (A,0,sizeof(a)); memset (b,0,sizeof(b)); scanf ("%d", &N);  for(intI=1; i<=n; i++) scanf ("%d", &A[i]);  for(intI=1; i<=n; i++) scanf ("%d", &B[i]);  for(intI=1; i<=n; i++)        {             for(intJ=i; j<=n; J + +) Dp[i][j]=INF; ///dp[i][i]=a[i]+b[i-1]+b[i+1];        }         for(intL=0; l<=n; l++)        {             for(intI=1; i+l<=n; i++)            {                intj=i+l;  for(intK=i; k<=j; k++) {Dp[i][j]=min (Dp[i][j], dp[i][k-1]+dp[k+1][j]+a[k]+b[i-1]+b[j+1]); }}} printf ("Case #%d:%d\n", t++, dp[1][n]); }    return 0;}
View Code

Dire Wolf---hdu5115 (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.