Submission #14396

#TimeUsernameProblemLanguageResultExecution timeMemory
14396gs14004Jakarta Skyscrapers (APIO15_skyscraper)C++14
22 / 100
220 ms262144 KiB
#include <cstdio> #include <map> #include <vector> #include <algorithm> #include <utility> #include <queue> using namespace std; typedef pair<int,int> pi; int m, n; vector<pi> vertex; vector<int> dummy[3005]; bool r_dir[30005][105], l_dir[30005][105]; vector<vector<pi> > graph; priority_queue<pi,vector<pi>,greater<pi> > pq; bool vis[12300005]; int dijkstra(int st, int ed){ pq.push(pi(0,st)); while (!pq.empty()) { pi t = pq.top(); pq.pop(); if(t.second == ed){ return t.first; } if(vis[t.second]) continue; vis[t.second] = 1; for (auto &i : graph[t.second]){ if(!vis[i.second]){ pq.push(pi(i.first + t.first,i.second)); } } } return -1; } int main(){ int st, ed; scanf("%d %d",&n,&m); for (int i=0; i<m; i++) { int x,y; scanf("%d %d",&x,&y); if(i == 0){ st = x; } else if(i == 1){ ed = x; } if(y <= 100){ r_dir[x][y] = 1; l_dir[x][y] = 1; } else{ for (int j=0; j * (y) + x < n; j++) { vertex.push_back(pi(j * y + x,y)); } for (int j=0; j * (y) + x >= 0; i--) { vertex.push_back(pi(j * y + x,-y)); } } dummy[x].push_back(y); } for (int i=1; i<=100; i++) { for (int j=i; j<n; j++) { r_dir[j][i] |= r_dir[j - i][i]; } for (int j=n-i-1; j>=0; j--) { l_dir[j][i] |= l_dir[j + i][i]; } for (int j=0; j<n; j++) { if(r_dir[j][i]){ vertex.push_back(pi(j,i)); } if(l_dir[j][i]){ vertex.push_back(pi(j,-i)); } } } sort(vertex.begin(),vertex.end()); vertex.resize(unique(vertex.begin(),vertex.end()) - vertex.begin()); graph.resize(n + vertex.size()); for (int i=0; i<n; i++) { for(auto &j : dummy[i]){ int pos = (int)(lower_bound(vertex.begin(),vertex.end(),pi(i,j)) - vertex.begin()); if(pos != vertex.size() && vertex[pos] == pi(i,j)){ graph[i].push_back(pi(0,pos+n)); } pos = (int)(lower_bound(vertex.begin(),vertex.end(),pi(i,-j)) - vertex.begin()); if(pos != vertex.size() && vertex[pos] == pi(i,-j)){ graph[i].push_back(pi(0,pos+n)); } } } for (int i=0; i<vertex.size(); i++){ pi tmp = vertex[i]; tmp.first += tmp.second; int np = (int)(lower_bound(vertex.begin(),vertex.end(),tmp) - vertex.begin()); graph[i+n].push_back(pi(0,vertex[i].first)); if(np == vertex.size() || vertex[np] != tmp) continue; graph[i+n].push_back(pi(1,np + n)); } printf("%d",dijkstra(st,ed)); }
#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...