#include<bits/stdc++.h>
//#include "crocodile.h"
#include<ext/pb_ds/assoc_container.hpp>
/**zagaro & lauren <3**/
#define mod 1000000007 //1e9 + 7
#define pi acos(-1)
#define wl while
#define str string
#define ENDL "\n"
#define sal ' '
#define tp_set ll
#define prc(n) cout.precision(n);cout<<fixed;
#define ord_set tree<tp_set, null_type, less<tp_set>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef bool bl;
typedef char car;
using namespace std;
using namespace __gnu_pbds;
vector<vector<ll> > edj;
vector<ll> dis;
ll dfs(ll nod, ll par){
ll a=LONG_LONG_MAX, b=LONG_LONG_MAX, x;
if(edj[nod].size() == 1)return dis[nod];
for(int i=0;i<edj[nod].size();i++){
if(edj[nod][i] != par){
x = dfs(edj[nod][i], nod);
if(x < a){
b = a;
a = x;
}
else if(x < b)b = x;
}
}
return b;
}
int travel_plan(int n, int m, int R[][2], int L[], int k, int P[]){
ll nod, val, par;
vector<vector<pair<ll,ll> > > adj(n+1);
edj.assign(n+1, vector<ll> (0, 0));
dis.assign(n+1, LONG_LONG_MAX);
vector<bl> vis(n+1, false);
vector<bl> ext(n+1, false);
vector<ll> vec(n+1, 0);
for(int i=0;i<m;i++){
adj[R[i][0]+1].push_back({R[i][1]+1, L[i]});
adj[R[i][1]+1].push_back({R[i][0]+1, L[i]});
vec[R[i][0]+1]++;
vec[R[i][1]+1]++;
}
for(int i=0;i<k;i++){ext[P[i]+1] = true;}
priority_queue<tuple<ll,ll,ll>, vector<tuple<ll,ll,ll> >, greater<tuple<ll,ll,ll> > > pq;
pq.push({0, 1, 0});
wl(!pq.empty()){
tie(val, nod, par) = pq.top();
pq.pop();
if(val < dis[nod]){
edj[nod].push_back(par);
edj[par].push_back(nod);
dis[nod] = val;
if(!ext[nod]){
dis[nod] = val;
for(int i=0;i<adj[nod].size();i++){
if(vec[adj[nod][i].first] != 2){
pq.push({val+adj[nod][i].second, adj[nod][i].first, nod});
}
else if(ext[adj[nod][i].first])pq.push({val+adj[nod][i].second, adj[nod][i].first, nod});
}
}
}
else if(ext[nod]){
n++;
edj.push_back(vector<ll> (0, 0));
edj[n].push_back(par);
edj[par].push_back(n);
dis.push_back(val);
}
}
return dfs(1, 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... |