POJ 2728 Desert King 0-1分数规划

题意:链接

方法: 0-1分数规划

解析:

这是之前没填的坑,现在来填坑。

这道题题意就是n个三维坐标系的点,任意两点都可以连边,每条边的花费是两点在xOy坐标系下的欧几里得距离,每条边的收益是两点的z值差的绝对值。

n个点连成一棵树

求最小的花费比收益。

即求最大的收益比花费。

一看求的东西就可以考虑0-1分数规划咯?

所以二分那个L,然后每条边的get-L*cost就是每条边新的权值,因为要拿最大的n-1条,所以上MST,但是这题是n^2的边,kruscal的话是n^2logn^2*log(二分)应该是T死的。

所以说稠密图上prim嘛。

每次拿值就行辣。


下午惊奇的发现,我之前写的什么玩意,值还重新计算一下,那个二分明明分的就是最终的答案,我干嘛要重新算….

所以直接拿二分最后的值就行了,但是好像有点精度问题?

1e-6拿l是不行的,差在精度,拿r反倒行- -科学性呢!

1e-10的话精度拿l就够了,还不T,正好。

然而代码的话,懒得改了,把过程求值那些玩意删了就好了,然后直接拿二分的最终答案。

代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 1010
#define eps 1e-6
#define INF 0x3f3f3f3f
using namespace std;
int n;
double top,bot;
struct point
{
    double x,y,z;
}pt[N];
struct line
{
    int x,y;
    double get,cost,l;
}l[N*N];
int cmp(line a,line b)
{
    return a.l>b.l; 
}
double thedis[N];
int theclo[N];
double function[N][N];
double get[N][N];
double cost[N][N];
double prim(int u)
{
    double ret=0;
    for(int i=1;i<=n;i++)
    {
        if(i==u)thedis[i]=-1,theclo[i]=-1;
        else thedis[i]=function[u][i],theclo[i]=u;
    }
    for(int i=1;i<n;i++)
    {
        double val=-10000000;
        int no;
        for(int j=1;j<=n;j++)
        {
            if(theclo[j]!=-1&&thedis[j]>val)val=thedis[j],no=j;
        }
        ret+=thedis[no];
        top+=get[theclo[no]][no];
        bot+=cost[theclo[no]][no];
        theclo[no]=-1;
        for(int j=1;j<=n;j++)
        {
            if(theclo[j]!=-1&&function[no][j]>thedis[j])thedis[j]=function[no][j],theclo[j]=no; 
        } 
    }
    return ret;
}
int main()
{
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        for(int i=1;i<=n;i++)scanf("%lf%lf%lf",&pt[i].x,&pt[i].y,&pt[i].z);
        int linetot=0;
        double l=0,r=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i==j)continue;
                cost[i][j]=fabs(pt[i].z-pt[j].z);
                get[i][j]=sqrt((pt[i].x-pt[j].x)*(pt[i].x-pt[j].x)+(pt[i].y-pt[j].y)*(pt[i].y-pt[j].y));
                r=max(r,get[i][j]/cost[i][j]);
            }
        }
        double ans=0;
        while(r-l>eps)
        {
            double mid=(l+r)/2.0;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(i==j)continue;
                    function[i][j]=get[i][j]-mid*cost[i][j];
                }
            }
            top=0,bot=0;
            double ret=prim(1);
            if(ret-eps>=0)ans=max(ans,top/bot),l=mid+eps;
            else r=mid-eps;
        }
        printf("%.3lf\n",1.0/ans); 
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值