# UVA - 1347 Tour 双调欧几里得旅行商问题

dp[i][i - 1] = min(dp[i][i-1], dp[i-1][k] + dis(k,j))

``````#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int N = 1010;
const double INF = 0x3f3f3f3f3f3f3f3f;
double dp[N][N], dis[N][N];
int n;
struct point{
double x, y;
}P[N];

double distance(int a, int b) {
return sqrt( (P[a].x - P[b].x) * (P[a].x - P[b].x) + (P[a].y - P[b].y) * (P[a].y - P[b].y));
}

void init() {
for(int i = 1; i <= n; i++)
scanf("%lf%lf", &P[i].x, &P[i].y);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
dis[i][j] = dis[j][i] =  distance(i,j);
}

double solve() {
dp[2][1] = dis[2][1];

for(int i = 3; i <= n; i++) {
dp[i][i-1] = INF;
for(int j = 1; j < i - 1; j++) {
dp[i][i-1] = min(dp[i][i-1], dp[i-1][j] + dis[j][i]);
dp[i][j] = dp[i-1][j] + dis[i][i-1];
}
}

double ans = INF;
for(int i = 1; i < n; i++)
ans = min(ans, dp[n][i] + dis[i][n]);
return ans;
}

int main() {
while(scanf("%d", &n) != EOF && n) {
init();
printf("%.2lf\n", solve());
}
return 0;
}
``````

UVA - 1347 Tour 双调欧几里得旅行商问题

(0)
(0)

0条