我有一个问题,我从 A 点到 B 点旅行,并且距离定义为 l
.在我的车里,我有一个坦克,只能让我走b
公里。n
地方都有加油站及其加油的成本(我只能完全加满油(。我从一个装满的坦克开始。
最低的成本从高速公路的起点到终点最有效的算法是什么?
我最近的想法:
使用窗口滑动最小算法来查找(长度为b
(那些成本最小的站点。法典:
int n, range, targetDist;
scanf("%d %d %d", &n, &targetDist, &range);
int di, ci;
for (int k = 1; k <= n; k++)
{
scanf("%d %d", &di, &ci);
D[k] = di;
C[k] = ci;
}
D[0] = C[0] = bestcost[0] = 0;
for (int i = 1; i < n+1; ++i)
{
bestcost[i] = 1 << 30;
for (int j = 0; j < i; ++j)
{
int xx = bestcost[j] + C[i];
if (D[i] - D[j] <= range && xx < bestcost[i])
{
bestcost[i] = xx;
printf("(%d, %d)n", i, bestcost[i]);
}
}
}
输入为:
3 8 4
2 1
4 2
6 3
输出为:
(1, 1)
(2, 2)
(3, 4)
所以它对应于(i,成本(i((的成本 - 要到达第i站,我必须支付成本(i(。
如何利用这些信息找到整个距离的最低成本?
我找到了一种使用滑动窗口算法在 O(n( 时间内做到这一点的方法:
#include <vector>
#include <algorithm>
#include <deque>
#include <stdio.h>
typedef unsigned long long int ulli;
using namespace std;
void sliding_window_minimum(vector<pair<ulli, ulli>> st, ulli n, ulli targetDist, ulli range)
{
deque<pair<ulli, ulli>> minimize;
ulli j = 0;
for (ulli i = 0; i < n; i++)
{
if (st[i].first <= range)
{
while (!(minimize.empty()) && (minimize.back().second > st[i].second))
{
minimize.pop_back();
}
minimize.push_back(st[i]);
j++;
}
else
{
break;
}
}
for (ulli k = j; k < n; k++)
{
while (!(minimize.empty()) && ((st[k].first - minimize.front().first) > range))
{
minimize.pop_front();
}
if (minimize.empty()) {
break;
}
ulli tempCost = st[k].second + minimize.front().second;
while (!(minimize.empty()) && (minimize.back().second > tempCost))
{
minimize.pop_back();
}
minimize.push_back(make_pair(st[k].first, tempCost));
}
while (!(minimize.empty()) && ((targetDist - minimize.front().first) > range))
{
minimize.pop_front();
}
if (minimize.empty())
{
printf("NIEn");
}
else
{
printf("%llu", minimize.front().second);
}
}
int main()
{
ulli n, d, b;
scanf("%llu %llu %llu", &n, &d, &b);
if (b >= d)
{
printf("%d", 0);
return 0;
}
int temp = n;
ulli d1, c1;
vector<pair<ulli, ulli>> st;
st.reserve(n+1);
while (temp--)
{
scanf("%llu %llu", &d1, &c1);
st.push_back(make_pair(d1, c1));
}
sliding_window_minimum(st, n, d, b);
}