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;
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(it, s) for (__typeof(s.begin()) it = (s).begin(); it != (s).end(); ++it)
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define file(TASK) \
if (fopen(TASK ".inp", "r")) { \
freopen(TASK ".inp", "r", stdin); \
freopen(TASK ".out", "w", stdout); \
}
template <class U, class V> bool maximize(U &A, const V &B) { return (A < B) ? (A = B, true) : false; }
template <class U, class V> bool minimize(U &A, const V &B) { return (A > B) ? (A = B, true) : false; }
const int MAXN = 7e6 + 5;
const int S = 200;
int N, M, source, sink, dp[MAXN];
vector <pair <int, int>> ke[MAXN];
void add_edges(int i, int j, int w, bool tmp = true) {
ke[i].push_back(make_pair(j, w));
if(tmp) ke[j].push_back(make_pair(i, w));
}
void process(void) {
cin >> N >> M;
auto ID = [&] (int i, int j) -> int { return (i - 1) * S + j; };
FOR(v, 1, M) {
int b, p; cin >> b >> p; b++;
if(v == 1) source = b;
if(v == 2) sink = b;
if(p >= S) {
for (int i = b + p; i <= N; i += p) add_edges(i, b, (i - b) / p);
for (int i = b - p; i >= 1; i -= p) add_edges(i, b, (b - i) / p);
} else add_edges(b, N + ID(b, p), 0, false);
}
FOR(i, 1, N) FOR(j, 1, S) add_edges(N + ID(i, j), i, 0, false);
FOR(i, 1, N) FOR(j, 1, S - i) {
add_edges(N + ID(i, j), N + ID(i + j, j), 1);
}
memset(dp, 0x3f, sizeof dp);
dp[source] = 0;
priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> q;
q.push(make_pair(0, source));
while((int) q.size()) {
auto [w1, u] = q.top(); q.pop();
if(dp[u] != w1) continue;
for (auto [v, w] : ke[u]) if(minimize(dp[v], dp[u] + w)) {
q.push(make_pair(dp[v], v));
}
}
cout << (dp[sink] >= 1e9 ? -1 : dp[sink]);
}
signed main() {
file("TASK");
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int test = 1;
// cin >> test;
while(test--) {
process();
cout << '\n';
}
cerr << "Time elapsed: " << TIME << " s.\n";
return (0 ^ 0);
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:17:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen(TASK ".inp", "r", stdin); \
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:69:5: note: in expansion of macro 'file'
69 | file("TASK");
| ^~~~
skyscraper.cpp:18:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen(TASK ".out", "w", stdout); \
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:69:5: note: in expansion of macro 'file'
69 | file("TASK");
| ^~~~
# | 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... |