#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = (int) 1e9 + 123;
const ll LINF = (ll) 1e18 + 123;
#define rep(i, s, t) for (auto i = (s); i < (t); ++(i))
#define per(i, s, t) for (auto i = (s); i >= (t); --(i))
#define sz(x) ((int) (x).size())
#define mp make_pair
#define pb push_back
bool mini(auto &x, const auto &y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
bool maxi(auto &x, const auto &y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
void run();
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
run();
return 0;
}
const int N = 30003;
int n, m;
int b[N], p[N];
int dist[N];
bool used[N];
vector<int> which[N];
void run() {
cin >> n >> m;
rep(i, 0, m) {
cin >> b[i] >> p[i];
which[b[i]].pb(p[i]);
}
fill(dist, dist + n, INF);
dist[b[0]] = 0;
priority_queue<pair<int, int>> q;
q.push({0, b[0]});
while (sz(q)) {
int v = q.top().second;
q.pop();
if (used[v]) continue;
used[v] = 1;
for (int step : which[v]) {
rep(i, 1, N) {
int to = v + step * i;
if (to >= n) break;
if (mini(dist[to], dist[v] + i)) {
q.push({-dist[to], to});
}
}
rep(i, 1, N) {
int to = v - step * i;
if (to >= n) break;
if (mini(dist[to], dist[v] + i)) {
q.push({-dist[to], to});
}
}
}
}
cout << (dist[b[1]] == INF ? -1 : dist[b[1]]) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
0 ms |
3260 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
0 ms |
3260 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
0 ms |
3260 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
0 ms |
3260 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
0 ms |
3260 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |