Submission #736159

# Submission time Handle Problem Language Result Execution time Memory
736159 2023-05-05T09:10:27 Z Alihan_8 Jakarta Skyscrapers (APIO15_skyscraper) C++17
0 / 100
1 ms 468 KB
#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 time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Runtime error 1 ms 444 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Runtime error 1 ms 448 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -