Loading

UVA 1347 Tour

题意:按x从小到大给出n个点,要求一条从最左走到最右再回到最左的路径最小值 要求走过每个点

Solution by Rujia,liu

#include<iostream>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<queue>
#include<stack>
#include<list>
#include<sstream>
#include<cstdio>
#define INF 0x3f3f3f3f
//const int maxn = 1e6 + 5;
const double PI = acos(-1.0);
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

const int maxn = 50 + 5;
double x[maxn], y[maxn], dist[maxn][maxn], d[maxn][maxn];

int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        for (int i = 1; i <= n; i++) scanf("%lf%lf", &x[i], &y[i]);
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) dist[i][j] = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
        }

        for (int i = n - 1; i >= 2; i--) {
            for (int j = 1; j < i; j++) {
                if (i == n - 1) d[i][j] = dist[i][n] + dist[j][n];
                else d[i][j] = min(dist[i][i + 1] + d[i + 1][j], dist[j][i + 1] + dist[i + 1][i]);
            }
        }
        printf("%.2f\n", dist[1][2] + d[2][1]);
    }
    return 0;
}

 

posted @ 2020-02-20 16:29  MQFLLY  阅读(148)  评论(0编辑  收藏  举报