#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 + val < 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 + val < 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
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;
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1272 KB |
Output is correct |
2 |
Correct |
3 ms |
1412 KB |
Output is correct |
3 |
Correct |
3 ms |
1412 KB |
Output is correct |
4 |
Correct |
3 ms |
1528 KB |
Output is correct |
5 |
Correct |
3 ms |
1752 KB |
Output is correct |
6 |
Correct |
3 ms |
1752 KB |
Output is correct |
7 |
Correct |
3 ms |
1752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1752 KB |
Output is correct |
2 |
Correct |
3 ms |
1752 KB |
Output is correct |
3 |
Correct |
3 ms |
1752 KB |
Output is correct |
4 |
Correct |
34 ms |
1816 KB |
Output is correct |
5 |
Correct |
3 ms |
1816 KB |
Output is correct |
6 |
Correct |
3 ms |
1816 KB |
Output is correct |
7 |
Correct |
3 ms |
1816 KB |
Output is correct |
8 |
Correct |
3 ms |
1816 KB |
Output is correct |
9 |
Incorrect |
3 ms |
1816 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1816 KB |
Output is correct |
2 |
Correct |
3 ms |
1816 KB |
Output is correct |
3 |
Correct |
3 ms |
1816 KB |
Output is correct |
4 |
Correct |
3 ms |
1816 KB |
Output is correct |
5 |
Correct |
3 ms |
1832 KB |
Output is correct |
6 |
Correct |
3 ms |
1836 KB |
Output is correct |
7 |
Correct |
3 ms |
1840 KB |
Output is correct |
8 |
Correct |
3 ms |
1860 KB |
Output is correct |
9 |
Incorrect |
3 ms |
1864 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1868 KB |
Output is correct |
2 |
Correct |
3 ms |
1876 KB |
Output is correct |
3 |
Correct |
3 ms |
1876 KB |
Output is correct |
4 |
Correct |
3 ms |
1880 KB |
Output is correct |
5 |
Correct |
3 ms |
1880 KB |
Output is correct |
6 |
Correct |
3 ms |
1888 KB |
Output is correct |
7 |
Correct |
3 ms |
1960 KB |
Output is correct |
8 |
Correct |
3 ms |
1960 KB |
Output is correct |
9 |
Incorrect |
3 ms |
1960 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1960 KB |
Output is correct |
2 |
Correct |
3 ms |
1960 KB |
Output is correct |
3 |
Correct |
3 ms |
1960 KB |
Output is correct |
4 |
Correct |
3 ms |
1960 KB |
Output is correct |
5 |
Correct |
2 ms |
1960 KB |
Output is correct |
6 |
Correct |
3 ms |
1960 KB |
Output is correct |
7 |
Correct |
2 ms |
1960 KB |
Output is correct |
8 |
Correct |
3 ms |
1960 KB |
Output is correct |
9 |
Incorrect |
3 ms |
1960 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |