# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
31985 | chonka | Jakarta Skyscrapers (APIO15_skyscraper) | C++98 | 0 ms | 2840 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std ;
#define MAXN 30007
int n , m ;
int st , en ;
vector < int > v[ MAXN ] ;
int dist[ MAXN ] ;
void bfs ( ) {
int i , j ;
queue < int > q ;
q.push ( st ) ;
dist[ st ] = 1 ;
while ( q.empty ( ) == false ) {
int vertex = q.front ( ) ;
q.pop ( ) ;
int sz = v[ vertex ].size ( ) ;
for ( i = sz - 1 ; i >= 0 ; i -- ) {
if ( i != ( sz - 1 ) && v[ vertex ][ i ] == v[ vertex ][ i + 1 ] ) { continue ; }
int br = 0 ;
for ( j = vertex ; j <= n ; j += v[ vertex ][ i ] ) {
if ( dist[ j ] == 0 ) {
dist[ j ] = dist[ vertex ] + br ;
q.push ( j ) ;
}
if ( j == en ) { return ; }
br ++ ;
}
br = 0 ;
for ( j = vertex ; j >= 1 ; j -= v[ vertex ][ i ] ) {
if ( dist[ j ] == 0 ) {
dist[ j ] = dist[ vertex ] + br ;
q.push ( j ) ;
}
if ( j == en ) { return ; }
br ++ ;
}
}
}
}
void input ( ) {
scanf ( "%d%d" , &n , &m ) ;
int i ;
for ( i = 1 ; i <= m ; i ++ ) {
int x, y ;
scanf ( "%d%d" , &x , &y ) ;
x ++ ;
v[ x ].push_back ( y ) ;
if ( i == 1 ) {
st = x ;
}
if ( i == 2 ) {
en = x ;
}
}
for ( i = 1 ; i <= n ; i ++ ) {
sort ( v[ i ].begin ( ) , v[ i ].end ( ) ) ;
}
}
void solve ( ) {
bfs ( ) ;
printf ( "%d\n" , dist[ en ] - 1 ) ;
}
int main ( ) {
input ( ) ;
solve ( ) ;
return 0 ;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |