POJ2728最优比率生成树

53 篇文章 1 订阅
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;

const int NN=1002;
const double inf=1000000000;
const double eps=1e-5;

typedef struct point{
    double x,y,h;
}point;
int n;
point p[NN];
double l,r,mid;
double dis[NN][NN];

void get_dis()
{
    for (int i=1; i<=n; i++)
      for (int j=1; j<=n; j++)
      {
         dis[i][j]=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));
         if (dis[i][j]<d1) d1=dis[i][j];
      }
}

double lowcost[NN];
bool intree[NN];
bool prim()
{
    double sum=0.0;
    int cur=1;
    lowcost[cur]=0;
    intree[cur]=true;
    for (int i=2; i<=n; i++) { lowcost[i]=inf; intree[i]=false; }
    for (int t=1; t<n; t++)
    {
        double min_cost=inf;
        int k;
        for (int i=1; i<=n; i++)
        {
            if (!intree[i])
            {
                if (lowcost[i]>fabs(p[cur].h-p[i].h)-mid*dis[cur][i])
                    lowcost[i]=fabs(p[cur].h-p[i].h)-mid*dis[cur][i];
                if (lowcost[i]<min_cost) min_cost=lowcost[k=i];
            }
        }
        sum+=min_cost;
        intree[k]=true;
        cur=k;
    }
    if (sum<=eps) return true;
    else          return false;
}

void solve()
{
    get_dis();
    l=0.0; r=1000.0;
    while (r-l>eps)
    {
        mid=(l+r)*0.5;
        if (prim()) r=mid;
        else        l=mid;
    }
    printf("%.3f\n",mid);  //我会告诉你因为自作多情在这儿加了ep而WA了好几次么?不懂精度就不要乱搞。。。。。。  
}

int main()
{
    while (scanf("%d",&n),n)
    {
        for (int i=1; i<=n; i++) scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].h);
        solve();
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值