【代码】分层最短路模板

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)

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

相关推荐
kissskill10 小时前
MUDUO 学习笔记
c++·muduo
饮水桶_T10 小时前
Java调用大语言模型、多轮对话 笔记
笔记·算法·设计模式·电脑·ai编程
AAA修煤气灶刘哥11 小时前
QQ 音乐怎么在 25 毫秒内,决定一句话该交给谁?
人工智能·算法·llm
开开心心就好12 小时前
电脑禁止自动休眠工具,还能按负载定时
算法·智能手机·pdf·电脑·word·音视频·lua
geovindu1 天前
python:DecisionTree
开发语言·后端·python·算法
ShineWinsu1 天前
对于Qt:事件的解析
c++·qt·面试·笔试·gui·客户端·事件
geovindu1 天前
python: Decision Tree Regressor
开发语言·后端·python·算法·决策树
闻缺陷则喜何志丹1 天前
【数论】P10136 [USACO24JAN] Cowlendar S|普及+
c++·数学·数论·洛谷
apcipot_rain1 天前
某游戏公司C++游开实习 机试复盘
c++·游戏