제출 #162244

#제출 시각아이디문제언어결과실행 시간메모리
162244rama_pangJakarta Skyscrapers (APIO15_skyscraper)C++14
22 / 100
904 ms188640 KiB
#include <bits/stdc++.h> using namespace std; int N, M; vector<set<pair<int, int>>> G; // first = destination, second = cost vector<set<int>> building; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> M; G.resize(N); building.resize(N); int start, end, p; cin >> start >> p; building[start].emplace(p); cin >> end >> p; building[end].emplace(p); for (int i = 2; i < M; i++) { int b, p; cin >> b >> p; building[b].emplace(p); } /* Build Graph */ for (int i = 0; i < N; i++) { for (auto v : building[i]) { for (int step = 1; i + step * v < N; step++) { // forward G[i].emplace(i + step * v, step); if (building[i + step * v].count(i)) break; } for (int step = 1; i - step * v >= 0; step++) { // backward G[i].emplace(i - step * v, step); if (building[i - step * v].count(i)) break; } } } /* Dijkstra on created graph */ int ans = INT_MAX; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; pq.emplace(0, start); vector<int> dist(N, INT_MAX); dist[start] = 0; while (!pq.empty()) { int n = pq.top().second, d = pq.top().first; pq.pop(); if (dist[n] != d) continue; if (n == end) ans = min(ans, d); for (auto v : G[n]) { if (dist[v.first] == INT_MAX || dist[v.first] > d + v.second) { dist[v.first] = d + v.second; pq.emplace(dist[v.first], v.first); } } } if (ans == INT_MAX) ans = -1; cout << ans << "\n"; 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...