Pintia 天梯地图 dijkstra进阶
7-14 天梯地图 - SMU 2024 spring 天梯赛3(补题) (pintia.cn)
dijkstra进阶做法,包含路径记录,以及按权重统计路径条件等;
不过最开始我一直将优先队列开的最大堆,但是一直过不了自己的例子,后来改成最小堆并且路径值改成负数存进去就对了,再后来我发现改成最大堆也可以,不过要把点值改成负数,最后才焕然大悟,这题路径取最短的同时,节点值也应该尽量的小(
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
struct DIJ {
using i64 = long long;
using PII = pair<i64, i64>;
vector<i64> dis, path, node;
vector<vector<array<int, 3>>> G;
int n;
DIJ() {}
DIJ(int n): n(n) {
node.resize(n + 1, 1);
dis.assign(n + 1, 1e18);
G.resize(n + 1);
path.resize(n + 1, -1);
}
void add(int u, int v, int w, int val) {
G[u].push_back({v, w, val});
}
void dijkstra(int s) {
priority_queue<PII,vector<PII>,greater<PII>> que;
dis[s] = 0;
que.push({0, -s});
while (!que.empty()) {
auto p = que.top();
que.pop();
int u = -p.second;
if (dis[u] < p.first) continue;
for (auto [v, w, val] : G[u]) {
if (dis[v] > dis[u] + w) {
node[v] = node[u] + val;
dis[v] = dis[u] + w;
que.push({ dis[v], -v});
path[v] = u;
} else if (dis[v] == dis[u] + w) {
if (node[v] > node[u] + val) {
node[v] = node[u] + val;
path[v] = u;
}
}
}
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
DIJ time(n), way(n);
while (m --) {
int u, v, c, t, w;
cin >> u >> v >> c >> w >> t;
time.add(u, v, t, w);
way.add(u, v, w, 1);
if (!c) {
time.add(v, u, t, w);
way.add(v, u, w, 1);
}
}
int st, ed, ok;
cin >> st >> ed;
time.dijkstra(st);
way.dijkstra(st);
ok = ed;
vector<int> ans, ans1;
while (ok != -1) {
ans.push_back(ok);
ok = time.path[ok];
}
ok = ed;
while (ok != -1) {
ans1.push_back(ok);
ok = way.path[ok];
}
if (ans1 == ans) {
cout << "Time = " << time.dis[ed] << "; ";
cout << "Distance = " << way.dis[ed] << ": ";
for (int i = ans1.size() - 1; i >= 0; i --) {
cout << ans1[i] ;
if (i) cout << " => ";
else cout << '\n';
}
} else {
cout << "Time = " << time.dis[ed] << ": ";
for (int i = ans.size() - 1; i >= 0; i --) {
cout << ans[i] ;
if (i) cout << " => ";
else cout << '\n';
}
cout << "Distance = " << way.dis[ed] << ": ";
for (int i = ans1.size() - 1; i >= 0; i --) {
cout << ans1[i] ;
if (i) cout << " => ";
else cout << '\n';
}
}
return 0;
}