Submission #251955

#TimeUsernameProblemLanguageResultExecution timeMemory
251955IgorIJakarta Skyscrapers (APIO15_skyscraper)C++17
36 / 100
1103 ms236280 KiB
const int LG = 21; const int N = 400005; const long long MOD = 1e9 + 7; const long long INF = 1e9; const long long INFLL = 1e18; #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vector<int> > vvi; typedef vector<ll> vll; #define forn(i, n) for (int (i) = 0; (i) != (n); (i)++) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define popcount(x) __builtin_popcount(x) #define popcountll(x) __builtin_popcountll(x) #define fi first #define se second #define re return #define pb push_back #define uniq(x) sort(all(x)); (x).resize(unique(all(x)) - (x).begin()) #ifdef LOCAL #define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl #define ln cerr << __LINE__ << endl #else #define dbg(x) void(0) #define ln void(0) #endif // LOCAL signed main() { int n, m; cin >> n >> m; vector<vi> dist(n, vector<int>(m, INF)); vector<int> b(m), p(m); for (int i = 0; i < m; i++) cin >> b[i] >> p[i]; deque<pii> q; int s = b[0], f = b[1]; for (int i = 0; i < m; i++) { if (b[i] == s) { dist[s][i] = 0; q.push_back({s, i}); } } vector<int> init(n); init[s] = 1; while (q.size()) { pair<int, int> x = q.front(); q.pop_front(); if (!init[x.first]) { for (int j = 0; j < m; j++) { if (b[j] == x.first && dist[b[j]][j] == INF) { dist[b[j]][j] = dist[x.first][x.second]; q.push_front({b[j], j}); } } init[x.first] = 1; } if (x.fi - p[x.se] >= 0 && dist[x.fi - p[x.se]][x.se] == INF) { dist[x.fi - p[x.se]][x.se] = dist[x.fi][x.se] + 1; q.push_back({x.fi - p[x.se], x.se}); } if (x.fi + p[x.se] < n && dist[x.fi + p[x.se]][x.se] == INF) { dist[x.fi + p[x.se]][x.se] = dist[x.fi][x.se] + 1; q.push_back({x.fi + p[x.se], x.se}); } } int ans = INF; for (int i = 0; i < m; i++) ans = min(ans, dist[f][i]); if (ans == INF) cout << -1; else cout << ans; } /* Note: Check constants at the beginning of the code. N is set to 4e5 but be careful in problems with large constant factor. Setting N in every problem is more effective. Check corner cases. N = 1 No def int long long for now. Add something here. */
#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...