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>
using namespace std;
typedef long long ll;
const ll Size=1e5+1;
vector<pair<ll,ll>> adj[Size];
ll safety[Size],dist[Size];
bool visited[Size];
queue<ll> to_process;
ll seg_tree[8*Size];
ll n,m;
void calc_danger(){
set<pair<ll,ll>> to_process;
for(ll i=1;i<=n;i++){
to_process.insert({safety[i],i});
}
while(to_process.size()){
pair<ll,ll> curr_node=*to_process.begin();
to_process.erase(curr_node);
while(to_process.size() and visited[curr_node.second]){
curr_node=*to_process.begin();
to_process.erase(curr_node);
}
visited[curr_node.second]=1;
for(auto itr:adj[curr_node.second]){
if(safety[itr.first]>safety[curr_node.second]+itr.second){
to_process.erase({safety[itr.first],itr.first});
safety[itr.first]=safety[curr_node.second]+itr.second;
to_process.insert({safety[itr.first],itr.first});
}
}
}
}
ll dijkstra(ll start_node,ll end_node){
set<pair<ll,ll>> to_process;
for(ll i=1;i<=n;i++){
if(i!=start_node){
dist[i]=0;
}else{
dist[i]=safety[i];
}
to_process.insert({dist[i],i});
visited[i]=0;
}
while(to_process.size()){
pair<ll,ll> curr_node=*to_process.rbegin();
to_process.erase(curr_node);
while(to_process.size() and visited[curr_node.second]){
curr_node=*to_process.rbegin();
to_process.erase(curr_node);
}
visited[curr_node.second]=1;
for(auto itr:adj[curr_node.second]){
if(dist[itr.first]<min(dist[curr_node.second],itr.second)){
to_process.erase({dist[itr.first],itr.first});
dist[itr.first]=min(dist[curr_node.second],itr.second);
to_process.insert({dist[itr.first],itr.first});
}
}
}
return dist[end_node];
}
void solve(){
cin>>n>>m;
for(ll i=0;i<=n;i++){
safety[i]=1e18;
}
for(ll i=0;i<m;i++){
ll from,to,w;
cin>>from>>to>>w;
adj[from].push_back({to,w});
adj[to].push_back({from,w});
}
ll k;
cin>>k;
for(ll i=0;i<k;i++){
ll v;
cin>>v;
safety[v]=0;
}
calc_danger();
for(ll i=1;i<=n;i++){
for(ll j=0;j<adj[i].size();j++){
adj[i][j]={adj[i][j].first,safety[adj[i][j].first]};
}
}
ll q;
cin>>q;
if(q>1 and n>1e3){
while(q--){
ll from,to;
cin>>from>>to;
ll ans=min(safety[from],safety[to]);
cout<<ans<<"\n";
}
}else{
while(q--){
ll from,to;
cin>>from>>to;
ll ans=dijkstra(from,to);
cout<<ans<<"\n";
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t=1;
while(t--){
solve();
}
return 0;
}
/*
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 solve()':
plan.cpp:89:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for(ll j=0;j<adj[i].size();j++){
| ~^~~~~~~~~~~~~~
# | 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... |