이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const short N = 3e4 + 5;
#define eb emplace_back
struct node {
short w, u, s;
node() {
w = u = s = 0;
}
node(short w, short u, short s) : w(w), u(u), s(s) {}
friend bool operator > (node a, node b) {
return a.w > b.w;
}
};
short n, m;
short dist[N][200];
vector<short> adj[N];
priority_queue<node, vector<node>, greater<node>> Q;
short start, finish;
bool vis[N];
void update(short u, short v, short w, short cur, short siu) {
if(dist[v][siu] > dist[u][cur] + w) {
dist[v][siu] = dist[u][cur] + w;
Q.emplace(dist[v][siu], v, siu);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
short SQRT = sqrt(n);
for(short i = 0; i < m; i++) {
short u, v;
cin >> u >> v;
adj[u].eb(v);
if(i == 0)
start = u;
if(i == 1)
finish = u;
}
memset(dist, 0x3f, sizeof dist);
dist[start][0] = 0;
Q.emplace(0, start, 0);
while(!Q.empty()) {
node top = Q.top();
Q.pop();
short u = top.u;
short cur = top.s;
if(dist[u][cur] != top.w)
continue;
if(u == finish)
return cout << dist[finish][cur], 0;
if(cur) {
if(u + cur <= n)
update(u, u + cur, 1, cur, cur);
if(u - cur >= 0)
update(u, u - cur, 1, cur, cur);
}
if(vis[u])
continue;
vis[u] = true;
for(short v : adj[u]) {
if(v >= SQRT) {
for(short j = 1; u + j * v < n; j++)
update(u, u + j * v, j, cur, 0);
for(short j = 1; u - j * v >= 0; j++)
update(u, u - j * v, j, cur, 0);
} else {
if(u + v <= n)
update(u, u + v, 1, cur, v);
if(u - v >= 0)
update(u, u - v, 1, cur, v);
}
}
}
cout << -1;
}
# | 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... |