HDU 5115 Dire Wolf

Source: Internet
Author: User

The last Beijing match on the scene, yesterday did a section DP, suddenly think of this problem, are very similar, turned out to do

Just started like yesterday, the same way, thinking about how to reverse, and later found that the problem should be pushed

In fact, the forward and reverse push at first glance looks very similar, only one is dp[i][j] said I, J and other wolves to eliminate the cost of i-j this period, one is to say that the last remaining i-j when the cost of eliminating this paragraph

To summarize the similarities between the DP and today's interval DP, find that the interval DP is applicable to the cost over time, or that DP is changing

#include <stdio.h> #include <algorithm> #include <string.h>using namespace Std;const int inf= 999999999;int a[205],b[205];int dp[205][205],n;void Init () {for (int. i=0;i<205;i++) {for (int j=0;j<205;j++) {dp[i ][j]=inf;}} b[n+1]=0;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++) dp[i][i+1]=min (dp[i][i]+a[i+ 1]+B[I+2]+B[I-1],DP[I+1][I+1]+A[I]+B[I-1]+B[I+2]);} int main () {#ifndef online_judgefreopen ("In.txt", "R", stdin), #endifint t;scanf ("%d", &t); int Cas=1;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]);} Init (); for (int. l=3;l<=n;l++) {for (int i=1;i+l-1<=n;i++) {int j=i+l-1;dp[i][j]=min (dp[i][j],dp[i+1][j]+a[i]+b[ I-1]+B[J+1]);DP [I][j]=min (Dp[i][j],dp[i][j-1]+a[j]+b[j+1]+b[i-1]); for (int k=i+1;k<=j-1;k++) {dp[i][j]=min (dp[ I][j],dp[i][k-1]+dp[k+1][j]+b[i-1]+b[j+1]+a[k]);} printf ("dp[%d][%d]:%d\n", I,j,dp[i][j]);}} printf ("Case #%d:%d\n", Cas++,dp[1][n]);}}


HDU 5115 Dire Wolf

Related Article

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.