This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <cmath>
#include <queue>
#include <bitset>
#include <numeric>
#include <array>
#include <cstring>
#include <random>
#include <chrono>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<class T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 1e5 + 42;
vector<pair<int, int> > g[N];
int dist[N];
int ans[N];
set<int> s[N];
int p[N], size[N];
int curdist = 0;
int findset(int x) {
if(x == p[x]) return x;
return p[x] = findset(p[x]);
}
void unionsets(int a, int b) {
a = findset(a);
b = findset(b);
if(a == b) return;
if(s[a].size() < s[b].size()) swap(s[a], s[b]);
p[b] = a;
for(auto& x : s[b]) {
if(s[a].count(x)) { s[a].erase(x); ans[x] = curdist; }
else s[a].insert(x);
}
s[b].clear();
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n = 0, m = 0;
cin >> n >> m;
for(int i = 0; i < n; i++) { p[i] = i; dist[i] = -1; }
for(int i = 0; i < m; i++) {
int a, b, w;
cin >> a >> b >> w;
g[--a].pb(mp(--b, w));
g[b].pb(mp(a, w));
}
int k = 0; cin >> k;
priority_queue<pair<int, int> > pq;
for(int i = 0; i < k; i++) {
int g; cin >> g; g--;
dist[g] = 0;
pq.push(mp(0, g));
}
while(!pq.empty()) {
auto cur = pq.top(); pq.pop();
cur.fi *= -1;
for(auto& x : g[cur.se])
if(dist[x.fi] == -1 || dist[x.fi] > dist[cur.se] + x.se) {
dist[x.fi] = dist[cur.se] + x.se;
pq.push(mp(-dist[x.fi], x.fi));
}
}
int q; cin >> q;
for(int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
s[--a].insert(i);
s[--b].insert(i);
}
vector<pair<int, int> > ord;
for(int i = 0; i < n; i++) ord.pb(mp(dist[i], i));
sort(rall(ord));
for(int i = 0; i < n; i++) {
curdist = ord[i].fi;
for(auto& x : g[ord[i].se]) {
if(dist[x.fi] >= curdist)
unionsets(x.fi, ord[i].se);
}
}
for(int i = 0; i < q; i++) cout << ans[i] << '\n';
return 0;
}
/*
*/
Compilation message (stderr)
plan.cpp:3: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
3 | #pragma GCC optimization ("unroll-loops")
|
# | 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... |