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 <cstdio>
#include <vector>
#include <queue>
#include <tuple>
#include <algorithm>
#include <limits>
using namespace std;
vector<int> ps[30000];
int dis[30000];
int main() {
int n, m;
scanf("%d %d", &n, &m);
int s, t;
for(int i = 0; i < m; i++) {
int b, p;
scanf("%d %d", &b, &p);
ps[b].push_back(p);
if(i == 0)
s = b;
else if(i == 1)
t = b;
}
for(int i = 0; i < n; i++) {
vector<int>& v = ps[i];
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
fill_n(dis, n, numeric_limits<int>::max());
dis[s] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s});
while(!pq.empty()) {
int cost, where;
tie(cost, where) = pq.top();
pq.pop();
if(where == t) {
printf("%d\n", cost);
return 0;
}
if(dis[where] < cost)
continue;
for(int p : ps[where]) {
for(int i = 1; where + i * p < n; i++) {
if(dis[where + i * p] > cost + i)
pq.push({dis[where + i * p] = cost + i, where + i * p});
//if(binary_search(ps[where + i * p].begin(), ps[where + i * p].end(), p))
//break;
}
for(int i = 1; where - i * p >= 0; i++) {
if(dis[where - i * p] > cost + i)
pq.push({dis[where - i * p] = cost + i, where - i * p});
//if(binary_search(ps[where - i * p].begin(), ps[where - i * p].end(), p))
//break;
}
}
}
puts("-1");
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:14:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:18:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &b, &p);
~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:41:9: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(where == t) {
^~
# | 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... |