Submission #1086403

#TimeUsernameProblemLanguageResultExecution timeMemory
1086403TrinhKhanhDungJakarta Skyscrapers (APIO15_skyscraper)C++14
100 / 100
655 ms3968 KiB
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define sz(x) (int)x.size() #define ALL(v) v.begin(),v.end() #define MASK(k) (1LL << (k)) #define BIT(x, i) (((x) >> (i)) & 1) #define oo (ll)1e18 #define INF (ll)1e9 #define MOD (ll)(998244353) using namespace std; template<class T1, class T2> bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;} template<class T1, class T2> bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;} template<class T1, class T2> void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;} template<class T1, class T2> void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;} template<class T> void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());} const int MAX = 3e4 + 10; int N, M, S, T; vector<int> adj[MAX]; int f[MAX]; void solve(){ cin >> N >> M; for(int i=0; i<M; i++){ int p, d; cin >> p >> d; adj[p].push_back(d); if(i == 0){ S = p; } if(i == 1){ T = p; } } memset(f, 0x3f, sizeof f); f[S] = 0; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; pq.push(make_pair(0, S)); while(sz(pq)){ int u = pq.top().se; int kc = pq.top().fi; pq.pop(); if(kc > f[u]) continue; for(int d: adj[u]){ int cnt = 0; for(int i=u+d; i<N; i+=d){ cnt++; if(minimize(f[i], f[u] + cnt)){ pq.push(make_pair(f[i], i)); } } cnt = 0; for(int i=u-d; i>=0; i-=d){ cnt++; if(minimize(f[i], f[u] + cnt)){ pq.push(make_pair(f[i], i)); } } } } cout << (f[T] >= INF ? -1 : f[T]) << '\n'; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); // freopen("numtable.inp","r",stdin); // freopen("numtable.out","w",stdout); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...