分层图最短路

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

}

};

相关推荐
小雉系统4 小时前
BcoreOS安全通信架构:FsSession 抗算法后门安全设计
算法·安全·架构
唐墨1238 小时前
linux kernel 比较数据大小的宏定义
linux·算法
拂拉氏9 小时前
【知识讲解-题目讲解】 统计满足K约束的子字符串数量2 (滑动窗口+前缀和+二分查找+分析)
算法·前缀和·二分查找·滑动窗口
通信小呆呆13 小时前
电子战抗干扰技术:当“不被干扰”成为一种系统
算法·通信·mimo·isac
笑容掩饰我的失落13 小时前
区块链从小白到精通(八):从零实现一个标准 ERC-20 代币(完整可运行代码)
算法·区块链·php·智能合约·哈希算法
Severus_black14 小时前
【C++初阶】Stack、Queue算法题大练兵!
c语言·c++·算法·stl
2601_9679791216 小时前
机器学习必修课:经典算法与Python实战
python·算法·机器学习·实战·课程
程序猿打工仔17 小时前
计算机基础(C语言)考编、专技岗
c语言·开发语言·算法
通信小呆呆17 小时前
电子战干扰技术:从“蛮力压制”到“精准欺骗”,再到“认知博弈”
算法·通信·isac