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

POJ 2728 Desert King 0-1分数规划

时间:2015-08-17 14:01:29      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:poj   三维   

题意:链接

方法: 0-1分数规划

解析:

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

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

n个点连成一棵树

求最小的花费比收益。

即求最大的收益比花费。

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

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

所以说稠密图上prim嘛。

每次拿值就行辣。

代码:

#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); 
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 2728 Desert King 0-1分数规划

标签:poj   三维   

原文地址:http://blog.csdn.net/wzq_qwq/article/details/47724087

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