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;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
void Solve()
{
int n, m; cin >> n >> m;
vector <pair<int, int>> a(m);
set <int> st[n];
int target;
vector <int> d(n, INF);
priority_queue <pair<int, int>> pq;
for (int i = 0; i < m; i++){
cin >> a[i].f >> a[i].s;
if (i == 0) {
d[a[i].f] = 0;
pq.push({0, a[i].f});
}
if (i != 1) st[a[i].f].insert(a[i].s);
else target = a[i].f;
}
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < n; i++){
for (auto x : st[i]){
for (int j = 1; ; j++){
int go = x * j + i;
if (go >= n || go < 0) break;
adj[i].push_back({go, j});
if (st[go].find(x) != st[go].end()) break;
}
for (int j = 1; ; j++){
int go = i - x * j;
if (go < 0 || go >= n) break;
adj[i].push_back({go, j});
if (st[go].find(x) != st[go].end()) break;
}
}
}
while (!pq.empty()){
auto pi = pq.top();
pq.pop();
int u = pi.second;
int ds = -pi.first;
if (d[u] != ds) continue;
for (auto [v, w] : adj[u]){
if (d[v] > d[u] + w){
d[v] = d[u] + w;
pq.push({-d[v], v});
}
}
}
// cout << target << "\n";
if (d[target] == INF) cout << -1 << "\n";
else cout << d[target] << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'void Solve()':
skyscraper.cpp:67:17: warning: 'target' may be used uninitialized in this function [-Wmaybe-uninitialized]
67 | if (d[target] == INF) cout << -1 << "\n";
| ^
# | 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... |