题目地址:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1021 经典区间dp,dp[i][j] 表示将从 i 到 j 堆石子合并最小代价,长度为 j-i+1,可看做之前已经合并的 i 到 k 和 k 到 j 两堆石子合并,代价是 i 到 j 的石子数量: #include<iostream> #include<cstring> #include<algorithm> using namesp…
有N堆石子,现要将石子有序的合并成一堆,规定如下:每次只能移动相邻的2堆石子合并,合并花费为新合成的一堆石子的数量.求将这N堆石子合并成一堆的总花费最小. 区间DP思想:现在小区间进行DP得到最优解,然后再利用小区间的最优解组合并求大区间的最优解.(需要从小到大枚举所有可能的区间) 代码(没提交过,不过应该正确): include using namespace std; const int maxn1=300; int main() { int n,a[maxn1]={0},sum[maxn1…
思路 :一道经典的区间dp 唯一不同的时候 终点和起点相连 所以要拆环成链 只需要把1-n的数组在n+1-2*n复制一遍就行了 #include<bits/stdc++.h> using namespace std; const int maxn=10005; int dp[maxn][maxn],dp1[maxn][maxn]; const int INF=10000000; int a[maxn]; int sum[maxn]; int dist(int x,int y){ // c…