이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 3e4 + 5, B = 175;
vector<int> g[MX];
int N, M;
int dist[MX][B];
bool vis[MX];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
for(int i = 0; i < MX; i++) {
for(int j = 0; j < B; j++) {
dist[i][j] = 1e9;
}
}
cin >> N >> M;
int s = 0, f = 0;
for(int i = 0; i < M; i++) {
int x, k;
cin >> x >> k;
g[x].push_back(k);
if(!i) s = x;
if(i == 1) f = x;
}
priority_queue<array<int,3>> pq;
for(auto x : g[s]) {
if(x < B) {
dist[s][x] = 0;
pq.push({dist[s][x], s, x});
} else {
for(int k = 1; s + k * x < N; k++) {
dist[s + k * x][0] = k;
pq.push({-dist[s + k * x][0], s + k * x, 0});
}
for(int k = 1; s - k * x >= 0; k++) {
dist[s - k * x][0] = k;
pq.push({-dist[s - k * x][0], s - k * x, 0});
}
}
}
vis[s] = 1;
while(!pq.empty()) {
auto [d, v, p] = pq.top(); pq.pop();
d = -d;
if(d > dist[v][p]) continue;
if(!vis[v]) {
for(auto x : g[v]) {
if(x < B) {
dist[v][x] = d;
pq.push({-dist[v][x], v, x});
} else {
for(int k = 1; v + k * x < N; k++) {
if(dist[v + k * x][0] > d + k) {
dist[v + k * x][0] = d + k;
pq.push({-dist[v + k * x][0], v + k * x, x});
}
}
for(int k = 1; v - k * x >= 0; k++) {
if(dist[v - k * x][0] > d + k) {
dist[v - k * x][0] = d + k;
pq.push({-dist[v - k * x][0], v - k * x, x});
}
}
}
}
vis[v] = 1;
}
if(v + p < N && dist[v + p][p] > d + 1) {
dist[v + p][p] = d + 1;
pq.push({-dist[v + p][p], v + p, p});
}
if(v - p >= 0 && dist[v - p][p] > d + 1) {
dist[v - p][p] = d + 1;
pq.push({-dist[v - p][p], v - p, p});
}
}
int ans = 1e9;
for(int k = 0; k < B; k++) ans = min(ans, dist[f][k]);
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |