Dire Wolf
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)Total Submission(s): 2092 Accepted Submission(s): 1206
Problem Description
Dire wolves, also known as Dark wolves, are extraordinarily large and powerful wolves. Many, if not all, Dire Wolves appear to originate from Draenor.
Dire wolves look like normal wolves, but these creatures are of nearly twice the size. These powerful beasts, 8 - 9 feet long and weighing 600 - 800 pounds, are the most well-known orc mounts. As tall as a man, these great wolves have long tusked jaws that look like they could snap an iron bar. They have burning red eyes. Dire wolves are mottled gray or black in color. Dire wolves thrive in the northern regions of Kalimdor and in Mulgore.
Dire wolves are efficient pack hunters that kill anything they catch. They prefer to attack in packs, surrounding and flanking a foe when they can.
— Wowpedia, Your wiki guide to the World of Warcra
Matt, an adventurer from the Eastern Kingdoms, meets a pack of dire wolves. There are N wolves standing in a row (numbered with 1 to N from left to right). Matt has to defeat all of them to survive.
Once Matt defeats a dire wolf, he will take some damage which is equal to the wolf’s current attack. As gregarious beasts, each dire wolf i can increase its adjacent wolves’ attack by b i. Thus, each dire wolf i’s current attack consists of two parts, its basic attack ai and the extra attack provided by the current adjacent wolves. The increase of attack is temporary. Once a wolf is defeated, its adjacent wolves will no longer get extra attack from it. However, these two wolves (if exist) will become adjacent to each other now.
For example, suppose there are 3 dire wolves standing in a row, whose basic attacks ai are (3, 5, 7), respectively. The extra attacks b i they can provide are (8, 2, 0). Thus, the current attacks of them are (5, 13, 9). If Matt defeats the second wolf first, he will get 13 points of damage and the alive wolves’ current attacks become (3, 15).
As an alert and resourceful adventurer, Matt can decide the order of the dire wolves he defeats. Therefore, he wants to know the least damage he has to take to defeat all the wolves.
Dire wolves look like normal wolves, but these creatures are of nearly twice the size. These powerful beasts, 8 - 9 feet long and weighing 600 - 800 pounds, are the most well-known orc mounts. As tall as a man, these great wolves have long tusked jaws that look like they could snap an iron bar. They have burning red eyes. Dire wolves are mottled gray or black in color. Dire wolves thrive in the northern regions of Kalimdor and in Mulgore.
Dire wolves are efficient pack hunters that kill anything they catch. They prefer to attack in packs, surrounding and flanking a foe when they can.
— Wowpedia, Your wiki guide to the World of Warcra
Matt, an adventurer from the Eastern Kingdoms, meets a pack of dire wolves. There are N wolves standing in a row (numbered with 1 to N from left to right). Matt has to defeat all of them to survive.
Once Matt defeats a dire wolf, he will take some damage which is equal to the wolf’s current attack. As gregarious beasts, each dire wolf i can increase its adjacent wolves’ attack by b i. Thus, each dire wolf i’s current attack consists of two parts, its basic attack ai and the extra attack provided by the current adjacent wolves. The increase of attack is temporary. Once a wolf is defeated, its adjacent wolves will no longer get extra attack from it. However, these two wolves (if exist) will become adjacent to each other now.
For example, suppose there are 3 dire wolves standing in a row, whose basic attacks ai are (3, 5, 7), respectively. The extra attacks b i they can provide are (8, 2, 0). Thus, the current attacks of them are (5, 13, 9). If Matt defeats the second wolf first, he will get 13 points of damage and the alive wolves’ current attacks become (3, 15).
As an alert and resourceful adventurer, Matt can decide the order of the dire wolves he defeats. Therefore, he wants to know the least damage he has to take to defeat all the wolves.
Input
The first line contains only one integer T , which indicates the number of test cases. For each test case, the first line contains only one integer N (2 ≤ N ≤ 200).
The second line contains N integers a i (0 ≤ a i ≤ 100000), denoting the basic attack of each dire wolf.
The third line contains N integers b i (0 ≤ b i ≤ 50000), denoting the extra attack each dire wolf can provide.
The second line contains N integers a i (0 ≤ a i ≤ 100000), denoting the basic attack of each dire wolf.
The third line contains N integers b i (0 ≤ b i ≤ 50000), denoting the extra attack each dire wolf can provide.
Output
For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1), y is the least damage Matt needs to take.
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: 74HintIn 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.
Source
题意:主人公要杀狼,狼是站成一排的,每只狼有它的基础攻击力和增益攻击力,主人公没每打倒一直狼除了受到这只狼的基础攻击力之外,还会受到这只狼左右两边的狼给它的增益攻击伤害。比如:
基础:(3, 5, 7)
增益:(8, 2, 0)
那么当下打倒这一只狼要受的伤害分别是:(5, 13, 9)
现在求主人公打倒所有狼受到的最小伤害
第一次写区间dp。。。区间dp的套路似乎是最外层枚举区间的长度,然后第二层枚举起点。由于打倒一只狼后两边的狼会凑过来,不存在中断的情况。
枚举区间长度——》区间起点位置——》改区间最后一只杀死的狼
dp【i】【j】代表从第 i 个位置到第 j 个位置所受的最小攻击值和。
dp[ i ][ j ]= min(dp[ i ][ j ],dp[ i ][ k-1 ]+dp[ k+1 ][ j ]+basic[ k ]+extra[ i-1 ]+extra[ j+1 ])
在区间i~j内最后杀死的狼,由于i~k-1和k+1~j内的狼都死光了,所以第k只狼受到的加成是i-1和第j+1只狼给它的。
还有关于dp数组初始化的问题,一开始每个区间都要初始成无穷大。
但是为什么是i=1;i《=n;i++——》j = i;j《=n;j++呢?
一开始我用了memset,答案是错的。这是因为后面dp的时候,当区间长度为1时,k与j是相等的,这时候用到的dp【i】【k-1】、dp【k+1】【j】中的k==i==j,dp【i】【k-1】==dp【k+1】【j】==0
所以一开始将dp清零再将一部分区间置INF
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 300;
int basic[maxn],extra[maxn];
int dp[maxn][maxn];
int main(){
int t,n,i,j,k,len;
int rnd = 1;
scanf("%d",&t);
while(t--){
printf("Case #%d: ",rnd);
rnd++;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&basic[i]);
}
for(i=1;i<=n;i++){
scanf("%d",&extra[i]);
}
basic[0] = 0,basic[n+1] = 0;
extra[0] = 0,extra[n+1] = 0;
memset(dp, 0, sizeof(dp));
for(i=1;i<=n;i++){
for(j=i;j<=n;j++){
dp[i][j]=INF;
}
}
for(len=1;len<=n;len++){
for(i=1;i<=n-len+1;i++){
int j = i+len-1;
for(k=i;k<=j;k++){
dp[i][j] = min(dp[i][j],dp[i][k-1]+dp[k+1][j]+basic[k]+extra[i-1]+extra[j+1]);
}
}
}
printf("%d\n",dp[1][n]);
}
return 0;
}