# include<iostream>
# include<cmath>
using namespace std;
const int P=99999999;
long long minm[2022][2022];
int visit[2022] = { 0 };
long long grd(long long a,long long b) {
if (b == 0) {
return a;
}
else {
long long c;
c = a % b;
a = b;
b = c;
return grd(a,b);
}
}//最大公约数,用除数去除余数,直到余数为0时,则除数即为最大公约数
int main() {
int i, j;
for (i = 1; i <= 2021; i++) {
for (j = 1; j <= 2021; j++) {
if (i == j) {
minm[i][j] = 0;
minm[j][i] = 0;
}
else if(abs(i-j)<=21) {
minm[i][j] = i * j / grd(i, j);
minm[j][i] = i * j / grd(i, j);
}
else {
minm[i][j] = P;
minm[j][i] = P;
}
}
}
//for(i=1;i<=2021;i++)
// for(j=i+1;j<=2021;j++)
// for (int k = i; k <= j; k++) {
// if (minm[i][k] != P && minm[k][j] != P && (minm[i][j] > minm[i][k] + minm[k][j])) {
// minm[i][j] = minm[i][k] + minm[k][j];
// }
// }
visit[1] = 1;
int k = 1;
for (i = 1; i <= 2021;i++) {
int j = 1, u = -1, min = P;
for (j = 1; j <= 2021; j++) {
if(!visit[j]&&minm[k][j]<min){
u = j;
min = minm[k][j];
}
}//找到起点与相邻点最短路径
if (u==-1)
{
break;
}
visit[u] = 1;
minm[k][u] = min;
for (j = 1; j <= 2021; j++) {
if (!visit[j] && minm[u][j] && minm[k][j] > minm[k][u] + minm[u][j]) {
minm[k][j] = minm[k][u] + minm[u][j];
}
}
}
cout << minm[1][2021] << endl;
return 0;
}
注释了的是floyd算法,时间较慢,如果就直接运行则是dijkastra算法,时间复杂度更小。