【代码】分层最短路模板

P4568 JLOI2011 飞行路线 - 洛谷 (luogu.com.cn)

即有 K 次免费走边的机会。

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N = 2e5 + 10;
struct node {
	int x;
	LL c;
};
vector<node> G[N];
LL d[N];
int st, ed;
bool operator<(node na, node nb) {
	return na.c > nb.c;
}
priority_queue<node> Q;
bool v[N];

void dijkstra() {
	memset(d, 0x3f, sizeof(d));
	memset(v, 0, sizeof(v));
	Q.push({st, 0}); d[st] = 0;
	while (!Q.empty()) {
		node no = Q.top(); Q.pop();
		if (v[no.x]) {
			continue;
		}
		v[no.x] = 1;
		for (node i : G[no.x]) {
			if (d[i.x] > d[no.x] + i.c) {
				d[i.x] = d[no.x] + i.c;
				Q.push({i.x, d[i.x]});
			}
		}
	}
}

int main () {
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n, m, K;
	cin >> n >> m >> K;
	cin >> st >> ed;
	
	for (int i = 1; i <= m; i ++) {
		int x, y; LL c;
		cin >> x >> y >> c;
		G[x].push_back({y, c});
		G[y].push_back({x, c});
		
		for (int j = 1; j <= K; j ++) {
			G[x + j * n].push_back({y + j * n, c});
			G[y + j * n].push_back({x + j * n, c});
			G[x + (j - 1) * n].push_back({y + j * n, 0});
			G[y + (j - 1) * n].push_back({x + j * n, 0});
		}
	}
	
	for (int j = 1; j <= K; j ++) {
		G[ed + (j - 1) * n].push_back({ed + j * n, 0});
	}
	
	dijkstra();
	
	cout << d[ed + K * n] << "\n";
	
	return 0;
}

AT_abc132_e ABC132E Hopscotch Addict - 洛谷 (luogu.com.cn)

这就是强制要求走三的倍数条边。不打了。

相关推荐
暖焰核心1 小时前
C++模板进阶——特化全解
javascript·c++·jquery
励志不掉头发的内向程序员1 小时前
【LibreCAD 2D架构】从两个坐标到图形实体:RS_ActionDrawLine如何创建RS_Line
开发语言·c++·qt·学习·系统架构
啊阿狸不会拉杆1 小时前
《计算机网络-自顶向下方法》5.2 路由选择算法 读书笔记
计算机网络·算法·路由
rannn_1111 小时前
【力扣hot100】多维动态规划|62、64、5、1143、72
算法·leetcode·动态规划
j7~2 小时前
【C++】C++的类型转换--详解
c++·static_cast·const_cast·rtti·c语言类型转换·c++强制类型转换
吴声子夜歌2 小时前
ApacheCommons——commons-math3(科学计算与线性代数)(一)
java·线性代数·算法·apache
tachibana22 小时前
Embedding 有哪几种算法?
人工智能·算法·ai·大模型·llm·embedding·agent
吴声子夜歌2 小时前
ApacheCommons——commons-configuration2(多数据源配置统一管理与热加载)
java·开发语言·算法·apache
kyle~2 小时前
奇异值分解(SVD)
人工智能·算法·机器学习