Submission #167485

#TimeUsernameProblemLanguageResultExecution timeMemory
167485muhammad_hokimiyonJakarta Skyscrapers (APIO15_skyscraper)C++14
100 / 100
461 ms131756 KiB
#include <bits/stdc++.h> //#pragma GCC optimize("Ofast") #define fi first #define se second #define ll long long using namespace std; const int N = 1e6 + 7; const int mod = 1e9 + 7; int n,m; int a[N]; int b[N]; int d[N]; int h[N]; int t[N]; set < int > s[N]; vector < pair < int , int > > v[N]; void solve() { cin >> n >> m; for( int i = 0; i < m; i++ ){ cin >> a[i] >> b[i]; s[a[i]].insert(b[i]); } for( int i = 0; i <= n; i++ )d[i] = 1e9; for( int i = 0; i < n; i++ ){ auto it = s[i].begin(); while( it != s[i].end() ){ int z = i , cnt = 0; while( z + *it < n ){ z += *it , cnt++; v[i].push_back({z , cnt}); if( s[z].find(*it) != s[z].end() )break; } z = i , cnt = 0; while( z - *it >= 0 ){ z -= *it , cnt++; v[i].push_back({z , cnt}); if( s[z].find(*it) != s[z].end() )break; } it++; } } d[a[0]] = 0; set < pair < int , int > > q; q.insert({0 , a[0]}); while( !q.empty() ){ int x = q.begin()->se; q.erase(q.begin()); for( auto y : v[x] ){ if( d[x] + y.se < d[y.fi] ){ q.erase({d[y.fi] , y.fi}); d[y.fi] = d[x] + y.se; q.insert({ d[y.fi] , y.fi }); } } } if( d[a[1]] == 1e9 )d[a[1]] = -1; cout << d[a[1]]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen( "input.txt" , "r" , stdin ); //freopen( "output.txt" , "w" , stdout ); int t = 1;//cin >> t; while( t-- ){ solve(); } }
#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...