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>
//code
#define fl(i,x,y,z) for(int i=x;i<=y;i=i+z)
#define fn(i,x,y,z) for(int i=x;i>=y;i=i-z)
#define rep(i,x,y) for(int i=x;i<y;i=i+1)
#define all(v) v.begin(),v.end()
#define pb emplace_back
#define tle cout<<"tle"<<endl
#define edl cout<<"\n"
#define el "\n"
#define getbit(x,i) ((x>>i)&1)
#define bitcnt __builtin_popcount
//ham
//#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
#define ld long double
using namespace std;
const int N = int(5e5) + 7;
const int inf = 1e9 + 1;
typedef pair<ll, ll> pii;
int n, m, k, d[N], q, u, v, w;
vector<pii> e[N];
struct TEdges {
int u, v, w;
TEdges() {u = v = w = 0;}
TEdges(int u, int v, int w): u(u), v(v), w(w) {}
bool operator < (const TEdges& x)const& {
return w > x.w;
}
};
vector<TEdges> edges;
priority_queue<pii, vector<pii>, greater<pii>> pq;
int lab[N];
int findset(int x)
{
return lab[x] < 0? x: lab[x] = findset(lab[x]);
}
bool uni(int x, int y) {
x = findset(x), y = findset(y);
if(x == y) return 0;
if(lab[x] > lab[y]) swap(x, y);
lab[x] += lab[y], lab[y] = x;
return 1;
}
const int logn = 20;
int p[N][logn + 1], mn[N][logn + 1], h[N];
void dfs(int u) {
fl(i,1,logn,1) {
p[u][i] = p[p[u][i - 1]][i - 1];
mn[u][i] = min(mn[u][i - 1], mn[p[u][i - 1]][i - 1]);
}
for(auto& v: e[u]) {
if(v.fi == p[u][0]) continue;
p[v.fi][0] = u;
mn[v.fi][0] = v.se;
h[v.fi] = h[u] + 1;
dfs(v.fi);
}
}
int get_min(int u, int v) {
if(h[u] < h[v]) swap(u, v);
int res = inf;
fn(i,logn,0,1) {
if(h[u] - (1 << i) >= h[v]) {
res = min(res, mn[u][i]);
u = p[u][i];
}
}
if(u == v) return res;
fn(i,logn,0,1) {
if(p[u][i] != p[v][i]) {
res = min({res, mn[u][i], mn[v][i]});
u = p[u][i], v = p[v][i];
}
}
res = min({res, mn[u][0], mn[v][0]});
return res;
}
void inp()
{
cin >> n >> m>> k>> q;
fl(i,1,m,1)
{
cin >> u >> v >> w;
e[u].pb(v, w);
e[v].pb(u, w);
edges.pb(u, v, w);
}
fill_n(&mn[0][0], N * (logn + 1), inf);
fill(lab, lab + n + 1, -1);
fill(d, d + n + 1, inf);
for(int i = 1; i <= k; ++i) {
cin >> u;
d[u] = 0, pq.emplace(d[u], u);
}
}
void sol()
{
pii top;
while(pq.size()) {
top = pq.top(); pq.pop();
if(top.fi != d[top.se]) continue;
for(auto& v: e[top.se]) {
if(top.fi + v.se < d[v.fi]) {
d[v.fi] = top.fi + v.se;
pq.emplace(d[v.fi], v.fi);
}
}
}
for(auto& it: edges) it.w = min(d[it.u], d[it.v]);
sort(edges.begin(), edges.end());
for(int i = 1; i <= n; ++i) e[i].clear();
for(auto &it: edges) {
if(uni(it.u, it.v)) {
e[it.u].pb(it.v, it.w);
e[it.v].pb(it.u, it.w);
}
}
dfs(1);
while(q --) {
cin >> u >> v;
cout << get_min(u, v) << el;
}
}
signed main()
{
#define name "walk"
if (fopen(name".inp", "r"))
{
freopen(name".inp", "r", stdin);
freopen(name". Out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
inp();
sol();
return 0;
}
Compilation message (stderr)
plan.cpp: In function 'int main()':
plan.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen(name". 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... |