이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'
#define MAXN 30100
#define SQRT 400
#define BLK (MAXN / SQRT) + 10
int n, m;
int B[MAXN], p[MAXN];
vector<int> hab[MAXN];
bool mark[MAXN][SQRT];
int dist[MAXN][SQRT + 1];
priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> s;
inline void relax(int v, int p, int u, int up, int w)
{
if (dist[v][p] + w < dist[u][up])
{
dist[u][up] = dist[v][p] + w;
s.push({dist[u][up], {u, up}});
}
}
void dij(void)
{
FOR(i, 0, MAXN) fill(dist[i], dist[i] + SQRT + 1, INF);
dist[B[1]][0] = 0;
s.push({0, {B[1], 0}});
while (!s.empty())
{
auto pr = s.top();
s.pop();
int v = pr.sc.fr;
int pw = pr.sc.sc;
if (dist[v][pw] != pr.fr) continue;
// relax others
if (pw == SQRT) // people
{
for (int j = B[v]; j <= n; j += p[v])
{
relax(v, pw, j, 0, (j - B[v]) / p[v]);
}
for (int j = B[v] - p[v]; j >= 1; j -= p[v])
{
relax(v, pw, j, 0, (B[v] - j) / p[v]);
}
}
else // building
{
if (pw == 0)
{
FOR(j, 1, SQRT) if (mark[v][j])
relax(v, pw, v, j, 0);
for (int u : hab[v]) if (p[u] >= SQRT)
relax(v, pw, u, SQRT, 0);
}
else
{
relax(v, pw, v, 0, 0);
if (v + pw <= n) relax(v, pw, v + pw, pw, 1);
if (v - pw >= 1) relax(v, pw, v - pw, pw, 1);
}
}
}
}
int32_t main(void)
{
fast_io;
cin >> n >> m;
FOR(i, 1, m + 1)
{
cin >> B[i] >> p[i];
B[i]++;
hab[B[i]].pb(i);
if (p[i] < SQRT) mark[B[i]][p[i]] = true;
}
dij(); // from (B[1], 0)
if (dist[B[2]][0] >= INF) cout << "-1\n";
else cout << dist[B[2]][0] << endl;
return 0;
}
# | 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... |