【代码】分层最短路模板

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)

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

相关推荐
倒头就睡的小比特5 天前
算法竞赛C++常用的STL
c++·算法
weilx12345 天前
C++笔记-文件IO-<fcntl.h>
c++
小羊没烦恼!5 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼5 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
Smileyqp沛沛5 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车5 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光5 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark5 天前
大规模并行计算中的负载均衡算法研究4
算法
吞下星星的少年·-·5 天前
C++ 萌新语法入门篇
c++·算法比赛
Because_of_Her15 天前
并查集-听课笔记
笔记·算法·并查集