1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <string> 6 #include <map> 7 #include <cmath> 8 #include <vector> 9 10 #define Faster ios::sync_with_stdio(false),cin.tie(0) 11 #define Read freopen("in.txt","r",stdin),freopen("out.txt","w",stdout) 12 #define Close fclose(stdin),fclose(stdout) 13 const int maxn = 105; 14 using namespace std; 15 const int MOD = 1e9+7; 16 #define INF 0xfffffff 17 typedef long long ll; 18 19 int a[maxn]; 20 int sum[maxn]; 21 int dp[maxn][maxn]; //i-j的最小花费 22 23 int main(){ 24 Faster; 25 int t; 26 int cnt = 0; 27 scanf("%d", &t); 28 while(t--){ 29 cnt++; 30 int n; 31 scanf("%d", &n); 32 memset(sum, 0, sizeof(sum)); 33 for(int i = 1;i <= n;i++){ 34 scanf("%d", &a[i]); 35 sum[i] = sum[i-1] + a[i]; 36 } 37 memset(dp, 0, sizeof(dp)); 38 for(int i = 1;i <= n;i++){ 39 for(int j = i+1;j <= n;j++){ 40 dp[i][j] = INF; 41 } 42 } 43 for(int l = 1;l < n;l++){ 44 for(int i = 1;i <= n-l;i++){ 45 int j = i+l; 46 for(int k = 1;k <= j-i+1;k++){ 47 dp[i][j] = min(dp[i][j], dp[i+1][i+k-1]+dp[i+k][j]+k*(sum[j] - sum[i+k-1])+a[i]*(k-1)); 48 } 49 } 50 } 51 printf("Case #%d: %d\n", cnt, dp[1][n]); 52 } 53 return 0; 54 }
大佬博客:https://blog.csdn.net/libin56842/article/details/9722077