#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
void solve()
{
ll n, m;
cin >> n >> m;
ll b[m], p[m];
vector<ll> eq[n];
for (ll i = 0; i < m; i++)
cin >> b[i] >> p[i], eq[b[i]].push_back(i);
vector<ll> g[m];
for (ll i = 0; i < n; i++)
for (ll j = 1; j < eq[i].size(); j++)
g[eq[i][j]].push_back(eq[i][0]), g[eq[i][0]].push_back(eq[i][j]);
map<ll, ll> dist[m];
dist[0][b[0]] = 0;
deque<pair<ll, ll>> q;
q.push_front(make_pair(0, b[0]));
while (!q.empty())
{
auto [i, j] = q.front();
q.pop_front();
if (j - p[i] >= 0 and (!dist[i].count(j - p[i]) or dist[i][j - p[i]] > dist[i][j] + 1))
dist[i][j - p[i]] = dist[i][j] + 1, q.push_back(make_pair(i, j - p[i]));
if (j + p[i] < n and (!dist[i].count(j + p[i]) or dist[i][j + p[i]] > dist[i][j] + 1))
dist[i][j + p[i]] = dist[i][j] + 1, q.push_back(make_pair(i, j + p[i]));
// if (!eq[j].empty())
// if (!dist[eq[j][0]].count(j) or dist[eq[j][0]][j] > dist[i][j])
// dist[eq[j][0]][j] = dist[i][j], q.push_front(make_pair(eq[j][0], j));
// for (ll k : g[i])
// if (!dist[k].count(j) or dist[k][j] > dist[i][j])
// dist[k][j] = dist[i][j], q.push_front(make_pair(k, j));
for (ll k : eq[j])
if (!dist[k].count(j) or dist[k][j] > dist[i][j])
dist[k][j] = dist[i][j], q.push_front(make_pair(k, j));
}
ll best = 1e18;
for (auto i : dist[1])
best = min(best, i.second);
if (best == 1e18)
best = -1;
cout << best << endl;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll t = 1;
// precomp();
// cin >> t;
for (ll i = 1; i <= t; i++)
solve();
cerr << "\nTime elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
Compilation message
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:16:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for (ll j = 1; j < eq[i].size(); j++)
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
552 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
5 ms |
1316 KB |
Output is correct |
12 |
Correct |
51 ms |
12884 KB |
Output is correct |
13 |
Correct |
57 ms |
13140 KB |
Output is correct |
14 |
Correct |
2 ms |
1112 KB |
Output is correct |
15 |
Correct |
2 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
6 ms |
1368 KB |
Output is correct |
12 |
Correct |
53 ms |
12884 KB |
Output is correct |
13 |
Correct |
57 ms |
13136 KB |
Output is correct |
14 |
Correct |
2 ms |
1112 KB |
Output is correct |
15 |
Correct |
2 ms |
1112 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
6 ms |
2140 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Execution timed out |
1102 ms |
137924 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
6 ms |
1368 KB |
Output is correct |
12 |
Correct |
50 ms |
12884 KB |
Output is correct |
13 |
Correct |
61 ms |
13108 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
2 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
6 ms |
2148 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
360 KB |
Output is correct |
20 |
Execution timed out |
1103 ms |
138064 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
5 ms |
1228 KB |
Output is correct |
12 |
Correct |
51 ms |
12880 KB |
Output is correct |
13 |
Correct |
57 ms |
13044 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
2 ms |
976 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
7 ms |
2140 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Execution timed out |
1062 ms |
131456 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |