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>
#include "crocodile.h"
using namespace std;
using ll = long long;
#define MAXN (100005)
vector<pair<ll,ll> > v[MAXN];
priority_queue<pair<ll,ll>,vector<pair<ll,ll> >,greater<pair<ll,ll> > > pq;
ll secondbest[MAXN]; //second best distance
ll visited[MAXN]; //number of times node i is visited
int travel_plan(int N, int M, int R[][2], int L[], int K, int P[]){
for(ll i = 0;i < M;i++){
ll a = R[i][0];
ll b = R[i][1];
ll c = L[i];
v[a].push_back({c,b});
v[b].push_back({c,a});
}
memset(secondbest,-1,sizeof(secondbest));
for(ll i = 0;i < K;i++){
pq.push({0,P[i]});
visited[P[i]] = 2; //we only need to visit exit nodes once anyways so just set it to 2
}
while(!pq.empty()){
ll d = pq.top().first;
ll x = pq.top().second;
pq.pop();
visited[x]++;
if(visited[x] >= 2){ //if visited at least twice, we can choose the second best distance among all the distances
if(secondbest[x] != -1 && d >= secondbest[x]) continue; //worse than pre-existing second best distance so far
if(secondbest[x] == -1){
secondbest[x] = d;
}else{
secondbest[x] = min(secondbest[x],d);
}
}else{
continue;
}
for(auto u : v[x]){
if(secondbest[u.second] == -1 || secondbest[x] + u.first < secondbest[u.second]){
pq.push({secondbest[x] + u.first,u.second});
}
}
}
return secondbest[0];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |