제출 #953557

#제출 시각아이디문제언어결과실행 시간메모리
953557nhatcaoEvent Hopping (BOI22_events)C++14
0 / 100
3 ms5208 KiB
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; int main() { int n, m; cin >> n >> m; // Số lượng sự kiện và số lượng cặp sự kiện vector<vector<pair<int, int>>> graph(n); // Đồ thị lưu trữ các sự kiện và trọng số của cặp sự kiện for (int i = 0; i < m; ++i) { int a, b, w; cin >> a >> b >> w; // Sự kiện a, sự kiện b, và trọng số w --a; --b; // Chuyển về 0-based index graph[a].emplace_back(b, w); graph[b].emplace_back(a, w); } vector<int> dist(n, INF); // Khoảng cách ngắn nhất từ sự kiện A đến các sự kiện khác dist[0] = 0; // Bắt đầu từ sự kiện 0 priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; pq.emplace(0, 0); // Đưa sự kiện 0 vào hàng đợi ưu tiên while (!pq.empty()) { int u, d; tie(d, u) = pq.top(); pq.pop(); if (d > dist[u]) continue; // Nếu khoảng cách hiện tại lớn hơn, bỏ qua for (auto& edge : graph[u]) { int v, w; tie(v, w) = edge; if (dist[u] + w < dist[v]) { dist[v] = dist[u] + w; pq.emplace(dist[v], v); } } } int q; cin >> q; // Số lượng truy vấn while (q--) { int a, b; cin >> a >> b; // Sự kiện A và sự kiện B --a; --b; // Chuyển về 0-based index cout << (dist[a] == INF || dist[b] == INF ? "impossible" : to_string(dist[a] + dist[b])) << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...