码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 1347 Tour

时间:2020-02-20 17:28:26      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:ble   turn   set   clu   lse   cstring   tor   要求   mat   

题意:按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;
}

 

UVA 1347 Tour

标签:ble   turn   set   clu   lse   cstring   tor   要求   mat   

原文地址:https://www.cnblogs.com/hznumqf/p/12336283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!