#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,pii> plii;
const int MAXN = 3e4+10;
const int BLK = 5000;
#define dist first
#define index second.first
#define delta second.second
int N, M, X, Y;
vector<pii> v[MAXN];
ll d[MAXN][BLK];
map<pii, bool> ok;
priority_queue<plii, vector<plii>, greater<plii>> q;
ll dBig[MAXN];
int posBig[MAXN];
inline void proc(int i, int dx, ll dd) {
if (dx < BLK) {
if (d[i][dx] == -1 || dd < d[i][dx]) {
d[i][dx] = dd;
q.push({dd, {i, dx}});
}
} else {
if (dBig[i] == -1 || dd < dBig[i]) {
dBig[i] = dd;
q.push({dd, {i, dx}});
}
}
}
inline void procBig(int cur, int dx, ll d0) {
// int cnt = cur/dx;
// int i = cur - cnt*dx;
// while (i < N) {
// if (i != cur) proc(i, 0, d0 + abs(cnt));
// i += dx; cnt--;
// }
int i = cur, cnt = 0;
while ((i-=dx) >= 0) proc(i, 0, d0 + (++cnt));
i = cur, cnt = 0;
while ((i+=dx) < N) proc(i, 0, d0 + (++cnt));
}
inline void procSmall(int cur, int dx, ll d0) {
if (cur - dx >= 0) {
proc(cur-dx, dx, d0+1);
proc(cur-dx, 0, d0+1);
}
if (cur + dx < N) {
proc(cur+dx, dx, d0+1);
proc(cur+dx, 0, d0+1);
}
}
inline void proc0(int ix) {
for (pii p : v[ix])
proc(p.first<BLK?ix:p.second, p.first, d[ix][0]);
}
void dijkstra() {
memset(d, -1, sizeof(d));
d[X][0] = 0; q.push({0,{X,0}});
while (!q.empty()) {
plii p = q.top(); q.pop();
if (p.delta < BLK && d[p.index][p.delta] < p.dist) continue;
if (p.delta >= BLK && dBig[p.index] < p.dist) continue;
if (p.delta == 0) proc0(p.index);
else if (p.delta < BLK) procSmall(p.index, p.delta, p.dist);
else procBig(posBig[p.index], p.delta, p.dist);
}
}
int main()
{
scanf("%d %d", &N, &M);
for (int i = 0,a,b; i < M; i++) {
scanf("%d %d", &a, &b);
if (i == 0) X = a;
else if (i == 1) Y = a;
if (b != 0) v[a].push_back(pii(b,i));
posBig[i] = a;
}
for (int i = 0; i < N; i++)
sort(v[i].begin(), v[i].end()),
v[i].resize(unique(v[i].begin(), v[i].end()) - v[i].begin());
dijkstra();
printf("%lld\n", d[Y][0]);
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:85:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &M);
^
skyscraper.cpp:87:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
0 ms |
1852 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
0 ms |
1852 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
0 ms |
1852 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
0 ms |
1852 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
0 ms |
1852 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |