#include <bits/stdc++.h>
#include "walk.h"
#define msize 1187500
#define MAX 9223372036854775806
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ii> vi;
ll dist[msize];
ll isq[msize];
vector<vi> G;
void dijktra(ll u){
memset(isq,0,sizeof isq);
for(int i=0;i<=msize;i++){
dist[i]=MAX;
}
priority_queue<ii,vi,greater<ii> > pq;
dist[u]=0;
isq[u]=1;
pq.push(ii(0,u));
while(!pq.empty()){
ll x=pq.top().second;
ll di=pq.top().first;
pq.pop();
isq[x]=0;
// if(di>dist[x]) continue;
for(auto &v:G[x]){
ll w=v.first;
if(dist[v.second]>dist[x]+w){
dist[v.second]=dist[x]+w;
if(isq[v.second]==0){
pq.push(ii(dist[v.second],v.second));
isq[v.second]=1;
}
}
}
}
}
long long min_distance(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g) {
ll no;
no=0;
map<ii,ll> m;
G.resize(msize);
vi aux;
for(int i=0;i<y.size();i++){
ll ant=-1,id;
for(int j=l[i];j<=r[i];j++){
if(h[j]>=y[i]){
if(m[ii(x[j],y[i])]==0){
no++;
m[ii(x[j],y[i])]=no;
}
aux.push_back(ii(x[j],y[i]));
if(ant==-1){
ant=m[ii(x[j],y[i])];
id=j;
}
else{
ll dis=abs(x[j]-x[id]);
ll u=m[ii(x[j],y[i])];
G[ant].push_back(ii(dis,u));
G[u].push_back(ii(dis,ant));
ant=u;
id=j;
}
}
}
}
if(m[ii(x[s],0)]==0){
no++;
m[ii(x[s],0)]=no;
aux.push_back(ii(x[s],0));
}
if(m[ii(x[g],0)]==0){
no++;
m[ii(x[g],0)]=no;
aux.push_back(ii(x[g],0));
}
sort(aux.begin(),aux.end());
for(int i=1;i<aux.size();i++){
if(aux[i].first==aux[i-1].first){
int u=m[ii(aux[i].first,aux[i].second)];
int v=m[ii(aux[i-1].first,aux[i-1].second)];
int w=abs(aux[i].second-aux[i-1].second);
G[u].push_back(ii(w,v));
G[v].push_back(ii(w,u));
}
}
dijktra(m[ii(x[s],0)]);
if(dist[m[ii(x[g],0)]]==MAX) dist[m[ii(x[g],0)]]=-1;
ll res=dist[m[ii(x[g],0)]];
return res;
}
Compilation message
walk.cpp: In function 'void dijktra(ll)':
walk.cpp:23:8: warning: unused variable 'di' [-Wunused-variable]
23 | ll di=pq.top().first;
| ^~
walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:45:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i=0;i<y.size();i++){
| ~^~~~~~~~~
walk.cpp:80:16: 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]
80 | for(int i=1;i<aux.size();i++){
| ~^~~~~~~~~~~
walk.cpp: In function 'void dijktra(ll)':
walk.cpp:15:12: warning: iteration 1187500 invokes undefined behavior [-Waggressive-loop-optimizations]
15 | dist[i]=MAX;
| ^
walk.cpp:14:16: note: within this loop
14 | for(int i=0;i<=msize;i++){
| ^
walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:59:24: warning: 'id' may be used uninitialized in this function [-Wmaybe-uninitialized]
59 | ll dis=abs(x[j]-x[id]);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
46848 KB |
Output is correct |
2 |
Correct |
30 ms |
46848 KB |
Output is correct |
3 |
Correct |
30 ms |
46848 KB |
Output is correct |
4 |
Correct |
30 ms |
46840 KB |
Output is correct |
5 |
Correct |
30 ms |
46976 KB |
Output is correct |
6 |
Correct |
30 ms |
46848 KB |
Output is correct |
7 |
Correct |
30 ms |
46848 KB |
Output is correct |
8 |
Correct |
30 ms |
46896 KB |
Output is correct |
9 |
Correct |
30 ms |
46840 KB |
Output is correct |
10 |
Correct |
30 ms |
46968 KB |
Output is correct |
11 |
Correct |
30 ms |
46848 KB |
Output is correct |
12 |
Correct |
30 ms |
46844 KB |
Output is correct |
13 |
Correct |
30 ms |
46840 KB |
Output is correct |
14 |
Correct |
31 ms |
46848 KB |
Output is correct |
15 |
Correct |
30 ms |
46848 KB |
Output is correct |
16 |
Correct |
30 ms |
46840 KB |
Output is correct |
17 |
Correct |
31 ms |
46976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
46848 KB |
Output is correct |
2 |
Correct |
29 ms |
46848 KB |
Output is correct |
3 |
Correct |
1553 ms |
188284 KB |
Output is correct |
4 |
Correct |
1340 ms |
185108 KB |
Output is correct |
5 |
Correct |
998 ms |
165536 KB |
Output is correct |
6 |
Correct |
1563 ms |
148428 KB |
Output is correct |
7 |
Correct |
972 ms |
165572 KB |
Output is correct |
8 |
Correct |
2023 ms |
229368 KB |
Output is correct |
9 |
Correct |
1108 ms |
164872 KB |
Output is correct |
10 |
Correct |
1907 ms |
247812 KB |
Output is correct |
11 |
Correct |
771 ms |
114136 KB |
Output is correct |
12 |
Correct |
431 ms |
77316 KB |
Output is correct |
13 |
Correct |
1616 ms |
219664 KB |
Output is correct |
14 |
Correct |
3792 ms |
76600 KB |
Output is correct |
15 |
Correct |
2166 ms |
77292 KB |
Output is correct |
16 |
Correct |
567 ms |
80488 KB |
Output is correct |
17 |
Correct |
530 ms |
79456 KB |
Output is correct |
18 |
Execution timed out |
4077 ms |
38608 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
209 ms |
62824 KB |
Output is correct |
2 |
Runtime error |
1328 ms |
361816 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
209 ms |
62824 KB |
Output is correct |
2 |
Runtime error |
1328 ms |
361816 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
46848 KB |
Output is correct |
2 |
Correct |
30 ms |
46848 KB |
Output is correct |
3 |
Correct |
30 ms |
46848 KB |
Output is correct |
4 |
Correct |
30 ms |
46840 KB |
Output is correct |
5 |
Correct |
30 ms |
46976 KB |
Output is correct |
6 |
Correct |
30 ms |
46848 KB |
Output is correct |
7 |
Correct |
30 ms |
46848 KB |
Output is correct |
8 |
Correct |
30 ms |
46896 KB |
Output is correct |
9 |
Correct |
30 ms |
46840 KB |
Output is correct |
10 |
Correct |
30 ms |
46968 KB |
Output is correct |
11 |
Correct |
30 ms |
46848 KB |
Output is correct |
12 |
Correct |
30 ms |
46844 KB |
Output is correct |
13 |
Correct |
30 ms |
46840 KB |
Output is correct |
14 |
Correct |
31 ms |
46848 KB |
Output is correct |
15 |
Correct |
30 ms |
46848 KB |
Output is correct |
16 |
Correct |
30 ms |
46840 KB |
Output is correct |
17 |
Correct |
31 ms |
46976 KB |
Output is correct |
18 |
Correct |
30 ms |
46848 KB |
Output is correct |
19 |
Correct |
29 ms |
46848 KB |
Output is correct |
20 |
Correct |
1553 ms |
188284 KB |
Output is correct |
21 |
Correct |
1340 ms |
185108 KB |
Output is correct |
22 |
Correct |
998 ms |
165536 KB |
Output is correct |
23 |
Correct |
1563 ms |
148428 KB |
Output is correct |
24 |
Correct |
972 ms |
165572 KB |
Output is correct |
25 |
Correct |
2023 ms |
229368 KB |
Output is correct |
26 |
Correct |
1108 ms |
164872 KB |
Output is correct |
27 |
Correct |
1907 ms |
247812 KB |
Output is correct |
28 |
Correct |
771 ms |
114136 KB |
Output is correct |
29 |
Correct |
431 ms |
77316 KB |
Output is correct |
30 |
Correct |
1616 ms |
219664 KB |
Output is correct |
31 |
Correct |
3792 ms |
76600 KB |
Output is correct |
32 |
Correct |
2166 ms |
77292 KB |
Output is correct |
33 |
Correct |
567 ms |
80488 KB |
Output is correct |
34 |
Correct |
530 ms |
79456 KB |
Output is correct |
35 |
Execution timed out |
4077 ms |
38608 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |