#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;
int x[1010],y[1010],n;
double dp[1010][1010],d[1010][1010];
double dfs(int t1,int t2){
if(dp[t1][t2]) return dp[t1][t2];
if(t1 == n-1) return dp[t1][t2]=d[t1][n]+d[t2][n];
dp[t1][t2]=min(dfs(t1+1,t2)+d[t1][t1+1],dfs(t1+1,t1)+d[t2][t1+1]);
return dp[t1][t2];
}
int main(){
while(scanf("%d",&n) == 1){
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]);
for(int i=1;i<=n;i++){
for(int j=i;j<=n;j++){
d[i][j]=d[j][i]=sqrt( (double)((x[i]-x[j]) * (x[i]-x[j]) + (y[i]-y[j]) * (y[i]-y[j]) ));
}
}
printf("%.2f\n",dfs(2,1)+d[1][2]);
}
}
平面n个点 求从最左边点到最右边点再返回的最短路长 uva 1347
最新推荐文章于 2020-01-10 11:01:59 发布