#include <bits/stdc++.h>
#include "crocodile.h"
using namespace std;
int travel_plan(int n, int m, int edges[][2], int w[], int k, int p[])
{
map<int,int> g[n];
map<int,int>index[n];
for(int i =0;i<m;i++){
g[edges[i][0]][edges[i][1]]=w[i];
g[edges[i][1]][edges[i][0]]=w[i];
index[edges[i][0]][edges[i][1]]=i;
index[edges[i][1]][edges[i][0]]=i;
}
bool ex[n];
bool rem[n];
bool vis[n];
fill(rem,rem+n,0);
fill(ex,ex+n,0);
fill(vis,vis+n,0);
set<array<int,3>>pq; // dist,destination,origin
map<int,int>mp;
for(int i = 0;i<k;i++){
vis[p[i]]=1;
ex[p[i]]=1;
}
for(int i = 0;i<k;i++){
for(pair<int,int>pa : g[p[i]]){
if(vis[pa.first]) continue;
pq.insert({pa.second,pa.first,p[i]});
}
}
while(!pq.empty()){
array<int,3> a = *(pq.begin());
pq.erase(pq.begin());
if(vis[a[1]]){
continue;
}
if(rem[a[1]]){
for(pair<int,int>p:g[a[1]]){
if(vis[p.first]) continue;
if(mp[index[a[1]][p.first]]!=0&&mp[index[a[1]][p.first]]>a[0]+p.second){
pq.erase({mp[index[a[1]][p.first]],p.first,a[1]});
}
pq.insert({a[0]+p.second,p.first,a[1]});
mp[index[a[1]][p.first]]=a[0]+p.second;
}
vis[a[1]]=1;
}
else{
//cout << "removing edge: " << a[1] << " " << a[2] << "\n";
g[a[1]].erase(a[2]);
rem[a[1]]=1;
}
}
fill(vis,vis+n,0);
pq.insert({0,0,-1});
vis[0]=1;
while(!pq.empty()){
array<int,3> a = *(pq.begin());
pq.erase(pq.begin());
vis[a[1]]=1;
if(ex[a[1]]) return a[0];
for(pair<int,int>p:g[a[1]]){
if(vis[p.first]) continue;
pq.insert({p.second+a[0],p.first,a[1]});
}
}
return -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
608 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
608 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
6 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
352 KB |
Output is correct |
11 |
Correct |
4 ms |
1116 KB |
Output is correct |
12 |
Correct |
30 ms |
4068 KB |
Output is correct |
13 |
Correct |
588 ms |
24960 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
19 ms |
2484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
608 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
6 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
352 KB |
Output is correct |
11 |
Correct |
4 ms |
1116 KB |
Output is correct |
12 |
Correct |
30 ms |
4068 KB |
Output is correct |
13 |
Correct |
588 ms |
24960 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
19 ms |
2484 KB |
Output is correct |
16 |
Execution timed out |
2071 ms |
249608 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |