#include <bits/stdc++.h>
#define task "BriantheCrab"
#define int long long
#define pii pair <int, int>
#define fi first
#define se second
#define szf sizeof
#define sz(s) (int)((s).size())
using namespace std;
template <class T> void mini (T &t, T f) {if (t > f) t = f;}
template <class T> void maxi (T &t, T f) {if (t < f) t = f;}
const int maxN = 1e5 + 5;
const int LOG = 20;
const int inf = 1e18 + 7;
const int mod = 1e9 + 7;
struct Node {
int u, dis;
bool operator < (const Node& other) const {
return dis > other.dis;
}
};
int n, m, K;
int L[maxN], d[maxN], h[maxN];
int par[maxN], sz[maxN];
int mi[maxN][LOG], mn[maxN][LOG], up[maxN][LOG];
bool check[maxN];
vector <Node> adj[maxN];
vector <int> nAdj[maxN], spe;
vector <pii> all;
void multiSources () {
priority_queue <Node> pq;
for (int i = 1; i <= n; i ++) {
L[i] = inf;
}
for (auto u : spe) {
L[u] = 0;
pq.push ({u, 0});
}
while (!pq.empty ()) {
auto [u, c] = pq.top ();
pq.pop ();
if (c > L[u]) {
continue;
}
for (auto [v, dis] : adj[u]) {
if (L[v] > L[u] + dis) {
L[v] = L[u] + dis;
pq.push ({v, L[v]});
}
}
}
}
void init () {
for (int i = 1; i <= n; i ++) {
par[i] = i;
sz[i] = 1;
}
}
int getRoot (int u) {
if (par[u] == u) {
return u;
}
return (par[u] = getRoot (par[u]));
}
void merge (int u, int v) {
u = getRoot (u);
v = getRoot (v);
if (u == v) {
return;
}
if (sz[u] < sz[v]) {
swap (u, v);
}
par[v] = u;
sz[u] += sz[v];
return;
}
void dfs (int u, int p) {
up[u][0] = p;
for (int j = 1; j < LOG; j ++) {
up[u][j] = up[up[u][j - 1]][j - 1];
mi[u][j] = min (mi[u][j - 1], mi[up[u][j - 1]][j - 1]);
}
for (auto v : nAdj[u]) {
if (v == p) {
continue;
}
h[v] = h[u] + 1;
mi[v][0] = L[v];
dfs (v, u);
}
}
int LCA (int u, int v) {
if (h[u] < h[v]) {
swap (u, v);
}
int k = h[u] - h[v];
int res = inf;
for (int j = LOG - 1; j >= 0; j --) {
if ((k >> j) & 1) {
res = min (res, mi[u][j]);
u = up[u][j];
}
}
if (u == v) {
return res;
}
for (int j = LOG - 1; j >= 0; j --) {
if (up[u][j] != up[v][j]) {
res = min ({res, mi[u][j], mi[v][j]});
u = up[u][j];
v = up[v][j];
}
}
return min ({res, mi[u][0], mi[v][0]});
}
void solve () {
cin >> n >> m;
for (int i = 1; i <= m; i ++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back ({v, w});
adj[v].push_back ({u, w});
}
cin >> K;
for (int i = 1; i <= K; i ++) {
int u;
cin >> u;
spe.push_back (u);
}
multiSources ();
for (int i = 1; i <= n; i ++) {
all.push_back ({L[i], i});
}
sort (all.rbegin (), all.rend ());
init ();
// for (int cc = 1; cc <= n; cc ++) {
// cout << par[cc] << ' ';
// }
// cout << '\n';
// for (int i = 1; i <= n; i ++) {
// cout << L[i] << ' ';
// }
//cout << '\n';
for (auto [_, u] : all) {
check[u] = 1;
for (auto [v, w] : adj[u]) {
int rtU = getRoot (u);
int rtV = getRoot (v);
if (rtU == rtV || check[v] == 0) {
continue;
}
merge (u, v);
// for (int cc = 1; cc <= n; cc ++) {
// cout << par[cc] << ' ';
// }
// cout << '\n';
nAdj[u].push_back (v);
nAdj[v].push_back (u);
//cout << u << ' ' << v << ' ' << '\n';
}
}
dfs (1, 0);
int q;
cin >> q;
while (q --) {
int u, v;
cin >> u >> v;
//cout << u << ' ' << v << ' ' << LCA (u, v) << '\n';
cout << LCA (u, v) << '\n';
}
return;
}
signed main () {
cin.tie (nullptr) -> sync_with_stdio (false);
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
int t = 1;
//cin >> t;
while (t --) {
solve ();
}
return 0;
}
// thfdgb
컴파일 시 표준 에러 (stderr) 메시지
plan.cpp: In function 'int main()':
plan.cpp:193:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
193 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:194:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
194 | freopen (task".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... |