Submission #28043

#TimeUsernameProblemLanguageResultExecution timeMemory
28043sampritiJakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
869 ms261272 KiB
#include <iostream> #include <algorithm> #include <set> #include <vector> #include <climits> using namespace std; #define LIM 150 int B[30000], P[30000]; set<int> A[30000]; vector<pair<pair<short, short>, unsigned char> > G[30000][LIM + 1]; short D[30000][LIM + 1]; set<pair<short, pair<short, short> > > Q; int main() { ios::sync_with_stdio(false); cin.tie(0); int N, M; cin >> N >> M; for(int i = 0; i < M; i++) { cin >> B[i] >> P[i]; A[B[i]].insert(P[i]); } for(int i = 0; i < N; i++) { for(int x: A[i]) { if(x < LIM) { G[i][LIM].push_back({{i, x}, 0}); } } for(int x: A[i]) { if(x >= LIM) { for(int d = 1; i + d * x < N; d++) { if(A[i + d * x].size() == 0) continue; G[i][LIM].push_back({{i + d * x, LIM}, d}); } for(int d = 1; i - d * x >= 0; d++) { if(A[i - d * x].size() == 0) continue; G[i][LIM].push_back({{i - d * x, LIM}, d}); } } } for(int x = 1; x < LIM; x++) { G[i][x].push_back({{i, LIM}, 0}); if(i + x < N) { G[i][x].push_back({{i + x, x}, 1}); } if(i - x >= 0) { G[i][x].push_back({{i - x, x}, 1}); } } } for(int i = 0; i < N; i++) { for(int j = 0; j <= LIM; j++) { D[i][j] = SHRT_MAX; } } Q.insert({0, {B[0], LIM}}); D[B[0]][LIM] = 0; while(!Q.empty()) { auto it = *Q.begin(); Q.erase(Q.begin()); int i = it.second.first, j = it.second.second; if(i == B[1] && j == LIM) break; for(auto it2: G[i][j]) { int i2 = it2.first.first, j2 = it2.first.second, d = it2.second; if(D[i][j] + d < D[i2][j2]) { if(D[i2][j2] != SHRT_MAX) { Q.erase({D[i2][j2], {i2, j2}}); } D[i2][j2] = D[i][j] + d; Q.insert({D[i2][j2], {i2, j2}}); } } } if(D[B[1]][LIM] == SHRT_MAX) { cout << -1 << endl; } else { cout << D[B[1]][LIM] << endl; } }
#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...