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("sse2,avx")
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
//#define int long long
using namespace std;
void dout() {
cerr << endl;
}
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << H << ' ';
dout(T...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair <int, int> pii;
const int N = 1e5 + 3, M = 5e5 + 3, inf = 1e9;
int n, m, k, queries, d[N], par[N], sz[N];
int ans[M], X;
bool used[N];
vector <pii> q[N], h[N];
vector <int> marked, dsu[N], order;
void dijkstra() {
set <pii> st;
for (int i = 1; i <= n; i++) {
d[i] = inf;
}
for (auto i : marked) {
d[i] = 0;
st.insert({0, i});
}
while (!st.empty()) {
pii z = *st.begin();
st.erase(z);
int v = z.se;
for (auto i : q[v]) {
int to = i.fi, w = i.se;
if (d[to] > d[v] + w) {
st.erase({d[to], to});
d[to] = d[v] + w;
st.insert({d[to], to});
}
}
}
}
void make_set(int x) {
dsu[x] = {x};
par[x] = x;
sz[x] = 1;
}
int find_set(int x) {
if (x == par[x]) {
return x;
}
return par[x] = find_set(par[x]);
}
void union_set(int x, int y) {
int tx = x, ty = y;
x = find_set(x);
y = find_set(y);
if (x == y) {
return;
}
if (sz[x] < sz[y]) {
swap(x, y);
}
par[y] = x;
sz[x] += sz[y];
for (auto i : dsu[y]) {
for (auto j : h[i]) {
if (find_set(j.fi) == x && d[j.fi] >= X) {
ans[j.se] = max(ans[j.se], X);
}
}
dsu[x].pb(i);
}
}
bool cmp(int x, int y) {
return d[x] > d[y];
}
void solve(int tc) {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y, z;
cin >> x >> y >> z;
q[x].pb({y, z});
q[y].pb({x, z});
}
cin >> k;
for (int i = 1; i <= k; i++) {
int x;
cin >> x;
marked.pb(x);
}
dijkstra();
cin >> queries;
for (int i = 1; i <= queries; i++) {
int x, y;
cin >> x >> y;
h[x].pb({y, i});
h[y].pb({x, i});
}
for (int i = 1; i <= n; i++) {
make_set(i);
order.pb(i);
}
sort(order.begin(), order.end(), cmp);
for (auto i : order) {
X = d[i];
used[i] = true;
for (auto j : q[i]) {
if (used[j.fi]) {
union_set(i, j.fi);
}
}
}
for (int i = 1; i <= queries; i++) {
cout << ans[i] << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tc = 1;
// cin >> tc;
for (int i = 0; i < tc; i++) {
solve(i);
// cleanup();
}
}
Compilation message (stderr)
plan.cpp: In function 'void union_set(int, int)':
plan.cpp:74:9: warning: unused variable 'tx' [-Wunused-variable]
int tx = x, ty = y;
^~
plan.cpp:74:17: warning: unused variable 'ty' [-Wunused-variable]
int tx = x, ty = y;
^~
# | 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... |