本文共 1485 字,大约阅读时间需要 4 分钟。
题意:杀一只狼i会收到a[i]+b[i当前左边]+b[i当前右边]的攻击,求杀死所有狼的最小代价
#include #include #include #include #include #include #include #include #include #include #include #include #define rep(i,j,k) for(register int i=j;i<=k;i++)#define rrep(i,j,k) for(register int i=j;i>=k;i--)#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])#define iin(a) scanf("%d",&a)#define lin(a) scanf("%lld",&a)#define din(a) scanf("%lf",&a)#define s0(a) scanf("%s",a)#define s1(a) scanf("%s",a+1)#define print(a) printf("%lld",(ll)a)#define enter putchar('\n')#define blank putchar(' ')#define println(a) printf("%lld\n",(ll)a)#define IOS ios::sync_with_stdio(0)using namespace std;const int maxn = 333+11;const int oo = 0x3f3f3f3f;const double eps = 1e-7;typedef long long ll;ll read(){ ll x=0,f=1;register char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}int n,a[maxn],b[maxn];ll dp[maxn][maxn];int main(){ int T=read(),kase=0; while(T--){ n=read(); rep(i,1,n) a[i]=read(); rep(i,1,n) b[i]=read();b[n+1]=0; rep(i,0,n)rep(j,i,n+1)dp[i][j]=1ll<<33; rep(i,1,n) dp[i][i]=a[i]+b[i-1]+b[i+1]; rep(len,2,n){ rep(i,1,n-len+1){ int j=i+len-1; rep(k,i,j){ 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: ",++kase); println(dp[1][n]); } return 0;}
转载于:https://www.cnblogs.com/caturra/p/8526264.html