//In The Name Of ALLAH!
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define ld long double
#define endl '\n'
#define pb push_back
#define pf push_front
#define Zemur007 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define bpc __builtin_popcountll
#define btz __builtin_ctzll
#define all(x) x.begin() , x.end()
#define allr(x) x.rbegin() , x.rend()
#define F first
#define S second
#define pll pair<ll , ll>
#define turtle tuple<ll , ll , ll>
#define pss pair<string , string>
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define indexed_set tree<pll , null_type , less<pll> , rb_tree_tag , tree_order_statistics_node_update>
const ll sz = 1e6 + 5 , INF = 1e18 , MOD = 1e9 + 7;
vector<pll> g[sz];
ll dis[sz];
ll mask , i , j , k;
void solve(){
ll n , m; cin >> n >> m;
for(i = 1 ; i <= n ; i++) dis[i] = INF;
priority_queue<pll , vector<pll> , greater<pll>> pq;
for(i = 1 ; i <= m ; i++){
ll u , v , w; cin >> u >> v >> w;
g[u].pb({v , w});
g[v].pb({u , w});
}
ll K , Q; cin >> K;
for(i = 1 ; i <= K ; i++){
ll x; cin >> x;
dis[x] = 0;
pq.push({0 , x});
}
while(!pq.empty()){
auto [d , cur] = pq.top(); pq.pop();
if(dis[cur] != d) continue;
for(auto [to , w] : g[cur]){
if(dis[to] > dis[cur] + w){
dis[to] = dis[cur] + w;
pq.push({dis[to] , to});
}
}
}
cin >> Q;
for(i = 1 ; i <= Q ; i++){
ll u , v; cin >> u >> v;
cout << min(dis[u] , dis[v]) << endl;
}
}
signed main(){
Zemur007;
//open;
ll t = 1;
// cin >> t;
while(t--){
solve();
}
// for(ll testcase = 1 ; testcase <= t ; testcase++){
// cout << "Case " << testcase << ":" << endl;
// solve();
// }
}
| # | 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... |