Dire Wolf HDU-5115 zone DP, winter vacation camp

There are n wolves in a row, each wolf has two attributes, attack power and bonus value. The actual attack power of a wolf is equal to its own attack power plus the bonus value of neighboring wolves. After being killed, the wolves are adjacent to each other. The bonus to the wolf’s attack power will be cancelled, and at the same time, the two wolves that were adjacent to the killed wolf will become adjacent wolves. Ask what is the minimum damage value for killing all wolves.
Input
multiple groups of inputs, one T on the first line, one for
each group, n (n <= 200), and
n numbers, which represent the attack power of each wolf (0<=a[i] <=1e5)
. Represents the bonus value of each wolf (0<=b[i]<=5e4)
Output
Each set of samples outputs a Case, and then outputs the answer.
Sample Input
2
3
3 5 7
8 2 0
10
1 3 5 7 9 2 4 6 8 10
9 4 1 2 1 2 1 4 5 1
Sample Output
Case #1: 17
Case #2: 74

Hint
In the first sample, Matt defeats the dire wolves from left to right. He takes 5 + 5 + 7 = 17 points of damage which is the least damage he has to take.、

Interval DP template questions, first remember to initialize the second half of dp[i][j], that is, the part j>i to the maximum value, because the second half is used to update the dynamic transition equation of the previous stage . Add the current attack value and adjacent bonuses on the left and right sides of the b array
dp[j][k]=min(dp[j][k],dp[j][x-1]+a[x]+dp[x+1][k]+b[j-1]+b[k+1]);

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#define ls (p<<1)
#define rs (p<<1|1)
#define ll long long
using namespace std;
const int maxn = 500;
const int INF = 0x3f3f3f3f;
ll  dp[maxn][maxn];
ll a[maxn],b[maxn];
void solve(){
    
    
    int t;
    cin>>t;
    int cas=0;
    while(t--){
    
    
       
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        for(int i=1;i<=n;i++)
            cin>>b[i];
        b[0]=0;
        b[n+1]=0;
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
            for(int j=i;j<=n;j++)
                dp[i][j]=INF;
        for(int i=1;i<=n;i++){
    
    
            for(int j=1;j<=n-i+1;j++){
    
    
                int k=i+j-1;
                for(int x=j;x<=k;x++){
    
    
                    dp[j][k]=min(dp[j][k],dp[j][x-1]+a[x]+dp[x+1][k]+b[j-1]+b[k+1]);
                }
            }
        }
        printf("Case #%d: %d\n",++cas,dp[1][n]);
    }
}   
int main()
{
    
    
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    solve();
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45891413/article/details/113000482