#include "walk.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
#include <stack>
#include <map>
typedef long long llong;
const int MAXN = 100000 + 10;
const int INTERSECTIONS = 10 + 5;
const llong INF = 1e18;
const int INTINF = 1e9;
namespace
{
int cnt;
int n, m;
int s, t;
int h[MAXN];
int x[MAXN];
int l[MAXN];
int r[MAXN];
int y[MAXN];
std::map <int,int> map[MAXN];
bool vis[MAXN * INTERSECTIONS];
llong dist[MAXN * INTERSECTIONS];
std::vector <std::pair <int,int>> g[MAXN * INTERSECTIONS];
int cntNodes;
}
llong solve()
{
for (int i = 1 ; i <= n ; ++i)
{
map[i][0] = ++cnt;
}
for (int i = 1 ; i <= m ; ++i)
{
int prev = -1;
// std::cout << "building: " << i << ' ' << l[i] << ' ' << r[i] << ' ' << y[i] << '\n';
for (int j = l[i] ; j <= r[i] ; ++j)
{
if (y[i] <= h[j])
{
if (map[j][y[i]] == 0)
{
map[j][y[i]] = ++cnt;
}
if (prev != -1)
{
// std::cout << "edge hor: " << x[prev] << ' ' << y[i] << ' ' << x[j] << ' ' << y[i] << ' ' << map[prev][y[i]] << ' ' << << '\n';
g[map[prev][y[i]]].push_back({map[j][y[i]], x[j] - x[prev]});
g[map[j][y[i]]].push_back({map[prev][y[i]], x[j] - x[prev]});
}
prev = j;
}
}
}
for (int i = 1 ; i <= n ; ++i)
{
auto prev = map[i].begin();
for (auto curr = std::next(map[i].begin()) ; curr != map[i].end() ; ++curr)
{
// std::cout << "edge ver: " << x[i] << ' ' << prev->first << ' ' << x[i] << ' ' << curr->first << '\n';
g[prev->second].push_back({curr->second, curr->first - prev->first});
g[curr->second].push_back({prev->second, curr->first - prev->first});
prev = curr;
}
}
std::priority_queue <std::pair <llong,int>> pq;
std::fill(dist + 1, dist + 1 + cnt, INF);
pq.push({0, map[s][0]});
dist[map[s][0]] = 0;
while (pq.size())
{
auto [d, node] = pq.top();
pq.pop();
if (vis[node])
{
continue;
}
vis[node] = true;
for (const auto &[u, ed] : g[node])
{
if (dist[u] > dist[node] + ed)
{
dist[u] = dist[node] + ed;
pq.push({-dist[u], u});
}
}
}
return (dist[map[t][0]] == INF ? -1 : dist[map[t][0]]);
}
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 t)
{
n = x.size();
m = l.size();
::s = s + 1; ::t = t + 1;
for (int i = 0 ; i < n ; ++i)
{
::h[i + 1] = h[i];
::x[i + 1] = x[i];
}
for (int i = 0 ; i < m ; ++i)
{
::l[i + 1] = l[i] + 1;
::r[i + 1] = r[i] + 1;
::y[i + 1] = y[i];
}
return solve();
}
Compilation message
walk.cpp:31:9: warning: '{anonymous}::cntNodes' defined but not used [-Wunused-variable]
31 | int cntNodes;
| ^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
41816 KB |
Output is correct |
2 |
Correct |
10 ms |
41820 KB |
Output is correct |
3 |
Correct |
11 ms |
41820 KB |
Output is correct |
4 |
Correct |
17 ms |
41820 KB |
Output is correct |
5 |
Correct |
12 ms |
41820 KB |
Output is correct |
6 |
Correct |
11 ms |
41888 KB |
Output is correct |
7 |
Correct |
11 ms |
41760 KB |
Output is correct |
8 |
Correct |
10 ms |
41820 KB |
Output is correct |
9 |
Correct |
11 ms |
41816 KB |
Output is correct |
10 |
Correct |
11 ms |
41820 KB |
Output is correct |
11 |
Correct |
12 ms |
41848 KB |
Output is correct |
12 |
Correct |
12 ms |
41820 KB |
Output is correct |
13 |
Correct |
11 ms |
41820 KB |
Output is correct |
14 |
Correct |
12 ms |
41816 KB |
Output is correct |
15 |
Correct |
11 ms |
41820 KB |
Output is correct |
16 |
Correct |
13 ms |
41820 KB |
Output is correct |
17 |
Correct |
12 ms |
41820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
41820 KB |
Output is correct |
2 |
Correct |
11 ms |
41636 KB |
Output is correct |
3 |
Correct |
602 ms |
137084 KB |
Output is correct |
4 |
Correct |
449 ms |
149332 KB |
Output is correct |
5 |
Correct |
286 ms |
133232 KB |
Output is correct |
6 |
Correct |
593 ms |
123636 KB |
Output is correct |
7 |
Correct |
298 ms |
133508 KB |
Output is correct |
8 |
Correct |
861 ms |
163804 KB |
Output is correct |
9 |
Correct |
382 ms |
132764 KB |
Output is correct |
10 |
Correct |
779 ms |
188156 KB |
Output is correct |
11 |
Correct |
286 ms |
94288 KB |
Output is correct |
12 |
Correct |
156 ms |
78960 KB |
Output is correct |
13 |
Correct |
581 ms |
169888 KB |
Output is correct |
14 |
Correct |
1804 ms |
77128 KB |
Output is correct |
15 |
Correct |
970 ms |
77172 KB |
Output is correct |
16 |
Correct |
240 ms |
78164 KB |
Output is correct |
17 |
Correct |
204 ms |
77324 KB |
Output is correct |
18 |
Execution timed out |
4064 ms |
64660 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
51976 KB |
Output is correct |
2 |
Runtime error |
984 ms |
443724 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
51976 KB |
Output is correct |
2 |
Runtime error |
984 ms |
443724 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
41816 KB |
Output is correct |
2 |
Correct |
10 ms |
41820 KB |
Output is correct |
3 |
Correct |
11 ms |
41820 KB |
Output is correct |
4 |
Correct |
17 ms |
41820 KB |
Output is correct |
5 |
Correct |
12 ms |
41820 KB |
Output is correct |
6 |
Correct |
11 ms |
41888 KB |
Output is correct |
7 |
Correct |
11 ms |
41760 KB |
Output is correct |
8 |
Correct |
10 ms |
41820 KB |
Output is correct |
9 |
Correct |
11 ms |
41816 KB |
Output is correct |
10 |
Correct |
11 ms |
41820 KB |
Output is correct |
11 |
Correct |
12 ms |
41848 KB |
Output is correct |
12 |
Correct |
12 ms |
41820 KB |
Output is correct |
13 |
Correct |
11 ms |
41820 KB |
Output is correct |
14 |
Correct |
12 ms |
41816 KB |
Output is correct |
15 |
Correct |
11 ms |
41820 KB |
Output is correct |
16 |
Correct |
13 ms |
41820 KB |
Output is correct |
17 |
Correct |
12 ms |
41820 KB |
Output is correct |
18 |
Correct |
12 ms |
41820 KB |
Output is correct |
19 |
Correct |
11 ms |
41636 KB |
Output is correct |
20 |
Correct |
602 ms |
137084 KB |
Output is correct |
21 |
Correct |
449 ms |
149332 KB |
Output is correct |
22 |
Correct |
286 ms |
133232 KB |
Output is correct |
23 |
Correct |
593 ms |
123636 KB |
Output is correct |
24 |
Correct |
298 ms |
133508 KB |
Output is correct |
25 |
Correct |
861 ms |
163804 KB |
Output is correct |
26 |
Correct |
382 ms |
132764 KB |
Output is correct |
27 |
Correct |
779 ms |
188156 KB |
Output is correct |
28 |
Correct |
286 ms |
94288 KB |
Output is correct |
29 |
Correct |
156 ms |
78960 KB |
Output is correct |
30 |
Correct |
581 ms |
169888 KB |
Output is correct |
31 |
Correct |
1804 ms |
77128 KB |
Output is correct |
32 |
Correct |
970 ms |
77172 KB |
Output is correct |
33 |
Correct |
240 ms |
78164 KB |
Output is correct |
34 |
Correct |
204 ms |
77324 KB |
Output is correct |
35 |
Execution timed out |
4064 ms |
64660 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |