hd 6024 dp 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6024 #include<iostream> #include<algorithm> #include<cstring> using namespace std; typedef long long ll; const int N=3010; const int inf=1e9; struct node { ll a,b; }s[N]; bool cmp(node a,node b) { return a.a<b.a; } ll dp[N][2]; int main() { int n; while(scanf("%d",&n)!=EOF) { memset(dp,0,sizeof(dp)); for(int i=0;i<n;i++) scanf("%lld%lld",&s[i].a,&s[i].b); sort(s,s+n,cmp); dp[0][0]=inf; dp[0][1]=s[0].b; for(int i=1;i<n;i++) { dp[i][1]=min(dp[i-1][0],dp[i-1][1])+s[i].b; dp[i][0]=inf; //计算j+1到i的距离的费用 //关于 t += t+=(i-j)*(s[j+1].a-s[j].a); 的解释: //比如我们要算x3 - x1 , x2 - x1的sum,那么由于保证了x是升序排列的,所以sum = (x3 - x2) + 2 * (x2 - x1). ll t=0; for(int j=i-1;j>=0;j--) { t+=(i-j)*(s[j+1].a-s[j].a); dp[i][0]=min(dp[i][0],dp[j][1]+t); } } printf("%lld\n",min(dp[n-1][0],dp[n-1][1])); } return 0; }