lc2714
带状态的Dijkstra算法求解允许最多跳过k条边权重的最短路径
状态为(节点, 剩余跳过次数),每次转移可选择走边或跳过
typedef pair<int, int> PII;
typedef tuple<int, int, int> TIII;
class Solution {
public:
int shortestPathWithHops(int n, vector<vector<int>>& edges, int s, int d, int k) {
vector<vector<PII>> g(n);
for (auto& edge : edges) {
int a = edge0, b = edge1, c = edge2;
ga.emplace_back(b, c);
gb.emplace_back(a, c);
}
vector<vector<int>> dist(n, vector<int>(k + 1, INT_MAX));
dists0 = 0;
vector<vector<bool>> seen(n, vector<bool>(k + 1, false));
priority_queue<TIII, vector<TIII>, greater<TIII>> pq;
pq.emplace(0, s, 0);
while (!pq.empty()) {
auto td, u, tk = pq.top();
pq.pop();
if (seenutk) continue;
seenutk = true;
for (auto& v, nd : gu) {
if (distvtk > td + nd) {
distvtk = td + nd;
pq.emplace(td + nd, v, tk);
}
if (tk < k and distvtk + 1 > td) {
distvtk + 1 = td;
pq.emplace(td, v, tk + 1);
}
}
}
return *min_element(distd.begin(), distd.end());
}
};