Nyoj 1111 Game Life "interval DP"

Source: Internet
Author: User

Title: Nyoj 1111 Game Life


Test instructions: In a straight line there are n wolves, each wolf has its own attack and auxiliary attack, auxiliary attack on the side of the two wolves have, ask what kind of a kill order to make the least cost.


Analysis: This is the problem of Beijing live game, at that time at a glance out is the interval DP, but did not consider the calculation of an interval after the two sides of the auxiliary attack, is also very good to do DP topic.

Definition: DP "i" "J" for strange damage from interval I-j, then transfer equation DP "I" "j" = Min (dp "I" "J", DP "I" "k-1" + a "K" + DP "k+1" "J") + Fu "i-1" "j+1"

has not taken into account the added part of the later, so the code is very good to write.


AC Code:

#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define N 350int Dp[n][n] ; int B[n],a[n];int Main () {    int n,t;    scanf ("%d", &t);    for (int cas = 1;cas<=t;cas++)    {        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]);        b[0]=b[n+1]=0;        Memset (Dp,0,sizeof (DP));        int i,j,l,k;for (l = 1; l <= N; ++l) {for (i = 1; I <= n-l + 1; ++i) {j = i + l-1;dp[i][j] = 2100000000;                for (int k=i; k<=j; k++)                {                    dp[i][j]=min (dp[i][j],dp[i][k-1]+a[k]+dp[k+1][j]);                }                DP[I][J]+=B[I-1]+B[J+1];}} printf ("Case #%d:%d\n", CAs, dp[1][n]);    }    return 0;}


Nyoj 1111 Game life "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.