Submission #602528

#TimeUsernameProblemLanguageResultExecution timeMemory
602528snasibov05Jakarta Skyscrapers (APIO15_skyscraper)C++14
36 / 100
111 ms262144 KiB
#include <bits/stdc++.h> using namespace std; #define oo 1000000000000000000ll #define int long long signed main() { int n, m; cin >> n >> m; vector<int> b(m), p(m); for (int i = 0; i < m; ++i) cin >> b[i] >> p[i]; vector<vector<int>> v(n); for (int i = 0; i < m; ++i) v[b[i]].push_back(i); vector<vector<int>> ed(m, vector<int>(m, oo)); for (int i = 0; i < m; ++i){ int cnt = 0; for (int j = b[i]; j < n; j += p[i]) { for (auto to : v[j]) ed[i][to] = min(ed[i][to], cnt); cnt++; } cnt = 0; for (int j = b[i]; j >= 0; j -= p[i]){ for (auto to : v[j]) ed[i][to] = min(ed[i][to], cnt); cnt++; } } vector<int> d(m, oo); d[0] = 0; vector<bool> visited(m); for (int i = 0; i < m; ++i){ int mn = -1; for (int j = 0; j < m; ++j){ if (!visited[j] && (mn == -1 || d[mn] > d[j])) mn = j; } if (mn == -1) break; visited[mn] = true; for (int j = 0; j < m; ++j){ if (visited[j]) continue; if (d[j] > d[mn] + ed[mn][j]) d[j] = d[mn] + ed[mn][j]; } } int ans = d[1]; if (ans == oo) 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...