#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
vector <pair <int, int> > g[maxn];
vector <pair <int,int> > inter[maxn];
int ver;
long long dist[maxn];
bool vis[maxn];
priority_queue <pair <long long, int> > q;
int nextt[maxn];
int prevv[maxn];
struct queries
{
int h;
int l;int r;
bool fl;
};
vector <queries> w;
long long dijkstra(int a, int b)
{
// vis[a] = 1;
for(int i=0;i<ver;i++)
dist[i] = 1e18;
dist[a] = 0;
q.push({(long long)0, a});
while(!q.empty())
{
int v = q.top().second;
if(vis[v]||dist[v]!=-q.top().first){q.pop();continue;}
q.pop();
//cout<<v<<"->"<<dist[v]<<endl;
vis[v] =1;
if(v==b)return dist[v];
for(auto u:g[v])
{
//cout<<u.first<<" "<<dist[v]+u.second<<endl;
if(vis[u.first])continue;
if(dist[u.first]>dist[v]+u.second)
{
dist[u.first]=dist[v]+u.second;
q.push({-dist[u.first],u.first});
}
}
}
return -1;
}
bool cmp(queries i, queries j)
{
if(i.h==j.h)return i.fl>j.fl;
return i.h<j.h;
}
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) {
ver = x.size();
int i,j;
for(i=0;i<x.size();i++)
{
inter[i].push_back({0,i});
// inter[i].push_back({h[i],i});
}
//inter[s].push_back({0,s});
//inter[_g].push_back({0,_g});
/* for(i=0;i<l.size();i++)
{int prev = -1,id;
for(j=l[i];j<=r[i];j++)
{
if(y[i]>h[j])continue;
inter[j].push_back({y[i],ver});
if(prev!=-1)
{
g[ver].push_back({prev,x[j]-x[id]});
g[prev].push_back({ver,x[j]-x[id]});
}
prev = ver;
id = j;
ver++;
}
}*/
for(i=0;i<x.size();i++)
{
nextt[i] = i+1;
prevv[i] = i-1;
}
prevv[0] = -1;
nextt[x.size()-1] = -1;
for(i=0;i<x.size();i++)
{
queries A;
A.h = h[i];
A.l = i;
A.fl = false;
w.push_back(A);
}
for(i=0;i<l.size();i++)
{
queries A;
A.h = y[i];
A.l = l[i];
A.r = r[i];
A.fl = true;
w.push_back(A);
}
sort(w.begin(),w.end(),cmp);
int n = x.size();
for(j=0;j<w.size();j++)
{
queries A = w[j];
//cout<<A.h<<" "<<A.l<<" "<<A.r<<" "<<A.fl<<endl;
if(!A.fl)
{
if(prevv[A.l]!=-1)
nextt[prevv[A.l]] = nextt[A.l];
if(nextt[A.l]!=-1)
prevv[nextt[A.l]] = prevv[A.l];
}
else
{
int prev = -1,id;
for(i = A.l; i!=-1&&i<=A.r; i = nextt[i])
{
inter[i].push_back({A.h,ver});
if(prev!=-1)
{
// cout << "ADD edge"<<" "<<prev<<" - "<<ver<<" "<<x[i]-x[id]<<endl;
g[prev].push_back({ver,x[i]-x[id]});
g[ver].push_back({prev,x[i]-x[id]});
}
prev= ver;
id = i;
ver++;
}
}
}
for(i=0;i<x.size();i++)
{
sort(inter[i].begin(),inter[i].end());
for(j=0;j<inter[i].size()-1;j++)
{
//cout<<i<<" :: "<<inter[i][j].first<<" "<<inter[i][j+1].first<<endl;
g[inter[i][j].second].push_back({inter[i][j+1].second,inter[i][j+1].first-inter[i][j].first});
g[inter[i][j+1].second].push_back({inter[i][j].second,inter[i][j+1].first-inter[i][j].first});
}
}
//cout<<"OK"<<endl;
return dijkstra(s,_g);
}
Compilation message
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:56:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(i=0;i<x.size();i++)
| ~^~~~~~~~~
walk.cpp:80:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(i=0;i<x.size();i++)
| ~^~~~~~~~~
walk.cpp:87:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for(i=0;i<x.size();i++)
| ~^~~~~~~~~
walk.cpp:95:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(i=0;i<l.size();i++)
| ~^~~~~~~~~
walk.cpp:106:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<queries>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for(j=0;j<w.size();j++)
| ~^~~~~~~~~
walk.cpp:135:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
135 | for(i=0;i<x.size();i++)
| ~^~~~~~~~~
walk.cpp:138:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | for(j=0;j<inter[i].size()-1;j++)
| ~^~~~~~~~~~~~~~~~~~
walk.cpp:105:9: warning: unused variable 'n' [-Wunused-variable]
105 | int n = x.size();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
5 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Runtime error |
80 ms |
26856 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
48 ms |
23620 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
48 ms |
23620 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
5 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
5068 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Runtime error |
80 ms |
26856 KB |
Execution killed with signal 11 |
21 |
Halted |
0 ms |
0 KB |
- |