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>
//#define FILENAME ""
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back
#define F first
#define S second
using namespace std;
#define int long long
const int N = 1e5 + 5;
const int INF = 1e12 + 5;
const double PI = acos(-1);
typedef long long ll;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
const int mod = 1e9 + 7;
int n, m, k, d[N], up[21][N], cost[21][N], p[N], s[N], h[N];
vector<pair<int,int>> g[N];
vector<int> gr[N];
int get(int a) {
if (p[a] == a) return a;
return p[a] = get(p[a]);
}
void unite(int a, int b) {
if (s[a] < s[b]) swap(a, b);
s[a] += s[b];
p[b] = a;
}
void dfs(int v, int p = 0) {
h[v] = h[p] + 1;
up[0][v] = p;
cost[0][v] = min(d[v], d[p]);
for (int i = 1; i <= 20; i++) {
up[i][v] = up[i - 1][up[i - 1][v]];
cost[i][v] = min(cost[i - 1][v], cost[i - 1][up[i - 1][v]]);
}
for (int i = 0; i < sz(gr[v]); i++) {
int to = gr[v][i];
if (to != p) dfs(to, v);
}
}
int ans(int a, int b) {
int mn = INF;
if (h[a] > h[b]) {
swap(a, b);
}
for (int i = 20; i >= 0; i--) {
if (h[a] <= h[up[i][b]]) {
mn = min(mn, cost[i][b]);
b = up[i][b];
}
}
if (a == b) return mn;
for (int i = 20; i >= 0; i--) {
if (up[i][a] != up[i][b]) {
mn = min(mn, cost[i][a]);
mn = min(mn, cost[i][b]);
b = up[i][b];
a = up[i][a];
}
}
return min({mn, cost[0][a], cost[0][b]});
}
main() {
#ifdef FILENAME
freopen(FILENAME".in", "r", stdin);
freopen(FILENAME ".out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1, u, v, x; i <= m; i++) {
cin >> u >> v >> x;
g[u].pb({v, x});
g[v].pb({u, x});
}
set<pair<int,int>> st;
for (int i = 0; i <= n; i++) {
d[i] = INF;
}
cin >> k;
for (int i = 1, x; i <= k; i++) {
cin >> x;
d[x] = 0;
st.insert({0, x});
}
while(st.size()) {
int v = st.begin() -> second;
st.erase(st.begin());
for (int i = 0; i < sz(g[v]); i++) {
int to = g[v][i].F;
int len = g[v][i].S;
if (d[v] + len < d[to]) {
st.erase({d[to], to});
d[to] = d[v] + len;
st.insert({d[to], to});
}
}
}
vector<pair<int,pair<int,int>>> w;
for (int i = 1; i <= n; i++) {
p[i] = i;
s[i] = 1;
for (int j = 0; j < sz(g[i]); j++) {
w.pb({min(d[i], d[g[i][j].F]), {i, g[i][j].F}});
}
}
sort(all(w));
for (int i = sz(w) - 1; i >= 0; i--) {
int a = w[i].S.F, b = w[i].S.S;
if (get(a) != get(b)) {
unite(get(a), get(b));
gr[a].pb(b);
gr[b].pb(a);
}
}
for (int i = 0; i <= 20; i++)
for (int j = 1; j <= N - 5; j++)
cost[i][j] = INF;
dfs(1);
int q;
cin >> q;
while(q--) {
int a, b;
cin >> a >> b;
cout << ans(a, b) << "\n";
}
return 0;
}
Compilation message (stderr)
plan.cpp:79:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
# | 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... |