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 comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
const double PI = 4 * atan(1);
#define sz(x) (int)(x).size()
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define vi vector<int>
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define vpi vector<pair<int, int>>
#define vpd vector<pair<double, double>>
#define pd pair<double, double>
#define f0r(i,a) for(int i=0;i<a;i++)
#define f1r(i,a,b) for(int i=a;i<b;i++)
#define trav(a, x) for (auto& a : x)
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
void io(string taskname){
string fin = taskname + ".in";
string fout = taskname + ".out";
const char* FIN = fin.c_str();
const char* FOUT = fout.c_str();
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
fast_io();
}
const int MAX = 3e4 + 5;
const int INF = 1e9;
int n, m;
map<int, map<int, vi>> um;
///first is the modulus
///next is remainders, then is set of doges that works
vpi d;
vpi adj[MAX];
set<int> power[MAX];
int dist[MAX];
int par[MAX];
int dijkstra(int st, int end) {
fill(dist, dist + MAX, INF);
dist[st] = 0;
// <distance, <previous vertex, current vertex>>
priority_queue<pair<ll, pair<int, int>>, vector<pair<ll, pair<int, int>> >, greater<pair<ll, pair<int ,int>> > > pq;
pq.emplace(0, make_pair(st, st));
while(!pq.empty()){
auto top = pq.top();
pq.pop();
ll d = top.first;
int prev = top.second.first;
int cur = top.second.second;
if(d > dist[cur])
continue;
par[cur] = prev;
for(auto v: adj[cur]){
int nxt = v.first;
ll w = v.second;
if (dist[nxt] > dist[cur] + w) {
dist[nxt] = dist[cur] + w;
pq.emplace(dist[nxt], make_pair(cur, nxt));
}
}
}
return dist[end];
}
int main(){
fast_io();
cin >> n >> m;
f0r(i, m){
int bi, pi;
cin >> bi >> pi;
d.eb(mp(bi, pi));
power[bi].insert(pi);
}
f0r(i, n){
for(int p: power[i]){
int cur = i+p;
int val = 1;
while(cur<n){
adj[i].eb(mp(cur, val));
if(power[cur].find(p) != power[cur].end()) break;
val++;
cur += p;
}
cur = i-p;
val = 1;
while(cur>=0){
adj[i].eb(mp(cur, val));
if(power[cur].find(p) != power[cur].end()) break;
val++;
cur -= p;
}
}
}
int res = dijkstra(d[0].f, d[1].f);
if(res == INF) cout << -1 << endl;
else cout << res << endl;
return 0;
}
Compilation message (stderr)
skyscraper.cpp:1:0: warning: ignoring #pragma comment [-Wunknown-pragmas]
#pragma comment(linker, "/stack:200000000")
skyscraper.cpp: In function 'void io(std::__cxx11::string)':
skyscraper.cpp:47:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FIN, "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:48:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FOUT, "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... |