Submission #167484

# Submission time Handle Problem Language Result Execution time Memory
167484 2019-12-08T16:37:27 Z muhammad_hokimiyon Jakarta Skyscrapers (APIO15_skyscraper) C++14
0 / 100
27 ms 23932 KB
#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];
vector < pair < int , int > > v[N];

void solve()
{
    cin >> n >> m;
    for( int i = 0; i < m; i++ ){
        cin >> a[i] >> b[i];
        h[a[i]] = 1;
    }
    for( int i = 0; i <= n; i++ )d[i] = 1e9;
    for( int i = 0; i < m; i++ ){
        int f = a[i];
        int cnt = 1;
        while( f + b[i] < n ){
            f += b[i];
            if( h[f] )v[a[i]].push_back({ f , cnt });
            cnt++;
        }
        f = a[i];
        cnt = 1;
        while( f - b[i] >= 0 ){
            f -= b[i];
            if( h[f] )v[a[i]].push_back({ f , cnt });
            cnt++;
        }
    }
    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[1] == 1e9 )d[1] = -1;
    cout << d[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 time Memory Grader output
1 Correct 26 ms 23928 KB Output is correct
2 Incorrect 26 ms 23804 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 23856 KB Output is correct
2 Incorrect 25 ms 23800 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 23800 KB Output is correct
2 Incorrect 25 ms 23928 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 23928 KB Output is correct
2 Incorrect 25 ms 23800 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 27 ms 23932 KB Output is correct
2 Incorrect 26 ms 23800 KB Output isn't correct
3 Halted 0 ms 0 KB -