이 제출은 이전 버전의 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 2020
#define SQRT 180
#define BLK (MAXN / SQRT) + 10
int n, m;
int B[MAXN], p[MAXN];
vector<int> hab[MAXN];
bool mark[MAXN][SQRT];
int dist[2 * MAXN][SQRT];
vector<pair<pii, int>> adj[2 * MAXN][SQRT];
bool anymark[MAXN];
inline void addedge(int x1, int y1, int x2, int y2, int w)
{
adj[x1][y1].pb({{x2, y2}, w});
}
void dij(void)
{
priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> s;
FOR(i, 0, 2 * MAXN) fill(dist[i], dist[i] + SQRT, 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 p = pr.sc.sc;
if (dist[v][p] != pr.fr) continue;
for (auto pu : adj[v][p])
{
int u = pu.fr.fr;
int up = pu.fr.sc;
int w = pu.sc;
if (dist[v][p] + w >= dist[u][up]) continue;
dist[u][up] = dist[v][p] + w;
s.push({dist[u][up], {u, up}});
}
}
}
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, anymark[B[i]] = true;
}
FOR(i, 1, n + 1)
{
FOR(j, 1, SQRT)
{
addedge(i, j, i, 0, 0);
if (mark[i][j]) addedge(i, 0, i, j, 0);
if (i + j <= n) addedge(i, j, i + j, j, 1);
if (i - j >= 1) addedge(i, j, i - j, j, 1);
}
for (int u : hab[i]) if (p[u] >= SQRT)
{
addedge(i, 0, n + u, 0, 0);
}
}
FOR(i, 1, m + 1) if (p[i] >= SQRT)
{
for (int j = B[i]; j <= n; j += p[i])
{
addedge(i + n, 0, j, 0, (j - B[i]) / p[i]);
}
for (int j = B[i] - p[i]; j >= 1; j -= p[i])
{
addedge(i + n, 0, j, 0, (B[i] - j) / p[i]);
}
}
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... |