#include <bits/stdc++.h>
#define int long long
#define vi vector<int>
#define ii pair<int, int>
#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define P 31
#define mod 1'000'000'007
#define inf 1'000'000'000'000
#define pb push_back
#define str string
#define sz(x) (x).size()
#define vvi vector<vi>
#define fun function
#define oopt cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false);
#define file freopen("problemname.in", "r", stdin); freopen("pr.out", "w", stdout);
#define dbg(v) cout << "Line(" << __LINE__ << ") -> " << #v << " = " << (v) << endl;
using namespace std;
template <class T, int SZ> using arr = array<T, SZ>;
int b[30001];
int power[30001];
set<int> pravi[30001];
set<ii> graf[30001];
int ob[30001];
int obp[201][30001];
void solve()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < 30001; i++)
{
ob[i] = inf;
for (int j = 0; j < 201; j++)
obp[j][i] = inf;
}
for (int i = 0; i < m; i++)
{
cin >> b[i] >> power[i];
if (power[i] > 200)
{
for (int j = 1; j*j <= n; j++)
{
if (b[i]+power[i]*j < n)
graf[b[i]].insert({b[i]+power[i]*j, j});
if (b[i]-power[i]*j >= 0)
graf[b[i]].insert({b[i]-power[i]*j, j});
}
}
else
{
pravi[b[i]].insert(power[i]);
}
}
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq;
if (power[0] > 200)
{
pq.push({0, b[0], -1});
ob[b[0]] = 0;
}
else
{
pq.push({0, b[0], power[0]});
obp[b[0]][power[0]] = 0;
}
while (pq.size())
{
auto [d, pos, pow] = pq.top();
pq.pop();
if (pos == b[1])
{
cout << d << "\n";
return;
}
if (pow > 200 || pow == -1)
{
if (ob[pos] < d) continue;
for (auto [p, w] : graf[pos])
{
if (ob[p] > d+w)
{
ob[p] = d+w;
pq.push({d+w, p, -1});
}
}
for (int p : pravi[pos])
{
if (obp[pos][p] > d)
{
obp[pos][p] = d;
pq.push({d, pos, p});
}
}
}
else
{
if (obp[pow][pos] < d) continue;
if (pos-pow >= 0 && obp[pow][pos-pow] > d+1)
{
obp[pow][pos-pow] = d+1;
pq.push({d+1, pos-pow, pow});
}
if (pos+pow < n && obp[pow][pos+pow] > d+1)
{
obp[pow][pos+pow] = d+1;
pq.push({d+1, pos+pow, pow});
}
if (ob[pos] == 0)
{
ob[pos] = d;
pq.push({d, pos, -1});
}
for (int p : pravi[pos])
{
if (obp[p][pos] > d)
{
obp[p][pos] = d;
pq.push({d, pos, p});
}
}
}
}
cout << -1 << "\n";
}
signed main()
{
oopt;
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
50504 KB |
Output is correct |
2 |
Correct |
28 ms |
50532 KB |
Output is correct |
3 |
Correct |
30 ms |
50512 KB |
Output is correct |
4 |
Correct |
32 ms |
50504 KB |
Output is correct |
5 |
Correct |
33 ms |
50524 KB |
Output is correct |
6 |
Correct |
32 ms |
50648 KB |
Output is correct |
7 |
Correct |
29 ms |
50504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
50508 KB |
Output is correct |
2 |
Correct |
30 ms |
50504 KB |
Output is correct |
3 |
Correct |
28 ms |
50512 KB |
Output is correct |
4 |
Correct |
37 ms |
50760 KB |
Output is correct |
5 |
Correct |
28 ms |
50504 KB |
Output is correct |
6 |
Correct |
30 ms |
50672 KB |
Output is correct |
7 |
Correct |
27 ms |
50580 KB |
Output is correct |
8 |
Correct |
28 ms |
50472 KB |
Output is correct |
9 |
Correct |
30 ms |
50508 KB |
Output is correct |
10 |
Correct |
26 ms |
50768 KB |
Output is correct |
11 |
Correct |
27 ms |
50720 KB |
Output is correct |
12 |
Correct |
26 ms |
50772 KB |
Output is correct |
13 |
Correct |
29 ms |
50684 KB |
Output is correct |
14 |
Correct |
28 ms |
50768 KB |
Output is correct |
15 |
Correct |
28 ms |
50764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
50504 KB |
Output is correct |
2 |
Correct |
30 ms |
50504 KB |
Output is correct |
3 |
Correct |
30 ms |
50512 KB |
Output is correct |
4 |
Correct |
31 ms |
50512 KB |
Output is correct |
5 |
Correct |
32 ms |
50712 KB |
Output is correct |
6 |
Correct |
32 ms |
50528 KB |
Output is correct |
7 |
Correct |
31 ms |
50504 KB |
Output is correct |
8 |
Correct |
31 ms |
50512 KB |
Output is correct |
9 |
Correct |
31 ms |
50512 KB |
Output is correct |
10 |
Correct |
30 ms |
50768 KB |
Output is correct |
11 |
Correct |
31 ms |
50820 KB |
Output is correct |
12 |
Correct |
30 ms |
50768 KB |
Output is correct |
13 |
Correct |
29 ms |
50776 KB |
Output is correct |
14 |
Correct |
30 ms |
50760 KB |
Output is correct |
15 |
Correct |
34 ms |
50768 KB |
Output is correct |
16 |
Correct |
27 ms |
50760 KB |
Output is correct |
17 |
Runtime error |
94 ms |
102728 KB |
Execution killed with signal 11 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
50512 KB |
Output is correct |
2 |
Correct |
30 ms |
50616 KB |
Output is correct |
3 |
Correct |
31 ms |
50460 KB |
Output is correct |
4 |
Correct |
28 ms |
50508 KB |
Output is correct |
5 |
Correct |
28 ms |
50512 KB |
Output is correct |
6 |
Correct |
28 ms |
50508 KB |
Output is correct |
7 |
Correct |
28 ms |
50692 KB |
Output is correct |
8 |
Correct |
30 ms |
50504 KB |
Output is correct |
9 |
Correct |
28 ms |
50512 KB |
Output is correct |
10 |
Correct |
26 ms |
50644 KB |
Output is correct |
11 |
Correct |
28 ms |
50928 KB |
Output is correct |
12 |
Correct |
27 ms |
50532 KB |
Output is correct |
13 |
Correct |
31 ms |
50672 KB |
Output is correct |
14 |
Correct |
31 ms |
50764 KB |
Output is correct |
15 |
Correct |
30 ms |
50800 KB |
Output is correct |
16 |
Correct |
28 ms |
50768 KB |
Output is correct |
17 |
Runtime error |
97 ms |
102640 KB |
Execution killed with signal 11 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
50512 KB |
Output is correct |
2 |
Correct |
31 ms |
50684 KB |
Output is correct |
3 |
Correct |
29 ms |
50512 KB |
Output is correct |
4 |
Correct |
33 ms |
50504 KB |
Output is correct |
5 |
Correct |
28 ms |
50504 KB |
Output is correct |
6 |
Correct |
33 ms |
50720 KB |
Output is correct |
7 |
Correct |
30 ms |
50512 KB |
Output is correct |
8 |
Correct |
31 ms |
50504 KB |
Output is correct |
9 |
Correct |
29 ms |
50504 KB |
Output is correct |
10 |
Correct |
30 ms |
50512 KB |
Output is correct |
11 |
Correct |
31 ms |
50640 KB |
Output is correct |
12 |
Correct |
29 ms |
50504 KB |
Output is correct |
13 |
Correct |
30 ms |
50760 KB |
Output is correct |
14 |
Correct |
35 ms |
51036 KB |
Output is correct |
15 |
Correct |
31 ms |
50768 KB |
Output is correct |
16 |
Correct |
30 ms |
50768 KB |
Output is correct |
17 |
Runtime error |
100 ms |
102728 KB |
Execution killed with signal 11 |
18 |
Halted |
0 ms |
0 KB |
- |