Title: Nyoj 1111 Game Life
Test instructions: In a straight line there are n wolves, each wolf has its own attack and auxiliary attack, auxiliary attack on the side of the two wolves have, ask what kind of a kill order to make the least cost.
Analysis: This is the problem of Beijing live game, at that time at a glance out is the interval DP, but did not consider the calculation of an interval after the two sides of the auxiliary attack, is also very good to do DP topic.
Definition: DP "i" "J" for strange damage from interval I-j, then transfer equation DP "I" "j" = Min (dp "I" "J", DP "I" "k-1" + a "K" + DP "k+1" "J") + Fu "i-1" "j+1"
has not taken into account the added part of the later, so the code is very good to write.
AC Code:
#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define N 350int Dp[n][n] ; int B[n],a[n];int Main () { int n,t; scanf ("%d", &t); for (int cas = 1;cas<=t;cas++) { scanf ("%d", &n); for (int i=1;i<=n;i++) { scanf ("%d", &a[i]); } for (int i=1; i<=n; i++) scanf ("%d", &b[i]); b[0]=b[n+1]=0; Memset (Dp,0,sizeof (DP)); int i,j,l,k;for (l = 1; l <= N; ++l) {for (i = 1; I <= n-l + 1; ++i) {j = i + l-1;dp[i][j] = 2100000000; for (int k=i; k<=j; k++) { dp[i][j]=min (dp[i][j],dp[i][k-1]+a[k]+dp[k+1][j]); } DP[I][J]+=B[I-1]+B[J+1];}} printf ("Case #%d:%d\n", CAs, dp[1][n]); } return 0;}
Nyoj 1111 Game life "interval DP"