HDU 5115 interval DP

Source: Internet
Author: User
Tags cas



Link: poke here


Dire Wolf
Time limit:5000/5000 MS (java/others) Memory limit:512000/512000 K (java/others)

Problem Description
Dire Wolves, also known as Dark Wolves, is 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 is of nearly twice the size. These powerful beasts, 8-9 feet long and weighing 600-800 pounds, is the most well-known ORC mounts. As tall as a man, these great wolves has a long tusked jaws that look like they could snap an iron bar. They has burning red eyes. Dire wolves is mottled gray or black in color. Dire wolves thrive in the northern regions of Kalimdor and in Mulgore.
Dire Wolves is 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 for the world of Warcra

Matt, an adventurer from the Eastern kingdoms, meets a pack of dire wolves. There is N wolves standing in a row (numbered with 1 to N from left to right). Matt has the defeat all of them to survive.

Once Matt defeats a dire wolf, he'll take some damage which are equal to the wolf's current attack. As gregarious beasts, each dire wolf I can increase their adjacent wolves ' attack by BI. Thus, each dire wolf I's current attack consists of the II parts, its basic attack AI and the extra attack provided by the CU Rrent adjacent wolves. The increase of attack is temporary. Once A wolf is defeated, its adjacent wolves would no longer get extra attack from it. However, these, wolves (if exist) would become adjacent to each and now.

For example, suppose there is 3 dire wolves standing in a row, whose basic attacks Ai is (3, 5, 7), respectively. The extra attacks bi they can provide is (8, 2, 0). Thus, the current attacks of them is (5, 13, 9). If Matt defeats the second Wolf first, he'll get 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 had 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 ai (0≤ai≤100000) and denoting the basic attack of each dire wolf.

The third line contains N integers bi (0≤bi≤50000), denoting the extra attack each dire wolf can provide.

Output
For each test case, output a single line "Case #x: Y", where x was the case number (starting from 1), and 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-rst sample, Matt defeats the dire wolves from left to right. He takes 5 + 5 + 7 = Points of damage which is the least damage he have to take.


Test instructions

N Wolf, each wolf has two properties, a for the wolf's own attack, B for additional damage

The hunter killed a wolf attacked as: The Wolf itself attack + the neighboring Wolf's additional attack power

The hunters are required to kill all wolves for the least amount of attacks.


Ideas:

Analyzed to see if it can be greedy to do, not very good selection of the first to kill the wolf

Interval DP Bar DP[I][J] means that all wolves within the kill interval [i,j] are under minimal attack.

K on enumeration interval, K for interval interval dp[i][j]=min (dp[i][j],dp[i][k-1]+a[k]+dp[k+1][j]+b[i-1]+b[j+1])

Merging the two intervals into a single interval must be a k to find the smallest attack and satisfy the condition.


Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < string> #include <vector> #include <ctime> #include <queue> #include <set> #include <map > #include <stack> #include <iomanip> #include <cmath> #define MST (SS,B) memset ((ss), (b), sizeof (SS )) #define MAXN 0x3f3f3f3f#define MAX 1000100///#pragma comment (linker, "/stack:102400000,102400000") typedef long LONG Ll;typedef unsigned long long ull; #define INF (1ll<<60) -1using namespace Std;int t,n;ll dp[1010][1010];    DP[I][J] represents the minimum damage to the wolf within the kill interval [I,J] ll A[1010],b[1010];int main () {scanf ("%d", &t);    int Cas=1;        while (t--) {scanf ("%d", &n);        for (int i=1;i<=n;i++) scanf ("%i64d", &a[i]);        for (int i=1;i<=n;i++) scanf ("%i64d", &b[i]);            for (int i=1;i<=n;i++) {for (int j=i;j<=n;j++) {dp[i][j]=inf; }} for (int len=1;len<=n;len++) {///interval length for (inT i=1;i<=n-len+1;i++) {int j=i+len-1;                for (int k=i;k<=j;k++) {dp[i][j]=min (dp[i][j],dp[i][k-1]+a[k]+dp[k+1][j]+b[i-1]+b[j+1]);    }}} printf ("Case #%d:%i64d\n", Cas++,dp[1][n]); } return 0;}


HDU 5115 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.