55 lines
1.3 KiB
C++
55 lines
1.3 KiB
C++
|
#include<stdio.h>
|
||
|
int cost[2050][2050];
|
||
|
// broadcast_cost[i][j]=cost[j][i]
|
||
|
int tensor[2050][32];
|
||
|
int prefix_sum[2050][32];
|
||
|
int max[32];
|
||
|
int n,k;
|
||
|
int broadcast_cost(int i,int j){
|
||
|
int cost=1;
|
||
|
for(int t=0;t<=k-3;t++){
|
||
|
if(prefix_sum[j+1][t]-prefix_sum[i][t])
|
||
|
cost*=max[t];
|
||
|
}
|
||
|
cost*=tensor[i][k-2];
|
||
|
cost*=tensor[j][k-1];
|
||
|
return cost;
|
||
|
}
|
||
|
int main(){
|
||
|
scanf("%d%d",&n,&k);
|
||
|
for(int i=0;i<n;i++){
|
||
|
for(int j=0;j<k;j++){
|
||
|
scanf("%d",&tensor[i][j]);
|
||
|
}
|
||
|
cost[i][i]=0;
|
||
|
}
|
||
|
|
||
|
for(int t=0;t<=k-3;t++){
|
||
|
int sum=0;
|
||
|
for(int i=0;i<n;i++){
|
||
|
prefix_sum[i][t]=sum;
|
||
|
if(tensor[i][t]!=1){
|
||
|
max[t]=tensor[i][t];
|
||
|
sum++;
|
||
|
}
|
||
|
}
|
||
|
prefix_sum[n][t]=sum;
|
||
|
}
|
||
|
for(int i=0;i<n;i++){
|
||
|
for(int j=i+1;j<n;j++){
|
||
|
cost[j][i]=broadcast_cost(i,j);
|
||
|
}
|
||
|
}
|
||
|
for(int t=1;t<n;t++){
|
||
|
for(int i=0;i+t<n;i++){
|
||
|
int min=2147483647;
|
||
|
for(int j=i;j<i+t;j++){
|
||
|
if(cost[i][j]+cost[j+1][i+t]+cost[i+t][i]*tensor[j][k-1]<min){
|
||
|
min=cost[i][j]+cost[j+1][i+t]+cost[i+t][i]*tensor[j][k-1];
|
||
|
}
|
||
|
}
|
||
|
cost[i][i+t]=min;
|
||
|
}
|
||
|
}
|
||
|
printf("%d",cost[0][n-1]);
|
||
|
}
|