1. 程式人生 > >HDU-5115 Dire Wolf (區間dp)

HDU-5115 Dire Wolf (區間dp)

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.
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.
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: 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題了,不會啊,網上查是簡單的區間dp題,仔細想了想,確實是那樣的。。。。
dp[i][j]代表i到j這段區間的最小代價,那麼dp轉移方程就是
dp[i][j] = min{dp[i][j],dp[i][k - 1] + dp[k + 1][j] + a[k] + b[i - 1] + b[j + 1]};
意思是把k作為i到j這段區間的最後一個狼來殺,這段區間附加的攻擊就是b[i - 1] + b[j + 1];
是真的妙;
接下來就是編碼問題了,記住dp陣列要初始化為0;然後在對i = 1;i <= n ;j = i ;j <= n;這些地方dp值賦為inf;因為邊界值dp[i][i] = dp[i][i - 1] + dp[i + 1] [i] + a[i] + b[i - 1] + b[i + 1];
所以a,b陣列也要初始化為0,再輸入值;dp[i][i - 1]必是0;

區間DP的定義:
區間動態規劃問題一般都是考慮,對於每段區間,他們的最優值都是由幾段更小區間的最優值得到,是分治思想的一種應用,將一個區間問題不斷劃分為更小的區間直至一個元素組成的區間,列舉他們的組合 ,求合併後的最優值。

設F[i,j](1<=i<=j<=n)表示區間[i,j]內的數字相加的最小代價
最小區間F[i,i]=0(一個數字無法合併,∴代價為0)

每次用變數k(i<=k<=j-1)將區間分為[i,k]和[k+1,j]兩段

For p:=1 to n do // p是區間長度,作為階段。
for i:=1 to n do // i是窮舉的區間的起點
begin
j:=i+p-1; // j是 區間的終點,這樣所有的區間就窮舉完畢
if j>n then break; // 這個if很關鍵。
for k:= i to j-1 do // 狀態轉移,去推出 f[i,j]
f[i , j]= max{f[ i,k]+ f[k+1,j]+ w[i,j] }
end;
這個結構必須記好,這是區間動態規劃的程式碼結構。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int N = 205;

int a[N];
int b[N];
int dp[N][N];

int main()
{
    int t;
    scanf("%d",&t);
    int cnt = 0;
    while(t--)
    {
        cnt++;
        int n;
        scanf("%d",&n);
        memset(dp,0,sizeof(dp));
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i = 1;i <= n;++i){
            scanf("%d",&a[i]);
        }
        for(int i = 1;i <= n;++i){
            scanf("%d",&b[i]);
        }
        for(int i = 1;i <= n;++i){
            for(int j = i;j <= n;++j){
                dp[i][j] = inf;
            }
        }
        for(int len = 0;len < n;++len){
            for(int i = 1;i <= n - len;++i){
                int j = i + len;
                for(int k = i;k <= j;++k){
                    if(dp[i][j] > dp[i][k - 1] + dp[k + 1][j] + a[k] + b[i - 1] + b[j + 1]){
                        dp[i][j] = dp[i][k - 1] + dp[k + 1][j] + a[k] + b[i - 1] + b[j + 1];
                    }
                }
            }
        }
//        for(int i = 1;i <= n;++i){
//            for(int j = 1;j <= n;++j){
//                printf("%d ",dp[i][j]);
//            }
//            printf("\n");
//        }
        printf("Case #%d: ",cnt);
        printf("%d\n",dp[1][n]);
    }
    return 0;
}