Hướng dẫn giải của [CSES 1671] Shortest Routes I
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Bài toán có thể được giải bằng thuật toán Dijkstra.
Độ phức tạp thời gian của lời giải là ~O(n + m \log m)~.
#include <iostream>
#include <queue>
using namespace std;
using ll = long long;
template <typename T>
using reverse_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const int N = 100001;
const ll INF = 1e18;
vector<pair<int, int>> g[N];
ll dist[N];
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int a, b, c;
cin >> a >> b >> c;
g[a].emplace_back(b, c);
}
for (int i = 1; i <= n; ++i) {
dist[i] = INF;
}
reverse_priority_queue<pair<ll, int>> queue;
queue.emplace(0, 1);
dist[1] = 0;
while (!queue.empty()) {
auto [d, x] = queue.top();
queue.pop();
if (dist[x] < d) continue;
for (auto [y, len] : g[x]) {
ll new_d = d + len;
if (new_d < dist[y]) {
dist[y] = new_d;
queue.emplace(new_d, y);
}
}
}
for (int i = 1; i <= n; ++i) {
cout << dist[i] << ' ';
}
cout << '\n';
}
Tài liệu tham khảo
- CPHB (Competitive Programmer's Handbook), Chương 7.3.2, Thuật toán Dijkstra
Chương trình tham khảo
#include <iostream>
#include <queue>
using namespace std;
using ll = long long;
template <typename T>
using reverse_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const int N = 100001;
const ll INF = 1e18;
vector<pair<int, int>> g[N];
ll dist[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int a, b, c;
cin >> a >> b >> c;
g[a].emplace_back(b, c);
}
for (int i = 1; i <= n; ++i) {
dist[i] = INF;
}
reverse_priority_queue<pair<ll, int>> queue;
queue.emplace(0, 1);
dist[1] = 0;
while (!queue.empty()) {
auto [d, x] = queue.top();
queue.pop();
if (dist[x] < d) continue;
for (auto [y, len] : g[x]) {
ll new_d = d + len;
if (new_d < dist[y]) {
dist[y] = new_d;
queue.emplace(new_d, y);
}
}
}
for (int i = 1; i <= n; ++i) {
cout << dist[i] << ' ';
}
cout << '\n';
}
Bình luận