This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef long long ll;
#define pb push_back
#define eb emplace_back
#define countbits __builtin_popcount
#define beg0 __builtin_clz
#define terminal0 __builtin_ctz
int mod = 1e9+7;
void setIO(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
}
void setIO(string f){
freopen((f+".in").c_str(),"r",stdin);
freopen((f+".out").c_str(),"w",stdout);
setIO();
}
int N, M;
vector<vector<int>> l;
vector<pair<int,int>> m;
vector<set<int>> pos;
int main(){
setIO();
cin >> N >> M;
for (int i=0;i<N;i++) l.pb(vector<int>());
for (int i=0;i<M;i++){
int pos; cin >> pos;
int moves; cin >> moves;
m.pb({pos,moves});
}
int dp[M][N];
for (int i=0;i<M;i++){
for (int j=0;j<N;j++){
dp[i][j]=1e9;
}
}
queue<pair<pair<int,int>,pair<int,int>>> q;
q.push({{m[0].first,m[0].second},{0,0}});
while (!q.empty()){
int pos=q.front().first.first;
int jmp = q.front().first.second;
int dist = q.front().second.first;
int ind = q.front().second.second;
q.pop();
if (dp[ind][pos]<dist) continue;
dp[ind][pos]=dist;
if (pos+jmp<N){
if (dp[ind][pos+jmp]>dist+1) q.push({{pos+jmp,jmp},{dist+1,ind}});
}
if (pos-jmp>=0){
if (dp[ind][pos-jmp]>dist+1) q.push({{pos-jmp,jmp},{dist+1,ind}});
}
for (int i=0;i<M;i++){
if (i==ind) continue;
if (m[i].first==pos){
if (dp[i][pos]>dist){
q.push({{pos,m[i].second},{dist,i}});
}
}
}
}
int totmin = 1e9;
for (int i=0;i<N;i++){
totmin=min(totmin,dp[1][i]);
}
cout << (totmin==1e9?-1:totmin) << "\n";
}
Compilation message (stderr)
skyscraper.cpp: In function 'void setIO(std::string)':
skyscraper.cpp:19:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen((f+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:20:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen((f+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |