Submission #396239

#TimeUsernameProblemLanguageResultExecution timeMemory
396239SavicSJakarta Skyscrapers (APIO15_skyscraper)C++14
100 / 100
249 ms70596 KiB
#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <bits/stdc++.h> #define fi first #define se second #define pb push_back #define sz(a) (int)a.size() #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define ff(i,a,b) for(int i=a;i<=b;i++) #define fb(i,b,a) for(int i=b;i>=a;i--) using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef pair<int,int> pii; const int maxn = 100005; const int inf = 1e9 + 5; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // os.order_of_key(k) the number of elements in the os less than k // *os.find_by_order(k) print the k-th smallest number in os(0-based) int n, m; set<int> skok[maxn]; vector<pii> g[maxn]; bool pripada(int X) { return (X >= 0 && X < n); } ll dist[maxn]; void dij(int s) { ff(i,0,n - 1)dist[i] = inf; priority_queue<pii, vector<pii>, greater<pii>> pq; pq.push({dist[s] = 0, s}); while(!pq.empty()) { pii v = pq.top(); pq.pop(); if(dist[v.se] < v.fi)continue; for(auto c : g[v.se]) { int u = c.fi; int w = c.se; if(dist[u] > w + v.fi) { pq.push({dist[u] = w + v.fi, u}); } } } } int main() { ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); cin >> n >> m; int start = 0, end = 0; ff(i,0,m - 1) { int X, Y; cin >> X >> Y; if(i != 1)skok[X].insert(Y); if(i == 0)start = X; if(i == 1)end = X; } ff(i,0,n - 1) { for(auto c : skok[i]) { int j = 1; while(1) { int nw = i + j * c; if(!pripada(nw))break; g[i].pb({nw, j}); if(skok[nw].count(c))break; j += 1; } } } ff(i,0,n - 1) { for(auto c : skok[i]) { int j = 1; while(1) { int nw = i - j * c; if(!pripada(nw))break; g[i].pb({nw, j}); if(skok[nw].count(c))break; j += 1; } } } dij(start); cout << (dist[end] == inf ? -1 : dist[end]) << endl; return 0; } /** 5 3 0 2 1 1 4 1 // probati bojenje sahovski ili slicno **/
#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...