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 <bits/stdc++.h>
# define speed ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
# define int long long
# define fi first
# define se second
using namespace std;
const int N = 2e6 + 7;
const int mod = 1e9 + 7;
vector < pair < int, int > > adj[N];
int dist[N], n, m, go;
bool used[N];
void dfs( int v, int dest, int x ) {
used[v] = 1;
if ( v == dest ) {
go = 1;
return;
}
for ( auto pr: adj[v] ) {
int u = pr.fi;
if ( !used[u] && dist[u] >= x ) {
dfs( u, dest, x );
}
}
}
bool can( int a, int b, int x ) {
go = 0;
for ( int i = 1; i <= n; i++ ) {
used[i] = 0;
}
if ( dist[a] < x ) {
return 0;
}
dfs( a, b, x );
return go;
}
int32_t main() {
speed;
cin >> n >> m;
for ( int i = 1; i <= m; i++ ) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back( { v, w } );
adj[v].push_back( { u, w } );
}
for ( int i = 1; i <= n; i++ ) {
dist[i] = 1e9;
}
int k;
cin >> k;
set < pair < int, int > > q;
for ( int i = 1; i <= k; i++ ) {
int x;
cin >> x;
q.insert( { 0, x } );
dist[x] = 0;
}
while ( !q.empty() ) {
int v = q.begin()->se;
q.erase( q.begin() );
for ( auto pr: adj[v] ) {
int u = pr.fi, w = pr.se;
if ( dist[u] > dist[v] + w ) {
q.erase( { dist[u], u } );
dist[u] = dist[v] + w;
q.insert( { dist[u], u } );
}
}
}
int qr;
cin >> qr;
while ( qr-- ) {
int a, b, ok = 0;
cin >> a >> b;
for ( auto pr: adj[a] ) {
int u = pr.fi;
if (u == b) {
cout << min( dist[u], dist[a] ) << '\n';
ok = 1;
}
}
if ( ok ) {
continue;
}
int l = 0, r = 1000, ans = 0;
while ( l <= r ) {
int mid = (l + r) / 2;
if ( can( a, b, mid ) ) {
ans = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
cout << ans << '\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... |