이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
}
al[x*N + i].emplace_back(i, 0);
}
}
FOR(i,0,M-1){
int B, P; cin >> B >> P;
if (P <= SQ) {
al[B].emplace_back(P*N + B, 0);
} else {
doge[B].push_back(P);
}
}
memset(dist,-1,sizeof dist);
dist[0] = 0;
pq.emplace(0,0);
while (!pq.empty()) {
int u = pq.top().sc, w = pq.top().fi; pq.pop();
if (w > dist[u]) continue;
if (u == 1) 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[1] << '\n';
}
# | 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... |