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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, int> ri3;
#define mp make_pair
#define pb push_back
#define fi first
#define sc second
#define SZ(x) (int)(x).size()
#define ALL(x) begin(x), end(x)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)
const int MX_N = 30000;
const int MX_M = 30000;
const int MX_P = 30000;
const int SQ = sqrt(MX_P);
int N, M;
vector<ii> al[MX_N*(SQ+1)];
vector<int> doge[MX_M];
int dist[MX_N*(SQ+1)];
priority_queue<ii, vector<ii>, greater<ii> > pq;
int main() {
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int N, M; cin >> N >> M;
FOR(x,1,SQ){
FOR(i,0,N-1){
if (i+x < N) {
al[x*N + i].emplace_back(x*N + i+x, 1);
al[x*N + i+x].emplace_back(x*N + i, 1);
//cout << "JOIN " << x << " :: " << i << " " << i+x << endl;
}
al[x*N + i].emplace_back(i, 0);
}
}
int S, T;
FOR(i,0,M-1){
int B, P; cin >> B >> P;
if (i == 0) S = B;
else if (i == 1) T = B;
if (P <= SQ) {
al[B].emplace_back(P*N + B, 0);
} else {
doge[B].push_back(P);
}
}
memset(dist,-1,sizeof dist);
dist[S] = 0;
pq.emplace(0,S);
while (!pq.empty()) {
int u = pq.top().sc, w = pq.top().fi; pq.pop();
if (w > dist[u]) continue;
//cout << "u " << u/N << " : " << u%N << " w " << w << endl;
if (u == T) break;
for (auto e : al[u]) {
int v = e.fi, w = dist[u] + e.sc;
if (dist[v] == -1 || w < dist[v]) {
dist[v] = w;
pq.emplace(dist[v], v);
}
}
if (u < N) {
for (auto p : doge[u]) {
for (int i = 1; u - i*p >= 0; ++i) {
int v = u - i*p, w = dist[u] + i;
if (dist[v] == -1 || w < dist[v]) {
dist[v] = w;
pq.emplace(dist[v], v);
}
}
for (int i = 1; u + i*p < N; ++i) {
int v = u + i*p, w = dist[u] + i;
if (dist[v] == -1 || w < dist[v]) {
dist[v] = w;
pq.emplace(dist[v], v);
}
}
}
}
}
cout << dist[T] << '\n';
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:68:9: warning: 'T' may be used uninitialized in this function [-Wmaybe-uninitialized]
if (u == T) break;
^~
# | 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... |