Submission #736159

#TimeUsernameProblemLanguageResultExecution timeMemory
736159Alihan_8Jakarta Skyscrapers (APIO15_skyscraper)C++17
0 / 100
1 ms468 KiB
#include <bits/stdc++.h> using namespace std; #define all(x) x.begin(), x.end() #define pb push_back #define ln '\n' #define int long long template <class _T> bool chmin(_T &x, const _T &y){ bool flag = false; if ( x > y ){ x = y; flag |= true; } return flag; } template <class _T> bool chmax(_T &x, const _T &y){ bool flag = false; if ( x < y ){ x = y; flag |= true; } return flag; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector <int> g[n]; for ( int i = 1; i <= m; i++ ){ int x, y; cin >> x >> y; g[x].pb(y); } const int inf = 1e15 + 1, N = 2e3 + 1; vector <vector<int>> dis(n, vector <int> (N, inf)); queue <pair<int,int>> q; for ( auto i: g[0] ){ q.push({0, i}); dis[0][i] = 0; } while ( !q.empty() ){ auto [x, p] = q.front(); q.pop(); int v = dis[x][p], y = x, step = 0; while ( y >= p ){ y -= p; ++step; if ( chmin(dis[y][p], v + step) ){ q.push({y, p}); } for ( auto i: g[y] ){ if ( chmin(dis[y][i], v + step) ){ q.push({y, i}); } } } step = 0; while ( x + p < n ){ x += p; ++step; if ( chmin(dis[x][p], v + step) ){ q.push({x, p}); } for ( auto i: g[x] ){ if ( chmin(dis[x][i], v + step) ){ q.push({x, i}); } } } } int ans = *min_element(all(dis[1])); if ( ans == inf ) ans = -1; cout << ans; cout << '\n'; }
#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...