我有一个问题,我从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)。
如何使用该信息找到整个距离的最低成本?
答案 0 :(得分:1)
我找到了一种在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("NIE\n");
}
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);
}