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>
#define int long long
using namespace std;
const long long oo = 1000000000000000000;
long long sum, ans = 0, mx = 0, mn = 1000000000, num, pos;
/*
ViHHiPuh
(( `'-""``""-'` ))
)-__-_.._-__-(
/ --- (o _ o) --- \
\ .-* ( .0. ) *-. /
_'-. ,_ '=' _, .-'_
/ `;#'#'# - #'#'#;` \
\_)) -----'#'----- ((_/
# --------- #
'# ------- ------ #'
/..-'# ------- #'-.\
_\...-\'# -- #'/-.../_
((____)- '#' -(____))
cout << fixed << setprecision(6) << x;
freopen ( "sum.in", "r", stdin )
*/
queue <int> q;
int d[ 100001 ], p[ 100001 ], r[ 100001 ], tin[ 100001 ], tout[ 100001 ], tmr;
vector <pair<int, int> > v[ 100001 ];
vector <pair<int, int> > v1[ 100001 ];
vector <pair<int, pair<int, int> > > vv;
int lg = 20;
int par[ 100001 ][ 21 ], mnn[ 100001 ][ 21 ];
int fin( int a ) {
if ( p[ a ] == a )
return a;
return p[ a ] = fin( p[ a ] );
}
void unin( int a, int b ) {
a = fin( a );
b = fin( b );
if ( a == b )
return;
if ( r[ a ] < r[ b ] )
swap( a, b );
r[ a ] += r[ b ];
p[ b ] = a;
}
void dfs( int u, int pr = 1 ) {
par[ u ][ 0 ] = pr;
tin[ u ] = ++tmr;
for ( int i = 1; i <= lg; i ++ ) {
par[ u ][ i ] = par[ par[ u ][ i - 1 ] ][ i - 1 ];
mnn[ u ][ i ] = min( mnn[ u ][ i - 1 ], mnn[ par[ u ][ i - 1 ] ][ i - 1 ] );
}
for ( auto to : v1[ u ] ) {
if ( to.first != pr ) {
mnn[ to.first ][ 0 ] = to.second;
dfs( to.first, u );
}
}
tout[ u ] = tmr;
}
bool ok( int a, int b ) {
return ( tin[ a ] <= tin[ b ] && tout[ a ] >= tout[ b ] );
}
int lca( int a, int b ) {
if ( ok( a, b ) )
return a;
if ( ok( b, a ) )
return b;
for ( int i = lg; i >= 0; i -- ) {
if ( !ok( par[ a ][ i ], b ) )
a = par[ a ][ i ];
}
return par[ a ][ 0 ];
}
int miin( int a, int b ) {
sum = d[ b ];
if ( a == b )
return sum;
for ( int i = lg; i >= 0; i -- ) {
if ( !ok( par[ a ][ i ], b ) ) {
sum = min( sum, mnn[ a ][ i ] + 0ll);
a = par[ a ][ i ];
}
}
sum = min( sum, mnn[ a ][ 0 ] + 0ll);
return sum;
}
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;
v[ x ].push_back( { y, z } );
v[ y ].push_back( { x, z } );
}
for ( int i = 1; i <= n; i ++ ) {
r[ i ] = 1;
p[ i ] = i;
d[ i ] = oo;
}
int k;
cin >> k;
for ( int i = 1; i <= k; i ++ ) {
cin >> num;
q.push( num );
d[ num ] = 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 );
}
}
}
for ( int i = 1; i <= n; i ++ ) {
for ( int to = 0; to < v[ i ].size(); to ++ ) {
v[ i ][ to ].second = min( d[ i ], d[ v[ i ][ to ].first ] );
vv.push_back( { v[ i ][ to ].second, make_pair( i, v[ i ][ to ].first ) } );
}
}
sort( vv.rbegin(), vv.rend() );
for ( auto i : vv ) {
if ( fin( i.second.first ) != fin( i.second.second ) ) {
unin( i.second.second, i.second.first );
v1[ i.second.second ].push_back( { i.second.first, min( d[ i.second.first ], d[ i.second.second ] ) } );
v1[ i.second.first ].push_back( { i.second.second, min( d[ i.second.first ], d[ i.second.second ] ) } );
}
}
dfs( 1 );
int qq;
cin >> qq;
int a[ qq + 1 ], b[ qq + 1 ];
for ( int i = 1; i <= qq; i ++ ) {
cin >> a[ i ] >> b[ i ];
num = lca( a[ i ], b[ i ] );
cout << min( miin( a[ i ], num ), miin( b[ i ], num ) ) << "\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:115:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
115 | main () {
| ^~~~
plan.cpp: In function 'int main()':
plan.cpp:149:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
149 | for ( int to = 0; to < v[ i ].size(); to ++ ) {
| ~~~^~~~~~~~~~~~~~~
# | 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... |