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 pb push_back
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define x first
#define y second
#define nl "\n"
using namespace std;
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair <ll, ll> pii;
const int N = (int)3e5 + 7;
const int M = (int)7e6 + 7;
const ll MOD = (ll)1e9 + 7;
const int inf = (int)1e9 + 7;
const ll INF = (ll)3e18 + 7;
pii dir[] = {{-1, -1}, {1, 1}, {-1, 1}, {1, -1}};
int n, m, k, s, t, ans, Q, d[N], e[N];
vector<pii> g[N];
bool used[N];
void solve() {
cin >> n >> m;
map<pii, bool> was;
for(int i = 1; i <= m; ++i) {
int u, v, w;
cin >> u >> v >> w;
was[{u, v}] = was[{v, u}] = 1;
g[u].pb({v, w});
g[v].pb({u, w});
}
cin >> k;
for(int i = 1; i <= n; ++i) d[i] = inf;
set<pii> q;
for(int i = 1; i <= k; ++i) {
int x;
cin >> x;
d[x] = 0;
q.insert({0, x});
}
while(!q.empty()) {
int v = (*q.begin()).y;
q.erase(q.begin());
for(auto [to, w] : g[v]) {
if(d[v] + w < d[to]) {
q.erase({d[to], to});
d[to] = d[v] + w;
q.insert({d[to], to});
}
}
}
cin >> Q;
while(Q--) {
cin >> s >> t;
if(was[{s, t}]) cout << min(d[s], d[t]) << nl;
else {
fill(e + 1, e + 1 + n, 0);
e[s] = d[s];
priority_queue<pii> o;
o.push({d[s], s});
while(!o.empty()) {
int v = o.top().y, cd = o.top().x;
o.pop();
if(cd < e[v]) continue;
for(auto [to, w] : g[v]) {
if(min(e[v], d[to]) > e[to]) {
e[to] = min(e[v], d[to]);
o.push({e[to], to});
}
}
}
cout << e[t] << nl;
}
}
}
signed main() {
ios_base::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
int test = 1;
//cin >> test;
for(int i = 1; i <= test; ++i) {
//cout << "Case " << i << ": ";
solve();
}
return 0;
}
/*
9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
5
1 6
5 3
4 8
5 8
1 5
*/
# | 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... |