Submission #612589

#TimeUsernameProblemLanguageResultExecution timeMemory
612589JomnoiJakarta Skyscrapers (APIO15_skyscraper)C++17
10 / 100
2 ms2400 KiB
#include <bits/stdc++.h> using namespace std; const int MAX_N = 3e4 + 5; const int INF = 1e9 + 7; int B[MAX_N], P[MAX_N]; map <int, int> dist[MAX_N]; vector <int> graph[MAX_N]; int main() { cin.tie(nullptr)->sync_with_stdio(false); int N, M; cin >> N >> M; for(int i = 0; i < M; i++) { cin >> B[i] >> P[i]; graph[B[i]].push_back(P[i]); } for(int i = 0; i < N; i++) { sort(graph[i].begin(), graph[i].end()); graph[i].resize(unique(graph[i].begin(), graph[i].end()) - graph[i].begin()); } queue <pair <int, int>> q; q.emplace(B[0], P[0]); dist[B[0]][P[0]] = 0; while(!q.empty()) { auto [b, p] = q.front(); q.pop(); if(b == B[1]) { cout << dist[b][p]; return 0; } if(b - p >= 0 and !dist[b - p].count(p)) { dist[b - p][p] = dist[b][p] + 1; q.emplace(b - p, p); } if(b + p < N and !dist[b + p].count(p)) { dist[b + p][p] = dist[b][p] + 1; q.emplace(b + p, p); } for(auto p2 : graph[b]) { if(!dist[b].count(p2)) { dist[b][p2] = dist[b][p]; q.emplace(b, p2); } } } cout << -1; return 0; }
#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...