LCP 35
Solutions
class Solution {
public:
#define node(c, pos) ((pos + 1) * 101 + (c + 1))
int electricCarPlan(vector<vector<int>>& paths, int cnt, int start, int end, vector<int>& charge) {
using tii = tuple<long, int, int>;
vector<vector<pair<int, int>>> g(100);
for (auto & e : paths) {
int st = e[0], ed = e[1], dis = e[2];
g[st].emplace_back(ed, dis);
g[ed].emplace_back(st, dis);
}
vector<bool> visited(101 * 120, false);
vector<long> dis(101 * 120, 0x3f3f3f3f);
priority_queue<tii, vector<tii>, greater<>> pq;
dis[node(0, start)] = 0;
pq.emplace(0, 0, start);
while (!pq.empty()) {
auto [time, c, cur] = pq.top(); pq.pop();
if (visited[node(c, cur)]) continue;
if (c == 0 && cur == end) break;
visited[node(c, cur)] = true;
// two options
// 1. stay and charge
for (int nc = c + 1; nc <= cnt; nc++) {
int new_time = time + (nc - c) * charge[cur];
if (new_time < dis[node(nc, cur)]) {
dis[node(nc, cur)] = new_time;
pq.emplace(new_time, nc, cur);
}
}
// 2. go to the next place
for (auto & [out, d] : g[cur]) {
if (c < d) continue;
int next = node(c - d, out);
int new_time = time + d;
if (new_time < dis[next]) {
dis[next] = new_time;
pq.emplace(new_time, c - d, out);
}
}
}
return dis[node(0, end)];
}
};Last updated