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
const int INF = 1e9;
vector<vector<pair<int,int>>> adj(30000);
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t=1,n,k,st,fin;
cin >> n >> k;
pair<int,int> doge[k];
vector<set<int>> doges(n);
vector<int> dist(n);
// memset(dist, 0x3f, sizeof dist);
for (int i = 0; i < k; i++){
cin >> doge[i].first >> doge[i].second;
doges[doge[i].first].insert(doge[i].second);
if (i == 0){
st = doge[i].first;
}
if (i == 1){
fin = doge[i].first;
}
}
sort(doge,doge+k);
for (int i = 0; i < k; i++){
for (int j = doge[i].first+doge[i].second; j < n; j += doge[i].second){
adj[doge[i].first].push_back({j,((j-doge[i].first)/doge[i].second)});
if (doges[j].find(doge[i].second) != doges[j].end()){
break;
}
}
for (int j = doge[i].first-doge[i].second; j >= 0; j -= doge[i].second){
adj[doge[i].first].push_back({j,((doge[i].first-j)/doge[i].second)});
if (doges[j].find(doge[i].second) != doges[j].end()){
break;
}
}
}
for (int i = 0; i < n; i++){
dist[i] = INF;
}
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
pq.push({0,st});
dist[st] = 0;
while(!pq.empty()){
int distp = pq.top().first;
int p = pq.top().second;
pq.pop();
if (distp != dist[p]){
continue;
}
for (auto ch: adj[p]){
if (dist[p] + ch.second < dist[ch.first]){
dist[ch.first] = dist[p] + ch.second;
pq.push({dist[ch.first],ch.first});
}
}
}
dist[fin] >= INF ? cout << "-1\n" : cout << dist[fin] << "\n";
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:10:9: warning: unused variable 't' [-Wunused-variable]
10 | int t=1,n,k,st,fin;
| ^
skyscraper.cpp:46:12: warning: 'st' may be used uninitialized in this function [-Wmaybe-uninitialized]
46 | dist[st] = 0;
| ^
skyscraper.cpp:61:13: warning: 'fin' may be used uninitialized in this function [-Wmaybe-uninitialized]
61 | dist[fin] >= INF ? cout << "-1\n" : cout << dist[fin] << "\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... |