이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |