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;
typedef long long ll;
const int mxN = 3e4, OO = 0x3f3f3f3f;
int N, M;
vector<int> doges[mxN];
vector<pair<int, int>> adj[mxN];
int dijkstra(int st, int en) {
vector<int> dist(mxN, OO);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, st});
while(pq.size()) {
auto node = pq.top();
pq.pop();
if (node.first > dist[node.second])
continue;
if (node.second == en)
return node.first;
for (auto it : adj[node.second]) {
if (dist[it.second] > node.first + it.first) {
dist[it.second] = node.first + it.first;
pq.push({dist[it.second], it.second});
}
}
}
return -1;
}
void solve() {
cin >> N >> M;
int stB, enB;
for (int i = 0; i < M; i++) {
int B, P;
cin >> B >> P;
if (!i) stB = B;
if (!(i-1)) enB = B;
doges[B].push_back(P);
}
for (int i = 0; i < N; i++) {
for (auto doge : doges[i]) {
for (int to = i + doge, j = 1; to < N; to += doge, j++){
adj[i].push_back({j, to});
}
for (int to = i - doge, j = 1; to >= 0; to -= doge, j++){
adj[i].push_back({j, to});
}
}
}
cout << dijkstra(stB, enB);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
}
Compilation message (stderr)
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:59:27: warning: 'enB' may be used uninitialized in this function [-Wmaybe-uninitialized]
59 | cout << dijkstra(stB, enB);
| ^
skyscraper.cpp:59:27: warning: 'stB' may be used uninitialized in this function [-Wmaybe-uninitialized]
# | 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... |