Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://lqdoj.edu.vn/problem/CJDUTIEC
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- using pi32 = pair<int, int>;
- using pi64 = pair<int64_t, int64_t>;
- using ti32 = tuple<int, int, int>;
- using ti64 = tuple<int64_t, int64_t, int64_t>;
- const int N = 1e5 + 5;
- const int INF = 1e18 + 7;
- int n, nE, s, t;
- vector<pi32> graph[N];
- int64_t d[N];
- int parent[N];
- void Dijkstra(int s) {
- fill(parent + 1, parent + n + 1, -1);
- fill(d + 1, d + n + 1, INF);
- d[s] = 0;
- priority_queue<pi64, vector<pi64>, greater<pi64>> pq;
- pq.push({0, s});
- while (!pq.empty()) {
- const auto [du, u] = pq.top(); pq.pop();
- if (d[u] > du) continue;
- for (const auto &[uv, v] : graph[u]) {
- if (d[v] > d[u] + uv) {
- d[v] = d[u] + uv;
- parent[v] = u;
- pq.push({d[v], v});
- }
- }
- }
- }
- int main() {
- #ifdef LOCAL
- freopen("in.txt", "r", stdin);
- #else
- freopen("CJDUTIEC.inp", "r", stdin);
- freopen("CJDUTIEC.out", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cin >> n >> nE >> s >> t;
- for (int i = 1; i <= nE; i++) {
- int u, v, w; cin >> u >> v >> w;
- graph[u].push_back({w, v});
- graph[v].push_back({w, u});
- }
- Dijkstra(s);
- vector<int> path;
- int u = t;
- while (u != -1) {
- path.push_back(u);
- u = parent[u];
- }
- cout << d[t] << '\n';
- reverse(path.begin(), path.end());
- for (int u : path)
- cout << u << ' ';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment