# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
674469 | AmirH | Jakarta Skyscrapers (APIO15_skyscraper) | C++14 | 1081 ms | 13132 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<bitset>
#include<queue>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<unordered_map>
#include<list>
#include<utility>
#include<stack>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
#define cl clear
#define F first
#define S second
#define pb push_back
#define Sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define faster ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const int MAX_N = 2e5 + 25;
const ll INF = 1e18;
const int inf = 1e9;
const int mod = 1e9 + 7;
void free() {
#ifndef ONLINE_JUDGE
freopen("inputi.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
}
int n, m;
int dist[MAX_N];
set<int> v[MAX_N];
pii ar[MAX_N];
void dij(int pos) {
set<pii> s;
s.insert({0, pos});
dist[pos] = 0;
while(s.size()) {
int x = s.begin()->first;
int y = s.begin()->second;
v[ar[y].first].erase(y);
s.erase(s.begin());
int d = 0;
for(int i = ar[y].first; i < n; i+= ar[y].second) {
for(auto j : v[i]) {
if(x + d < dist[j]) {
s.erase({dist[j], j});
dist[j] = x + d;
s.insert({dist[j], j});
}
}
d++;
}
d = 0;
for(int i = ar[y].first; i >= 0; i-= ar[y].second) {
for(auto j : v[i]) {
if(x + d < dist[j]) {
s.erase({dist[j], j});
dist[j] = x + d;
s.insert({dist[j], j});
}
}
d++;
}
}
}
int main() {
faster; //free();
cin >> n >> m;
for(int i = 1; i <= m; i++)
dist[i] = inf;
for(int i = 1; i <= m; i++) {
cin >> ar[i].first >> ar[i].second;
v[ar[i].first].insert(i);
}
dij(1);
if(dist[2] == inf)
cout << -1;
else
cout << dist[2];
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |