hdu 5115 Dire Wolf【区间DP】
原创
©著作权归作者所有:来自51CTO博客作者mb640163eb338d0的原创作品,请联系作者获取转载授权,否则将追究法律责任
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5115
题意:
有n只狼,每只狼有两种属性,一种攻击力一种附加值,我们没杀一只狼,那么我们受到的伤害值为这只狼的攻击值与它旁边的两只狼的附加值的和,求把所有狼都杀光受到的最小的伤害值。
代码:
#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
using namespace std;
int n;
int a[500],b[500],dp[500][500];
int main()
{
int t, cases = 1;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
dp[i][j] = 100000000;
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++) dp[i][i] = a[i] + b[i - 1] + b[i + 1];
for (int l = 1; l <= n; l++)
{
for (int i = 1; i + l <= n; i++)
{
int j = i + l;
dp[i][j] = min(dp[i+1][j] + b[i-1] + b[j+1] + a[i], dp[i][j-1] + a[j] + b[j + 1] + b[i-1]);
for (int k = i+1; k <= j; k++)
{
dp[i][j] = min(dp[i][j], dp[i][k - 1] + dp[k + 1][j] + a[k] + b[i - 1] + b[j + 1]);
}
}
}
printf("Case #%d: %d\n", cases++, dp[1][n]);
}
return 0;
}