This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ll int
#define F first
#define S second
#define ull unsigned long long
#define db double
#define ldb long double
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define all(x) x.begin(), x.end()
const int mod = 1e9 + 7;
const int N = 1e5 + 7;
using namespace std;
using namespace __gnu_pbds;
ll n, m, k, p[N], a, b, c, ans, d[N], l, r, used[N], res, mx;
set <pair <ll, ll>> s;
vector <pair <ll, ll>> g[N];
void dfs (ll v, ll md){
if (d[v] < md){
return;
}
if (c){
return;
}
if (v == b){
c = 1;
return;
}
used[v] = 1;
for (int i = 0; i < g[v].size(); i++){
if (used[g[v][i].F] == 0 && d[g[v][i].F] >= md){
dfs (g[v][i].F, md);
}
}
}
signed main (){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++){
cin >> a >> b >> c;
mx = max (mx, c);
g[a].pb({b, c});
g[b].pb ({a, c});
}
for (int i = 1; i <= n; i++){
d[i] = 1e9;
}
cin >> k;
for (int i = 1; i <= k; i++){
cin >> p[i];
d[p[i]] = 0;
}
for (int i = 1; i <= n; i++){
s.insert({d[i], i});
}
while (s.size()){
pair <ll, ll> opa = *s.begin();
s.erase (opa);
ll v = opa.second, w = opa.F;
for (int i = 0; i < g[v].size(); i++){
if (d[g[v][i].F] > w + g[v][i].S){
s.erase ({d[g[v][i].F], g[v][i].F});
d[g[v][i].F] = w + g[v][i].S;
s.insert ({d[g[v][i].F], g[v][i].F});
}
}
}
cin >> k;
while (k--){
cin >> a >> b;
l = 0, r = mx * n, res = 0;
while (l <= r){
ll md = (l + r) / 2;
c = 0;
for (int i = 1; i <= n; i++){
used[i] = 0;
}
// cout << md << '\n';
dfs (a, md);
// cout << '\n';
if (c){
l = md + 1;
res = md;
}
else{
r = md - 1;
}
}
cout << res << '\n';
}
}
/*
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
*/
Compilation message (stderr)
plan.cpp: In function 'void dfs(int, int)':
plan.cpp:42:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int i = 0; i < g[v].size(); i++){
| ~~^~~~~~~~~~~~~
plan.cpp: In function 'int main()':
plan.cpp:74:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 0; i < g[v].size(); i++){
| ~~^~~~~~~~~~~~~
# | 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... |