제출 #520850

#제출 시각아이디문제언어결과실행 시간메모리
520850Alex_tz307Jakarta Skyscrapers (APIO15_skyscraper)C++17
0 / 100
1 ms220 KiB
#include <bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; void minSelf(int &x, int y) { if (y < x) { x = y; } } void testCase() { int n, m; cin >> n >> m; vector<vector<int>> g(n); int source = 0, sink = 0; for (int i = 0; i < m; ++i) { int b, p; cin >> b >> p; if (0 <= b - p || b + p < n) { g[b].emplace_back(p); } if (i == 0) { source = b; } if (i == 1) { sink = b; } } for (int v = 0; v < n; ++v) { sort(g[v].begin(), g[v].end()); g[v].erase(unique(g[v].begin(), g[v].end()), g[v].end()); } queue<pair<int, int>> q; vector<map<int, int>> dp(n); for (int p : g[source]) { q.emplace(source, p); dp[source][p] = 0; } while (!q.empty()) { int u, p; tie(u, p) = q.front(); q.pop(); if (0 <= u - p) { if (dp[u - p].find(p) == dp[u - p].end() || dp[u - p][p] > dp[u][p] + 1) { dp[u - p][p] = dp[u][p] + 1; q.emplace(u - p, p); } } if (u + p < n) { if (dp[u + p].find(p) == dp[u + p].end() || dp[u + p][p] > dp[u][p] + 1) { dp[u + p][p] = dp[u][p] + 1; q.emplace(u + p, p); } } for (int pp : g[u]) { if (0 <= u - pp) { if (dp[u - pp].find(pp) == dp[u - pp].end() || dp[u - pp][pp] > dp[u][p] + 1) { dp[u - pp][pp] = dp[u][p] + 1; q.emplace(u - pp, pp); } } if (u + pp < n) { if (dp[u + pp].find(pp) == dp[u + pp].end() || dp[u + pp][pp] > dp[u][p] + 1) { dp[u + pp][pp] = dp[u][p] + 1; q.emplace(u + pp, pp); } } } } int best = INF; for (auto it : dp[sink]) { minSelf(best, it.second); } if (best == INF) { best = -1; } cout << best << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int tests = 1; for (int tc = 0; tc < tests; ++tc) { testCase(); } 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...