这题相当于二分法,就两个决策,向左向右,关于费用计算要注意,作者是每次都把所有点的总费用累积到状态值中。
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<sstream>
#include<cassert>
using namespace std;
const int maxn = 1000 + 5;
const double INF = 1e30;
struct Section {
double x, c, dt;
bool operator<(const Section &rhs) {
return x < rhs.x;
}
}s[maxn];
int kase=0, n;
int vis[maxn][maxn][2];
double v, x, d[maxn][maxn][2];
double psdt[maxn];
double cost(double x1, double x2, int i, int j) {
double finished_dt = 0;
assert(i <= j);//返回为假就结束程序
if (i >= 0 && j >= 0)finished_dt += psdt[j] - psdt[i - 1];
return (psdt[n] - finished_dt)*fabs(x2 - x1) / v;
}
double dp(int i, int j, int p) {
if (i == 1 && j == n)return 0;
double &ans = d[i][j][p];
if (vis[i][j][p] == kase)return ans;
vis[i][j][p] = kase;
ans = INF;
double x = (p == 0 ? s[i].x : s[j].x);
if (i > 1)ans = min(ans, dp(i - 1, j, 0) + cost(x, s[i - 1].x, i, j));
if (j < n)ans = min(ans, dp(i, j + 1, 1) + cost(x, s[j + 1].x, i, j));
return ans;
}
int main() {
memset(vis, 0, sizeof(vis));
while (scanf("%d%lf%lf", &n, &v, &x) == 3 && n) {
++kase;
double sumc = 0;
for (int i = 1; i <= n; i++) {
scanf("%lf%lf%lf", &s[i].x, &s[i].c, &s[i].dt);
sumc += s[i].c;
}
sort(s + 1, s + n + 1);
psdt[0] = 0;
for (int i = 1; i <= n; i++) {
psdt[i] = psdt[i - 1] + s[i].dt;
}
s[0].x = -INF;
s[n + 1].x = INF;
double ans = INF;
for (int i = 1; i <= n + 1; i++) {
if (x > s[i - 1].x&&x < s[i].x) {
if (i > 1)ans = min(ans, dp(i - 1, i - 1, 0) + cost(x, s[i - 1].x, -1,-1));
if (i <= n)ans = min(ans, dp(i, i, 0) + cost(x, s[i].x, -1, -1));
break;
}
}
printf("%.0lf", floor(ans + sumc));
}
return 0;
}