이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
freopen ( "sum.in", "r", stdin )
*/
int d[ 100001 ], dd[ 100001 ], p[ 100001 ];
queue <int> q;
vector <pair<int, int> > v[ 100001 ];
map <pair<int, int>, bool> mp;
int main () {
int n, m;
cin >> n >> m;
int x, y, z;
for ( int i = 1; i <= m; i ++ ) {
cin >> x >> y >> z;
mp[ { x, y } ] = mp[ { y, x } ] = 1;
v[ x ].push_back( { y, z } );
v[ y ].push_back( { x, z } );
}
for ( int i = 1; i <= n; i ++ )
d[ i ] = mn;
int k;
cin >> k;
int a;
for ( int i = 1; i <= k; i ++ ) {
cin >> 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, r;
while ( qq -- ) {
cin >> l >> r;
if ( mp[ { l, r } ] )
cout << min( d[ l ], d[ r ] ) << "\n";
else {
for ( int i = 1; i <= n; i ++ )
dd[ i ] = 0;
dd[ l ] = d[ l ];
q.push( l );
p[ l ] = -1;
while ( q.size() ) {
num = q.front();
q.pop();
for ( auto to : v[ num ] ) {
if ( dd[ to.first ] <= min( dd[ num ], d[ to.first ] ) ) {
q.push( to.first );
dd[ to.first ] = min( dd[ num ], d[ to.first ] );
p[ to.first ] = num;
}
}
}
num = r;
mx = oo;
for ( int i = num; i != -1; i = p[ i ] ) {
mx = min( mx, dd[ i ] + 0ll );
}
cout << mx << "\n";
}
}
}
# | 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... |