This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define maxn 30010
#define pii pair<int, int>
int N, M;
priority_queue<pii, vector<pii>, greater<pii>> pq;
const int inf = 987654321;
vector<vector<int>> mov(maxn);
int dist[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
int source, sink;
int b, p;
for (int i = 0; i < M; i++) {
cin >> b >> p;
mov[b].push_back(p);
if (i == 0) source = b;
if (i == 1) sink = b;
}
for (int i = 0; i < N-1; i++) {
sort(mov[i].begin(), mov[i].end());
}
fill(dist, dist+maxn, inf);
dist[source] = 0;
pq.push(pii(0, source));
while (pq.size()) {
pii cur = pq.top(); pq.pop();
int spot = cur.second;
int d = cur.first;
if (d > dist[spot]) continue;
if (spot == sink) break;
int prev = -1;
for (int val : mov[spot]) {
if (val == prev) continue;
prev = val;
for (int jp = 1; jp < N; jp++) {
int nx = jp*val + spot;
if (nx >= N) break;
if (d + jp < dist[nx]) {
dist[nx] = d+jp;
pq.push(pii(d+jp, nx));
}
if (binary_search(mov[nx].begin(), mov[nx].end(), val)) break;
}
for (int jp = 1; jp < N; jp++) {
int nx = spot - jp*val;
if (nx < 0) break;
if (nx >= N) break;
if (d + jp < dist[nx]) {
dist[nx] = d+jp;
pq.push(pii(d+jp, nx));
}
if (binary_search(mov[nx].begin(), mov[nx].end(), val)) break;
}
}
}
if (dist[sink] == inf) dist[sink] = -1;
cout << dist[sink] << endl;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:38:3: warning: 'sink' may be used uninitialized in this function [-Wmaybe-uninitialized]
if (spot == sink) break;
^~
# | 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... |