HDU - 5115 Dire Wolf (非原创)

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.

InputThe 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.OutputFor 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: 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.

第一次遇见区间dp,学到了。
参考题解:https://www.cnblogs.com/zhengguiping--9876/p/4952759.html

题意:有一排狼,每只狼有一个伤害A,还有一个伤害B。杀死一只狼的时候,会受到这只狼的伤害A和这只狼两边的狼的伤害B的和。如果某位置的狼被杀,那么杀它左边的狼时就会收到来自右边狼的B,因为这两只狼是相邻的了。求杀掉一排狼的最小代价。

样例解释:

n   = 3

A[] = 3 5 7

B[] = 8 2 0

一共有3只狼,第一次杀掉第一只狼,代价为A[1]+B[2] = 3+2 = 5 (B和A相邻),只剩下第二只狼和第三只狼了,

所以杀掉第二只狼的代价为A[2]+B[3] = 5+0 = 5; 最后剩下一只狼了,代价为A[3] = 7,总代价为5+5+7 = 17;

 
解法:设dp[i][j]为消灭编号从i到j只狼的代价,那么结果就是dp[1][n]
    枚举k作为最后一只被杀死的狼,此时会受到a[k]和b[i-1] b[j+1]的伤害 取最小的即可
 
可列出转移方程: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];
附ac代码:
 1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 #include <algorithm>
5 #include <iomanip>
6 #include <cmath>
7 using namespace std;
8 typedef long long ll;
9 const int maxn = 222;
10 const int inf = 0x3f3f3f3f;
11 int nu[maxn];
12 int num[maxn];
13 int dp[maxn][maxn];
14 int main()
15 {
16 ios::sync_with_stdio(false);
17 int t,n;
18 // cout<<setiosflags(ios::fixed)<<setprecision(6);
19 cin>>t;
20 for(int cas=1;cas<=t;++cas)
21 {
22 memset(nu,0,sizeof(nu));
23 memset(num,0,sizeof(num));//这两步的初始化是为了让b[0]和b[n+1]为0
24 cin>>n;
25 for(int i=1;i<=n;++i)
26 cin>>num[i];
27 for(int i=1;i<=n;++i)
28 cin>>nu[i];
29 for(int i=1;i<=n;++i)
30 for(int j=i;j<=n;++j)
31 dp[i][j]=inf; //为了让i-1和j+1的值为0的同时,其他值为inf
32 for(int l=0;l<=n;l++)
33 {
34 for(int i=1;i+l<=n;++i)
35 {
36 int j=i+l;
37 for(int k=i;k<=j;k++)
38 {
39 dp[i][j]=min(dp[i][j],dp[i][k-1]+nu[i-1]+nu[j+1]+dp[k+1][j]+num[k]);
40 // cout<<dp[i][j]<<" "<<i<<" "<<j<<endl;
41 }
42 }
43 }
44 cout<<"Case #"<<cas<<": "<<dp[1][n]<<endl;
45 }
46 return 0;
47 }
上一篇:Axure RP一个专业的快速原型设计工具


下一篇:Html 之div+css布局之css选择器