UVA 1347 Tour

Posted hznumqf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
", dist[1][2] + d[2][1]);
    }
    return 0;
}

 

以上是关于UVA 1347 Tour的主要内容,如果未能解决你的问题,请参考以下文章

UVa 1347 Tour

Tour UVA - 1347

UVA - 1347 Tour

uva1347 Tour

uva 1347 tour

Chapter_9 DP : uva1347 tour (bitonic tour)