Submission #248518

#TimeUsernameProblemLanguageResultExecution timeMemory
248518sahil_kJakarta Skyscrapers (APIO15_skyscraper)C++14
100 / 100
195 ms65764 KiB
#include <iostream> #include <vector> #include <map> #include <set> #include <cmath> #include <queue> using namespace std; /* int n, m; set<int> inc[30100]; vector< pair<int, int> > adj[2000000]; int dist[2000000]; bool vis[2000000]; priority_queue< pair<int, int> > ord; int main () { cin >> n >> m; int rt = sqrt(n); int start, end; for (int i=0; i<n; i++) { for (int j=1; j<rt; j++) { adj[i*rt+j].push_back({i*rt, 0}); if (i-j >= 0) adj[i*rt+j].push_back({(i-j)*rt+j, 1}); if (i+j < n) adj[i*rt+j].push_back({(i+j)*rt+j, 1}); } } int bi, pi; for (int i=0; i<m; i++) { cin >> bi >> pi; if (i == 0) start = bi; if (i == 1) end = bi; if (pi < rt) { adj[bi*rt].push_back({bi*rt+pi, 0}); } else { for (int j=1; j<n; j++) { if (bi-j*pi < 0 && bi+j*pi >= n) break; if (bi-j*pi >= 0) adj[bi*rt].push_back({(bi-j*pi)*rt, j}); if (bi+j*pi < n) adj[bi*rt].push_back({(bi+j*pi)*rt, j}); } } } for (int i=0; i<n*rt; i++) { dist[i] = 1e9; vis[i] = false; } dist[start*rt] = 0; ord.push({0, start*rt}); while (ord.size() > 0) { pair<int, int> cur = ord.top(); cur.first = -cur.first; ord.pop(); if (cur.second == end*rt) { cout << cur.first << endl; return 0; } if (vis[cur.second]) continue; vis[cur.second] = true; for (auto i: adj[cur.second]) { if (!vis[i.first] && cur.first+i.second < dist[i.first]) { dist[i.first] = cur.first+i.second; ord.push({-dist[i.first], i.first}); } } } cout << -1 << endl; } */ int n, m; set<int> inc[30100]; vector< pair<int, int> > adj[30100]; int dist[30100]; bool vis[30100]; priority_queue< pair<int, int> > pq; int main () { cin >> n >> m; int bi, pi; int start, end; for (int i=0; i<m; i++) { cin >> bi >> pi; inc[bi].insert(pi); if (i == 0) start = bi; if (i == 1) end = bi; } for (int i=0; i<n; i++) { for (auto j: inc[i]) { for (int k=1; k<n; k++) { if (i-j*k < 0) break; if (inc[i-j*k].find(j) != inc[i-j*k].end()) { adj[i].push_back({i-j*k, k}); break; } adj[i].push_back({i-j*k, k}); } for (int k=1; k<n; k++) { if (i+j*k >= n) break; if (inc[i+j*k].find(j) != inc[i+j*k].end()) { adj[i].push_back({i+j*k, k}); break; } adj[i].push_back({i+j*k, k}); } } } for (int i=0; i<n; i++) { dist[i] = 1e9; } dist[start] = 0; pq.push({0, start}); while (pq.size() > 0) { pair<int, int> cur = pq.top(); cur.first = -cur.first; pq.pop(); if (cur.second == end) { cout << cur.first << endl; return 0; } if (vis[cur.second]) continue; vis[cur.second] = true; for (auto i: adj[cur.second]) { if (!vis[i.first] && cur.first+i.second < dist[i.first]) { dist[i.first] = cur.first+i.second; pq.push({-dist[i.first], i.first}); } } } cout << -1 << endl; }

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:113:3: warning: 'end' may be used uninitialized in this function [-Wmaybe-uninitialized]
   if (cur.second == end) {
   ^~
#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...