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>
#define lsb(x) (x & (-x))
#define ll long long
using namespace std;
const int INF = 1e9;
const int MAXN = 30000;
vector <int> arr[MAXN + 1];
map < pair <int, int>, bool > mp;
struct Data {
int dst, nod;
bool operator <(const Data &other) const {
return dst > other.dst;
}
};
priority_queue < Data > pq;
bool vis[MAXN + 1];
int dist[MAXN + 1];
int main() {
//ifstream cin("C.in");
//ofstream cout("C.out");
int i, n, m;
ios::sync_with_stdio(false);
cin >> n >> m;
int nod1, nod2;
for(i = 0; i < m; i++) {
int nod, jump;
cin >> nod >> jump;
if(mp[{nod, jump}] == 1) {
continue;
}
mp[{nod, jump}] = 1;
arr[nod].push_back(jump);
if(i == 0) {
pq.push({0, nod});
nod1 = nod;
}
if(i == 1) {
nod2 = nod;
}
}
for(i = 0; i < n; i++) {
dist[i] = INF;
}
dist[nod1] = 0;
while(!pq.empty()) {
int nod = pq.top().nod;
int dst = pq.top().dst;
pq.pop();
if(vis[nod]) {
continue;
}
if(nod == nod2) {
cout << dst;
return 0;
}
vis[nod] = 1;
for(auto it : arr[nod]) {
int aux = nod - it;
int cst = 1;
while(aux >= 0) {
if(dist[aux] > dst + cst) {
dist[aux] = dst + cst;
pq.push({dist[aux], aux});
}
aux -= it;
cst++;
}
aux = nod + it;
cst = 1;
while(aux < n) {
if(dist[aux] > dst + cst) {
dist[aux] = dst + cst;
pq.push({dist[aux], aux});
}
aux += it;
cst++;
}
}
}
cout << -1;
//cin.close();
//cout.close();
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:58:9: warning: 'nod2' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(nod == nod2) {
^~
skyscraper.cpp:50:16: warning: 'nod1' may be used uninitialized in this function [-Wmaybe-uninitialized]
dist[nod1] = 0;
~~~~~~~~~~~^~~
# | 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... |