分层图最短路

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());

}

};

相关推荐
xunberg1 小时前
【生命科学 Agent】推理平台(三)
算法·程序员·架构
INGNIGHT1 小时前
151 · 买卖股票的最佳时机 III(序列dp)
数据结构·算法·leetcode
庖丁解牛1 小时前
十进制转二进制:凑数法(填1法)
算法
行者全栈架构师1 小时前
【共创稿事节】双引擎智能相册:HarmonyOS 7(API 26)文搜图 × 图像超分端侧 AI 联动实战
前端·算法·架构
小蒋观天下2 小时前
【无标题】红外补光近距过曝、远距欠曝:导致车牌AI识别失效调试
人工智能·深度学习·数码相机·算法·机器学习·ai大模型
吞下星星的少年·-·3 小时前
牛客周赛 119B题(贪心)
算法·贪心
NEFU_Jyj3 小时前
牛客周赛R166(max型瓶颈最短路,前后缀DP合并)
算法·深度优先
Tisfy3 小时前
LeetCode 3644.排序排列:不对则与(脑筋急转弯)
数据结构·算法·leetcode·题解·位运算
lisw054 小时前
分岔论揭示演化突变密码
开发语言·算法·数学建模
aqiu1111115 小时前
C++ TopK:使用 nth_element 快速求最小 K 个数(蓝桥杯 / LeetCode 可用)
算法