题面
题解
我们以六边形为例,假设划分的区间为[l,r] 又因为划分的三角形不能重合,所以我们可以分为三部分,左右和中间,那么 f[l][r] = f[l][k] + f[k][r] + w[l]*w[k]*w[r] ,要想f[l][r]最小,就要使f[l][k]最小+f[k][r]最小,我们只需要枚举[l,r],然后用k划分,更新集合即可
由于题中数据范围较大,所以要用高精度,这里直接用java的BigInteger
代码
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static int N = 55;
static int n;
static BigInteger[] w = new BigInteger[N];
static BigInteger f[][] = new BigInteger[N][N];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 1; i <= n; i++) {
w[i] = sc.nextBigInteger();
}
for (int len = 2; len <= n; len++) {
for (int l = 1; l + len - 1 <= n; l++) {
int r = l + len - 1;
f[l][r] = new BigInteger("1000000000000000000000000000");
if (len == 2) f[l][r] = new BigInteger("0");
else {
for (int k = l + 1; k < r; k++) {
f[l][r] = f[l][r].min(f[l][k].add(f[k][r]).add((w[l].multiply(w[k]).multiply(w[r]))));
}
}
}
}
System.out.println(f[1][n]);
}
}