#include <bits/stdc++.h>
using namespace std;
vector <long long> cost (200001,1e18);
vector <long long> secondcost (200001,1e18);
vector <long long> from(200001,1e18);
vector<pair<long long,long long>> adj[200001];
int d(int k,int P[]){
priority_queue<pair<long long,long long>> q1;
for(int i = 0;i<k;i++){
q1.push({0,P[i]});
cost[P[i]]= 0;
secondcost[P[i]]= 0;
}
while(!q1.empty()){
long long co = q1.top().first*-1;
long long no = q1.top().second;
q1.pop();
if (co>secondcost[no]) continue ;
for(int i= 0;i<adj[no].size();i++){
if(secondcost[adj[no][i].first]>(adj[no][i].second+secondcost[no])){
if((adj[no][i].second+secondcost[no])<=cost[adj[no][i].first]){
if(from[adj[no][i].first]!=no){
secondcost[adj[no][i].first] = cost[adj[no][i].first];
}
cost[adj[no][i].first] = (adj[no][i].second+secondcost[no]);
from[adj[no][i].first] = no;
q1.push({secondcost[adj[no][i].first]*-1,adj[no][i].first});
}else if(from[adj[no][i].first]!=no){
secondcost[adj[no][i].first] = (adj[no][i].second+secondcost[no]);
q1.push({secondcost[adj[no][i].first]*-1,adj[no][i].first});
}
}
}
}
return secondcost[0];
}
long long travel_plan(int N,int M,int R[][2],int L[],int K,int P[]){
for(int i = 0;i<M;i++){
adj[R[i][0]].push_back({R[i][1],L[i]});
adj[R[i][1]].push_back({R[i][0],L[i]});
}
return d(K,P);
}
/*
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int N = 5 , M = 7, K = 2;
int R[4][2];
R[0][0] = 0;R[0][1] = 1;
R[1][0] = 0;R[1][1] = 2;
R[2][0] = 3;R[2][1] = 2;
R[3][0] = 2;R[3][1] = 4;
int L[4] = {2,3,1,4};
int P[3] = {1,3,4};
cout<<travel_plan(N,M,R,L,K,P);
}*/
Compilation message
crocodile.cpp: In function 'int d(int, int*)':
crocodile.cpp:20:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for(int i= 0;i<adj[no].size();i++){
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9736 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
9740 KB |
Output is correct |
6 |
Correct |
5 ms |
9728 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
6 ms |
9744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9736 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
9740 KB |
Output is correct |
6 |
Correct |
5 ms |
9728 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
6 ms |
9744 KB |
Output is correct |
9 |
Correct |
7 ms |
10068 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
7 ms |
9856 KB |
Output is correct |
12 |
Correct |
8 ms |
10444 KB |
Output is correct |
13 |
Correct |
8 ms |
10556 KB |
Output is correct |
14 |
Correct |
6 ms |
9784 KB |
Output is correct |
15 |
Correct |
5 ms |
9864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9736 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
9740 KB |
Output is correct |
6 |
Correct |
5 ms |
9728 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
6 ms |
9744 KB |
Output is correct |
9 |
Correct |
7 ms |
10068 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
7 ms |
9856 KB |
Output is correct |
12 |
Correct |
8 ms |
10444 KB |
Output is correct |
13 |
Correct |
8 ms |
10556 KB |
Output is correct |
14 |
Correct |
6 ms |
9784 KB |
Output is correct |
15 |
Correct |
5 ms |
9864 KB |
Output is correct |
16 |
Correct |
504 ms |
91364 KB |
Output is correct |
17 |
Correct |
90 ms |
25288 KB |
Output is correct |
18 |
Correct |
117 ms |
27768 KB |
Output is correct |
19 |
Correct |
589 ms |
98812 KB |
Output is correct |
20 |
Correct |
291 ms |
75132 KB |
Output is correct |
21 |
Correct |
49 ms |
16936 KB |
Output is correct |
22 |
Correct |
311 ms |
71216 KB |
Output is correct |