Submission #564907

#TimeUsernameProblemLanguageResultExecution timeMemory
564907training4usacoJakarta Skyscrapers (APIO15_skyscraper)C++11
100 / 100
229 ms6020 KiB
#include <iostream> #include <vector> #include <queue> #include <set> #include <algorithm> using namespace std; #define MAXN 30005 #define INF 1000000009 #define pii pair<int, int> #define mp make_pair #define f first #define s second int n, m; int b[MAXN]; int p[MAXN]; int dist[MAXN]; vector<int> jumps[MAXN]; void solve(int s) { dist[b[0]] = 0; priority_queue<pii, vector<pii>, greater<pii>> pq; pq.push(mp(0,b[0])); // dist, pos while (!pq.empty()) { pii node = pq.top(); pq.pop(); if (node.f > dist[node.s]) { continue; } for(auto jump : jumps[node.s]) { for (int j = 1; node.s + jump * j < n; ++j) { // forward if (dist[node.s + jump * j] > node.f + j) { pq.push(mp(dist[node.s + jump * j] = node.f + j, node.s + jump * j)); if (binary_search(jumps[node.s + jump * j].begin(), jumps[node.s + jump * j].end(), jump)) { // no need to have multiple doges going same "route" break; } } } for (int j = 1;node.s - jump * j >= 0;++j) { // backward if (dist[node.s - jump * j] > node.f + j) { pq.push(mp(dist[node.s - jump * j] = node.f + j, node.s - jump * j)); if (binary_search(jumps[node.s - jump * j].begin(), jumps[node.s - jump * j].end(), jump)) { break; } } } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for(int i = 0; i < m; ++i) { cin >> b[i] >> p[i]; jumps[b[i]].push_back(p[i]); } for(int i = 0; i < n; ++i) { sort(jumps[i].begin(), jumps[i].end()); jumps[i].erase(unique(jumps[i].begin(), jumps[i].end()), jumps[i].end()); } for(int i = 0; i < MAXN; ++i) { dist[i] = INF; } solve(b[0]); if (dist[b[1]] == INF) { cout << "-1" << endl; return 0; } cout << dist[b[1]] << endl; 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...