#include <iostream>
#include <cstdlib>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
typedef pair<int, int> PII;
const int N = 2050, M = N * N;
int n = 2021;
int e[M], ne[M], h[N], idx, w[M];
int dist[N], st[N];
void add(int a, int b, int c) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++;
}
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
int dijkstra() {
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({0, 1});
while(heap.size()) {
auto t = heap.top();
heap.pop();
int ver = t.second, distance = t.first;
if(st[ver]) continue;
st[ver] = 1;
for(int i = h[ver]; i != -1; i = ne[i]) {
int j = e[i];
if(dist[j] > dist[ver] + w[i]) {
dist[j] = dist[ver] + w[i];
heap.push({dist[j], j});
}
}
}
if(dist[n] == 0x3f3f3f3f) return -1;
return dist[n];
}
int main() {
memset(h, -1, sizeof h);
for(int i = 1; i <= n; i ++ )
for(int j = i + 1; j <= n; j ++ )
if(abs(i - j) <= 21) {
add(i, j, i * j / gcd(i, j));
add(j, i, i * j / gcd(i, j));
}
int ans = dijkstra();
cout << ans << endl;
return 0;
}
路径 蓝桥杯
最新推荐文章于 2024-09-05 15:48:42 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)