男神的礼物 最优矩阵链乘 triangulation 双dp
Source
My Solution
这是一个经典的最优矩阵链乘问题,只不过单个费用会改变,像是2个dp搞在一起,是把单个的min换成if语句就好
dp[i-1][i] = a[i-1]*a[i];
da[i][i] = a[i];
dp[i][i] = 0;
#include
#include
//#define LOCAL
using namespace std;
const int INF = 0x3f3f3f3f;
int dp[104][104],a[104],da[104][104];
int main()
{
#ifdef LOCAL
freopen("a.txt","r",stdin);
#endif // LOCAL
int T,n;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
a[0] = 1;
for(int i = 1; i <= n; i++){ scanf("%d", &a[i]); da[i-1][i] = (a[i-1] + a[i]) % 100; dp[i-1][i] = a[i-1]*a[i]; da[i][i] = a[i]; dp[i][i] = 0; } for(int i = n-1; i >= 1; i--){
for(int j = i+1; j <= n; j++){ //前面这里少了个 = 为 j <= n 而不是 <
if(i+1 != j)dp[i][j] = INF;
for(int k = i; k < j; k++){ int &a = dp[i][j], b = da[i][k] * da[k+1][j]+dp[i][k]+dp[k+1][j]; if(a > b){
a = b;
da[i][j] =(da[i][k]+da[k+1][j])%100;
}
if(a == b) da[i][j] = min(da[i][j], (da[i][k]+da[k+1][j])%100);
//dp[i][j] = min(dp[i][j], da[i][k] * da[k+1][j]+dp[i][k]+dp[k+1][j]);
}
}
}
printf("%dn",dp[1][n]);
}
return 0;
}
非特殊说明,本站所有文章均为博主原创,未经许可不得转载。
https://www.prolightsfxjh.com/
Thank you!
------from ProLightsfx
非特殊说明,本博所有文章均为博主原创,未经许可不得转载。
如经许可后转载,请注明出处:https://prolightsfxjh.com/article/uestc-1131/
共有 0 条评论