Submission #146366

#TimeUsernameProblemLanguageResultExecution timeMemory
146366muhammad_hokimiyonJakarta Skyscrapers (APIO15_skyscraper)C++14
36 / 100
1084 ms68632 KiB
#include <bits/stdc++.h> //#pragma GCC optimize("Ofast") #define LL long long #define se second #define fi first using namespace std; const int N = 3e4 + 7; const int mod = 1e9 + 7; const int B = sqrt(N); LL n,m; LL a[N]; LL b[N]; LL d[N]; vector < pair < LL , LL > > v[N]; int main() { ios_base::sync_with_stdio(0); //freopen( "input.txt" , "r" , stdin ); //freopen( "output.txt" , "w" , stdout ); cin >> n >> m; for( int i = 0; i < m; i++ ){ cin >> a[i] >> b[i]; } for( int i = 0; i <= n; i++ )d[i] = 2e18; for( int i = 0; i < m; i++ ){ for( int j = 0; j < m; j++ ){ if( i == j )continue; if( abs( a[i] - a[j] ) % b[i] == 0 ){ v[a[i]].push_back({ a[j] , abs( a[i] - a[j] ) / b[i] }); } } } for( int i = 0; i <= n; i++ ){ sort( v[i].begin() , v[i].end() ); } d[a[0]] = 0; queue< LL > q; q.push(a[0]); while( !q.empty() ){ LL x = q.front(); q.pop(); for( auto y : v[x] ){ if( d[x] + y.se < d[y.fi] ){ d[y.fi] = d[x] + y.se; q.push(y.fi); } } } if( d[a[1]] == 2e18 )d[a[1]] = -1; cout << d[a[1]]; }
#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...