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 ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pp push
#define MOD 1000000007
#define INF 1e18
#define N 30005
#define M 5000000
typedef long long ll;
const ll c=174;
using namespace std;
ll b[N],p[N],path[N];
vector<int>pos[N];
unordered_map<int,bool>pp[N];
vector<pair<int,int> >adj[N];
bool vis[N],visited[N];
int main(){
// ios_base::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0)
memset(path,-1,sizeof(path));
ll n,m;
cin >> n >> m;
for(ll i=1;i<=m;i++){
cin >> b[i] >> p[i];
if(pp[b[i]][p[i]]!=0){
vis[i]=1;
}
else{
pos[b[i]].pb(i);
pp[b[i]][p[i]]=1;
}
}
for(int i=1;i<=m;i++){
if(vis[i]==1) continue;
for(int j=b[i]+p[i];j<n;j+=p[i]){
bool flag=0;
for(int k=0;k<pos[j].size();k++){
adj[i].pb({pos[j][k],(j-b[i])/p[i]});
if(p[i]==p[pos[j][k]]) flag=1;
}
if(flag==1) break;
}
for(int j=b[i]-p[i];j>=0;j-=p[i]){
bool flag=0;
for(int k=0;k<pos[j].size();k++){
adj[i].pb({pos[j][k],(b[i]-j)/p[i]});
if(p[i]==p[pos[j][k]]) flag=1;
}
if(flag==1) break;
}
}
priority_queue<pair<ll,ll> >pq;
pq.push({0,1});
while(!pq.empty()){
ll cost=-pq.top().ff;
ll doge_id=pq.top().ss;
// cout << doge_id <<" " << cost << '\n';
path[doge_id]=cost;
pq.pop();
visited[doge_id]=1;
for(pair<int,int>i:adj[doge_id]){
pq.push({-(cost+i.ss),i.ff});
}
while(!pq.empty() && visited[pq.top().ss]==1) pq.pop();
}
cout << path[2];
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:41:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int k=0;k<pos[j].size();k++){
| ~^~~~~~~~~~~~~~
skyscraper.cpp:49:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int k=0;k<pos[j].size();k++){
| ~^~~~~~~~~~~~~~
# | 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... |