﻿ Poj 2677 Tour//DP - 鸿网互联

# Poj 2677 Tour//DP

/*Poj 2677 TourDP 双调欧几里得旅行商问题转移方程 :当j = i - 1: d[i][i-1] = min{d[i-1][k] + p[i][k]} (1=ki-1)当j i - 1: d[i][j] = d[i-1][j] + p[i-1][i]*/#includecstdio#includecstring#includecmath#define Min(a,b) (ab?a:b)using namespace std;
```/*Poj 2677 Tour
DP 双调欧几里得旅行商问题

*/
#include<cstdio>
#include<cstring>
#include<cmath>
#define Min(a,b) (a<b?a:b)
using namespace std;
const int maxn = 205;
const double INF = 1000000000.0;
double d[maxn][maxn];
struct Points
{
int x;
int y;
} P[maxn];
int n;
double getDis(int i,int j)
{
double x = P[i].x - P[j].x;
double y = P[i].y - P[j].y;
return sqrt(x*x + y*y);
}
void input()
{
for(int i = 1; i <= n; i++)
scanf("%d%d",&P[i].x,&P[i].y);
}
void init()
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++) d[i][j] = INF;
}
}
void solve()
{
d[2][1] = getDis(2, 1);
for(int i = 2; i< n; i++)
for(int j = 1; j < i; j++) {
d[i + 1][j] = d[i][j] + getDis(i, i + 1);
d[i + 1][i] = Min(d[i + 1][i], d[i][j] + getDis(j, i + 1));
}
double re = d[n][n-1] + getDis(n,n-1);
printf("%.2f\n", re);
}
int main()
{
while(scanf("%d",&n) != EOF)
{
input();
init();
solve();
}
return 0;
}```

<