/*
Code written by Talant I.D.
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define OK cout << "OK" << endl;
const int mod = 998244353;
ll mode(ll a) {
a %= mod;
if (a < 0) a += mod;
return a;
}
ll subt(ll a, ll b) {
return mode(mode(a)-mode(b));
}
ll add(ll a, ll b) {
return mode(mode(a)+mode(b));
}
ll mult(ll a, ll b) {
return mode(mode(a)*mode(b));
}
ll binpow(ll a, ll b) {
ll res = 1;
while (b) {
if (b&1) res = mult(res, a);
a = mult(a, a);
b >>= 1;
}
return res;
}
const int N = 3e4+7;
int dist[N];
pii par[N];
vector <int> who[N];
vector <pii> graph[N];
void dijkstra() {
for (int i = 0; i < N; i++) {
dist[i] = 2e9;
par[i] = mp(-1, -1);
}
dist[1] = 0;
priority_queue <pii> K;
K.push(mp(0, 1));
while(!K.empty()) {
auto v = K.top().second; K.pop();
for (auto to : graph[v]) {
if (dist[to.first] > dist[v]+to.second) {
dist[to.first] = dist[v]+to.second;
par[to.first] = mp(v, to.second);
K.push(mp(-dist[to.first], to.first));
}
}
}
}
int main() {
do_not_disturb
int h, n;
cin >> h >> n;
vector <pii> doges(n);
for (auto &to : doges) cin >> to.first >> to.second;
for (int i = 0; i < n; i++) {
who[doges[i].first].pb(i);
}
for (int i = 0; i < n; i++) {
int cnt = 0;
int jump = doges[i].second;
int j = doges[i].first-jump;
while (j+jump < h) {
j += jump;
cnt++;
for (auto to : who[j]) {
graph[to].pb(mp(i, cnt));
}
}
j = doges[i].first+jump;
cnt = 0;
while (j-jump >= 0) {
j -= jump;
cnt++;
for (auto to : who[j]) {
graph[to].pb(mp(i, cnt));
}
}
}
dijkstra();
if (dist[0] >= 1e9) cout << -1;
else cout << dist[0];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2000 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |