题目:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3469
这道题关键要找到一个规律:
派送任意一个区间[i,j]的用户,按照最优路线走,一定最后停留在该区间的两端处,即 i 和 j !!!
所以,就会有以下的状态设计,从而可以轻松写出状态转移方程了:
dp[i][j][0] := 派送区间[i,j]的用户并且最后派送 i 时的最小值
dp[i][j][1] := 派送区间[i,j]的用户并且最后派送 j 时的最小值
dp[i][j][0] = min(dp[i+1][j][0]+cost1, dp[i+1][j][1]+cost2)
dp[i][j][1] = min(dp[i][j-1][0]+cost3, dp[i][j-1][1]+cost4)
cost1 = (x[i+1]-x[i])*(sum[n]-sum[j]+sum[i])
cost2 = (x[j]-x[i])*(sum[n]-sum[j]+sum[i])
cost3 = (x[j]-x[i])*(sum[n]-sum[j-1]+sum[i-1])
cost4 = (x[j]-x[j-1])*(sum[n]-sum[j-1]+sum[i-1])
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 1000 + 5;
int dp[MAXN][MAXN][2];
int sum[MAXN];
struct P{
int x, b;
bool operator<(const P& p) const {
return x < p.x;
}
bool operator<(int X) const {
return x < X;
}
} p[MAXN];
int main ()
{
int N, V, X;
while(scanf("%d%d%d", &N, &V, &X) != EOF) {
for(int i=1; i<=N; i++) {
scanf("%d%d", &p[i].x, &p[i].b);
}
++N;
p[N].x = X;
p[N].b = 0;
sort(p+1, p+1+N);
for(int i=1; i<=N; i++) sum[i] = sum[i-1] + p[i].b;
int pos = lower_bound(p+1, p+1+N, X) - p;
for(int i=1; i<=N; i++) {
for(int j=1; j<=N; j++) {
dp[i][j][0] = dp[i][j][1] = INF;
}
}
dp[pos][pos][0] = dp[pos][pos][1] = 0;
for(int i=pos; i>=1; i--) {
for(int j=pos; j<=N; j++) {
if(i==j) continue;
dp[i][j][0] = min(dp[i][j][0], dp[i+1][j][0]+(sum[i]+sum[N]-sum[j])*(p[i+1].x-p[i].x));
dp[i][j][0] = min(dp[i][j][0], dp[i+1][j][1]+(sum[i]+sum[N]-sum[j])*(p[j].x-p[i].x));
dp[i][j][1] = min(dp[i][j][1], dp[i][j-1][0]+(sum[i-1]+sum[N]-sum[j-1])*(p[j].x-p[i].x));
dp[i][j][1] = min(dp[i][j][1], dp[i][j-1][1]+(sum[i-1]+sum[N]-sum[j-1])*(p[j].x-p[j-1].x));
}
}
printf("%d\n", V*min(dp[1][N][0], dp[1][N][1]));
memset(dp, 0, sizeof(dp));
}
return 0;
}