栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 2728 Desert King

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 2728 Desert King

#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <cstdlib>using namespace std;#define P(a) printf("%lfn", (a))const int maxn = 1000 + 10;const double INF = 100000000;const double eps = 1e-8;double g[maxn][maxn], lowcost[maxn];bool vis[maxn];int n;int pre[maxn];struct Point{    double x, y;    double h;    Point(){}    Point(double x, double y):x(x), y(y){}}p[maxn];double prim(double l){    int k;    double temp, sum = 0, sum_h = 0, sum_d = 0;    memset(vis, false, sizeof(vis));    for(int i = 0; i < n; i++) pre[i] = 0;    vis[0] = true;    for(int i = 0; i < n; ++i)        lowcost[i] = fabs(p[0].h - p[i].h) - l * g[0][i];    for(int i = 0; i < n; ++i)    {        temp = INF;        for(int j = 0; j < n; ++j) if(!vis[j] && temp > lowcost[j])     temp = lowcost[k = j];        if(temp == INF) break;        vis[k] = true;        sum_h += fabs(p[k].h - p[pre[k]].h);        sum_d += g[k][pre[k]];        for(int j = 0; j < n; ++j) if(!vis[j] && lowcost[j] > fabs(p[k].h - p[j].h) - l * g[k][j]){     lowcost[j] = fabs(p[k].h - p[j].h) - l * g[k][j];     pre[j] = k; }    }    return sum_h / sum_d;}Point read_point(){    Point a;    scanf("%lf%lf%lf", &a.x, &a.y, &a.h);    return a;}double dist(int i, int j){    return sqrt((p[i].x - p[j].x) * (p[i].x - p[j].x) + (p[i].y - p[j].y) * (p[i].y - p[j].y));}int main(){    while(scanf("%d", &n) != EOF){        if(!n) break;        for(int i = 0; i < n; i++){ p[i] = read_point();        }        for(int i = 0; i < n; i++)        for(int j = 0; j < n; j++){ g[i][j] = dist(i, j);        }        double ans = 0, tmp;        while(1){ tmp = prim(ans); if(fabs(ans - tmp) < eps) break; ans = tmp;        }        printf("%.3lfn", ans);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/380136.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号