题目:https://www.luogu.org/problemnew/show/P1220
区间DP。
代码如下:
#include<iostream> #include<cstdio> using namespace std; int n,c,pos[55],w[55],sum,s[55][55],dp[55][55][3],INF=10000006; int main() { scanf("%d%d",&n,&c); for(int i=1;i<=n;i++) { scanf("%d%d",&pos[i],&w[i]); sum+=w[i]; if(i==c) { dp[i][i][0]=0;dp[i][i][1]=0; } else { dp[i][i][0]=INF;dp[i][i][1]=INF; } } for(int i=1;i<=n;i++) { int tot=0; for(int j=i;j<=n;j++) { tot+=w[j]; s[i][j]=tot; } } for(int l=2;l<=n;l++) for(int i=1;i<=n-l+1;i++) { int j=i+l-1; dp[i][j][0]=min(dp[i+1][j][0]+(sum-s[i+1][j])*(pos[i+1]-pos[i]), dp[i+1][j][1]+(sum-s[i+1][j])*(pos[j]-pos[i])); dp[i][j][1]=min(dp[i][j-1][0]+(sum-s[i][j-1])*(pos[j]-pos[i]), dp[i][j-1][1]+(sum-s[i][j-1])*(pos[j]-pos[j-1])); } printf("%d",min(dp[1][n][0],dp[1][n][1])); return 0; }
原文地址:https://www.cnblogs.com/Zinn/p/8457022.html
时间: 2024-10-11 22:43:51