이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ll long long
#define ld long double
#define all(_v) _v.begin(), _v.end()
#define sz(_v) (int)_v.size()
#define pii pair <int, int>
#define pll pair <ll, ll>
#define veci vector <int>
#define vecll vector <ll>
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
const int MAXN = 1e5 + 10;
int n, m, k;
vector <pii> g[MAXN];
ll d[MAXN];
int up[MAXN][20];
ll mn[MAXN][20];
int tin[MAXN], tout[MAXN], tmr;
void dfs(int v, int p) {
mn[v][0] = d[v];
up[v][0] = p;
for(int i = 1; i < 18; ++i) {
if(up[v][i - 1]) {
up[v][i] = up[up[v][i - 1]][i - 1];
mn[v][i] = min(mn[up[v][i - 1]][i - 1], mn[v][i - 1]);
}
}
tin[v] = ++tmr;
for(auto to : g[v]) if(to.f != p) dfs(to.f, v);
tout[v] = ++tmr;
}
bool upper(int v, int u) {
return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
int lca_(int v, int u) {
if(upper(v, u)) return v;
if(upper(u, v)) return u;
for(int i = 17; i >= 0; --i)
if(up[v][i] && !upper(up[v][i], u)) v = up[v][i];
return up[v][0];
}
ll get_mn(int v, int lca) {
if(v == lca) return d[v];
assert(upper(lca, v));
ll res = 1e18;
for(int i = 17; i >= 0; --i) {
if(up[v][i] && !upper(up[v][i], lca)) {
res = min(res, mn[v][i]);
v = up[v][i];
}
}
return min(res, mn[v][0]);
}
int par[MAXN];
int get(int v) {
return (v == par[v] ? v : par[v] = get(par[v]));
}
bool unite(int v, int u) {
v = get(v), u = get(u);
if(v == u) return 0;
if(rand() & 1) par[v] = u;
else par[u] = v;
return 1;
}
void solve() {
cin >> n >> m;
vector <pii> ed;
for(int i = 1; i <= m; ++i) {
int u, v, w;
cin >> u >> v >> w;
ed.pb(mp(u, v));
g[u].pb(mp(v, w));
g[v].pb(mp(u, w));
}
for(int i = 1; i <= n; ++i) d[i] = 1e18;
cin >> k;
priority_queue <pair <ll, int> > q;
for(int i = 1, v; i <= k; ++i) {
cin >> v;
q.push(mp(0ll, v));
d[v] = 0;
}
while(!q.empty()) {
auto v = q.top();
q.pop();
v.f = -v.f;
if(v.f != d[v.s]) continue;
for(auto to : g[v.s]) {
if(d[to.f] > v.f + to.s) {
d[to.f] = v.f + to.s;
q.push(mp(-d[to.f], to.f));
}
}
}
sort(all(ed), [](const pii &a, const pii &b) {return (min(d[a.f], d[a.s]) < min(d[b.f], d[b.s]));});
reverse(all(ed));
for(int i = 0; i <= n; ++i) {
par[i] = i;
for(int j = 0; j < 18; ++j) mn[i][j] = 1e18;
g[i].clear();
}
for(auto it : ed) {
if(unite(it.f, it.s)) {
g[it.f].pb(mp(it.s, 0));
g[it.s].pb(mp(it.f, 0));
}
}
dfs(1, 0);
cin >> k;
for(int cs = 1, u, v; cs <= k; ++cs) {
cin >> u >> v;
int LCA = lca_(u, v);
cout << min({get_mn(u, LCA), get_mn(v, LCA), d[LCA]}) << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int CNT_TESTS = 1;
///cin >> CNT_TESTS;
for(int NUMCASE = 1; NUMCASE <= CNT_TESTS; ++NUMCASE) {
solve();
if(NUMCASE != CNT_TESTS) cout << '\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... |