This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
const int N = 100011;
set<ll> qr[N];
int n, m;
ll d[N], p[N], res[N];
vector<pair<int, int>> g[N];
void dijkstra() {
int k;
cin >> k;
fill(d + 1, d + N, LLONG_MAX);
set<pair<ll, int>> st;
for (int i = 0; i < k; i++) {
int x;
cin >> x;
d[x] = 0;
st.insert({0, x});
}
while (!st.empty()) {
int v = st.begin()->second;
st.erase(st.begin());
for (auto [to, w] : g[v]) {
if (d[to] > d[v] + w) {
st.erase({d[to], to});
d[to] = d[v] + w;
st.insert({d[to], to});
}
}
}
}
int get(int v) {
if (p[v] == v) return v;
return p[v] = get(p[v]);
}
int cur = 0;
void merge(int v, int u) {
v = get(v);
u = get(u);
if (v == u) return;
p[u] = v;
if (qr[v].size() < qr[u].size()) {
qr[v].swap(qr[u]);
}
for (auto i : qr[u]) {
if (qr[v].find(i) == qr[v].end()) {
qr[v].insert(i);
} else {
res[i] = cur;
qr[v].erase(i);
}
}
qr[u].clear();
}
void process()
{
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y, w;
cin >> x >> y >> w;
g[x].emplace_back(y, w);
g[y].emplace_back(x, w);
}
dijkstra();
int q;
cin >> q;
for (int i = 1; i <= q; i++)
{
int s, t;
cin >> s >> t;
qr[s].insert(i);
qr[t].insert(i);
}
vector<int> b(n);
iota(b.begin(), b.end(), 1);
sort(b.begin(), b.end(), [&](int x, int y) {
return d[x] > d[y];
});
iota(p + 1, p + n + 1, 1);
for (int i : b)
{
cur = d[i];
for (auto [to, w] : g[i])
{
if (d[to] >= d[i]) {
merge(i, to);
}
}
}
for (int i = 1; i <= q; i++)
{
cout << res[i] << '\n';
}
}
main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
#define task "phcbaka"
if (fopen(task ".inp", "r"))
{
freopen(task ".inp", "r", stdin);
freopen(task ".out", "w", stdout);
}
process();
}
Compilation message (stderr)
plan.cpp:113:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
113 | main()
| ^~~~
plan.cpp: In function 'int main()':
plan.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | freopen(task ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | freopen(task ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |