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