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 <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <queue>
using namespace std;
const long long oo = 1000000000000000000;
long long int sum, ans = 0, mx = 0, mn = 1000000000, num, pos;
/*
ViHHiPuh
(( `'-""``""-'` ))
)-__-_.._-__-(
/ --- (o _ o) --- \
\ .-* ( .0. ) *-. /
_'-. ,_ '=' _, .-'_
/ `;#'#'# - #'#'#;` \
\_)) -----'#'----- ((_/
# --------- #
'# ------- ------ #'
/..-'# ------- #'-.\
_\...-\'# -- #'/-.../_
((____)- '#' -(____))
cout << fixed << setprecision(6) << x;
freopen ( "sum.in", "r", stdin )
*/
int d[ 100001 ], p[ 100001 ], dp[ 100001 ][ 20 ], col[ 100001 ];
queue <int> q;
set <pair<int, int> > st;
vector <pair<int, int> > v[ 100001 ], vv;
map <pair<int, int>, int> answer, mp;
map <int, int> dd;
int main () {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
int x, y, z;
for ( int i = 1; i <= m; i ++ ) {
cin >> x >> y >> z;
x --;
y --;
v[ x ].push_back( { y, z } );
v[ y ].push_back( { x, z } );
}
for ( int i = 0; i < n; i ++ )
d[ i ] = mn;
int k;
cin >> k;
int a;
for ( int i = 1; i <= k; i ++ ) {
cin >> a;
a --;
q.push( a );
d[ a ] = 0;
}
while ( q.size() ) {
num = q.front();
q.pop();
for ( auto to : v[ num ] ) {
if ( d[ to.first ] > d[ num ] + to.second ) {
d[ to.first ] = d[ num ] + to.second;
q.push( to.first );
}
}
}
int qq;
cin >> qq;
int l[ 100001 ], r[ 100001 ];
for ( int t = 1; t <= qq; t ++ ) {
cin >> l[ t ] >> r[ t ];
l[ t ] --;
r[ t ] --;
mp[ { l[ t ], r[ t ] } ] = mp[ { l[ t ], r[ t ] } ] = 1;
col[ l[ t ] ] ++;
col[ r[ t ] ] ++;
}
for ( int i = 0; i < n; i ++ )
vv.push_back( { col[ i ], i } );
sort( vv.rbegin(), vv.rend() );
for ( auto j : vv ) {
int i = j.second;
dd.clear();
if ( col[ i ] ) {
st.clear();
dd[ i ] = d[ i ];
st.insert( { dd[ i ], i } );
while ( st.size() ) {
num = st.rbegin()->second;
if ( mp[ { i, num } ] ) {
col[ i ] --;
mp[ { i, num } ] = mp[ { num, i } ] = 0;
answer[ { i, num } ] = answer[ { num, i } ] = dd[ num ];
col[ num ] --;
if ( !col[ i ] )
break;
}
st.erase( --st.end() );
for ( auto to : v[ num ] ) {
if ( dd[ to.first ] < min( d[ to.first ], dd[ num ] ) ) {
st.erase( { dd[ to.first ], to.first } );
dd[ to.first ] = min( d[ to.first ], dd[ num ] );
st.insert( { dd[ to.first ], to.first } );
}
}
}
}
}
for ( int i = 1; i <= qq; i ++ )
cout << answer[ { l[ i ], r[ i ] } ] << "\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
*/
# | 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... |