#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 2e3+5;
const ll INF = 1e18;
vector<pii> adj[N];
ll dis[N];
int main(){
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; ++i){
int x, p;
scanf("%d%d", &x, &p), x++;
for(int ad = 1;; ++ad){
if(x-ad*p < 1 and x+ad*p > n)
break;
if(x-ad*p >= 1)
adj[x].pb({x-ad*p, ad});
if(x+ad*p <= n)
adj[x].pb({x+ad*p, ad});
}
}
for(int i = 1; i <= n; ++i)
dis[i] = INF;
priority_queue<pli, vector<pli>, greater<pli>> pq;
pq.push({0, 1});
dis[1] = 0;
while(!pq.empty()){
pli P = pq.top();
pq.pop();
ll val = P.ff;
int x = P.ss;
if(dis[x] != val)
continue;
tr(it, adj[x]){
int u = it->ff, w = it->ss;
if(umin(dis[u], dis[x]+w))
pq.push({dis[u], u});
}
}
if(dis[2] == INF)
puts("-1");
else
printf("%lld\n", dis[2]);
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:22:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
skyscraper.cpp:25:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | scanf("%d%d", &x, &p), x++;
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |