01分数规划poj2728(最优比例生成树)

Desert King
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 21766 Accepted: 6087

Description

David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be watered. As the dominate ruler and the symbol of wisdom in the country, he needs to build the channels in a most elegant way. 

After days of study, he finally figured his plan out. He wanted the average cost of each mile of the channels to be minimized. In other words, the ratio of the overall cost of the channels to the total length must be minimized. He just needs to build the necessary channels to bring water to all the villages, which means there will be only one way to connect each village to the capital. 

His engineers surveyed the country and recorded the position and altitude of each village. All the channels must go straight between two villages and be built horizontally. Since every two villages are at different altitudes, they concluded that each channel between two villages needed a vertical water lifter, which can lift water up or let water flow down. The length of the channel is the horizontal distance between the two villages. The cost of the channel is the height of the lifter. You should notice that each village is at a different altitude, and different channels can't share a lifter. Channels can intersect safely and no three villages are on the same line. 

As King David's prime scientist and programmer, you are asked to find out the best solution to build the channels.

Input

There are several test cases. Each test case starts with a line containing a number N (2 <= N <= 1000), which is the number of villages. Each of the following N lines contains three integers, x, y and z (0 <= x, y < 10000, 0 <= z < 10000000). (x, y) is the position of the village and z is the altitude. The first village is the capital. A test case with N = 0 ends the input, and should not be processed.

Output

For each test case, output one line containing a decimal number, which is the minimum ratio of overall cost of the channels to the total length. This number should be rounded three digits after the decimal point.

Sample Input

4
0 0 0
0 1 1
1 1 2
1 0 3
0

Sample Output

1.000
题意:有n个村庄,分别给出每个村庄的地理坐标(xi,yi)和海拔hi,要求在村庄之间修一些河道,使这些河道联通所有的村庄,不同的村庄由于海拔的差异需要修水泵,每个水泵的费用为这两个村庄的海拔差值,且每个水泵为一条河道所用,要求所有的费用/总的河道长度比率最小
分析:r=sigma(h[i][j])/sigma(l[i][j]),设R为最优值,则r>=R;(h[i][j]代表修每条河道的费用,l[i][j]代表每条河道的长度,sigma()为生成树里面的边)
即:sigma(h[i][j])/sigma(l[i][j])>=R,所以:h[r]=sigma(h[i][j])-r*sigma(l[i][j])>=0;
所以对于每一个r对应的h(r)的最小生成树>=0
当h(r)<0的时候减小r的值,否则增大r的值,逐渐二分使h(r)=0即可;
方法一:二分法
#include"stdio.h"
#include"string.h"
#include"math.h"
#define inf 0x3f3f3f3f
#define M 2009
#define eps 1e-7
struct node
{
    int x,y,h;
}p[M];
double dist(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));
}
double dis[M],G[M][M],L[M][M];
int use[M];
double dij(int n,int s)
{
    double sum=0;
    memset(use,0,sizeof(use));
    for(int i=1;i<=n;i++)
        dis[i]=inf;
    dis[s]=0;
    for(int i=1;i<=n;i++)
    {
        double mini=inf;
        int id=-1;
        for(int j=1;j<=n;j++)
        {
            if(!use[j]&&dis[j]<mini)
            {
                mini=dis[j];
                id=j;
            }
        }
        if(id==-1)break;
        sum+=dis[id];
        use[id]=1;
        for(int j=1;j<=n;j++)
        {
            if(!use[j]&&dis[j]>G[id][j])
                dis[j]=G[id][j];
        }
    }
    return sum;
}
double solve(int n,double r)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
            G[i][j]=G[j][i]=fabs(p[i].h*1.0-p[j].h*1.0)-L[i][j]*r+1000000000.0;
    }
    return dij(n,1)-(n-1)*1000000000.0;
}
int main()
{
    int n;
    while(scanf("%d",&n),n)
    {
        double l=0,r=0,mid;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].h);
            r+=p[i].h;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                L[i][j]=L[j][i]=dist(p[i],p[j]);
            }
        }
        while(r-l>eps)
        {
            mid=(l+r)/2;
            double msg=solve(n,mid);
            if(msg<0)
            {
                r=mid;
            }
            else
            {
                l=mid;
            }
        }
        printf("%.3lf\n",r);
    }
    return 0;
}

  方法二:迭代法

#include"stdio.h"
#include"string.h"
#include"math.h"
#define inf 0x3f3f3f3f
#define M 2009
#define eps 1e-6
struct node
{
    int x,y,h;
}p[M];
double dist(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));
}
double dis[M],G[M][M],L[M][M];
int use[M],pre[M];
double dij(int n,int s)
{
    double sum=0,cost=0,leng=0;
    memset(use,0,sizeof(use));
    for(int i=1;i<=n;i++)
    {
        dis[i]=inf;
        pre[i]=-1;
    }
    dis[s]=0;
    for(int i=1;i<=n;i++)
    {
        double mini=inf;
        int id=-1;
        for(int j=1;j<=n;j++)
        {
            if(!use[j]&&dis[j]<mini)
            {
                mini=dis[j];
                id=j;
            }
        }
        if(id==-1)break;
        sum+=dis[id];
        use[id]=1;
        if(pre[id]!=-1)
        {
            cost+=fabs(p[id].h*1.0-p[pre[id]].h);
            leng+=L[id][pre[id]];
        }
        for(int j=1;j<=n;j++)
        {
            if(!use[j]&&dis[j]>G[id][j])
            {
                dis[j]=G[id][j];
                pre[j]=id;
            }
        }
    }
    return cost/leng;
}
double solve(int n,double r)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            G[i][j]=G[j][i]=fabs(p[i].h*1.0-p[j].h*1.0)-L[i][j]*r+1000000000.0;
        }
    }
    return dij(n,1);
}
int main()
{
    int n;
    while(scanf("%d",&n),n)
    {
        for(int i=1;i<=n;i++)
            scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].h);
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                L[i][j]=L[j][i]=dist(p[i],p[j]);
        double x0=0,x;
        while(1)
        {
            x=solve(n,x0);
            if(fabs(x-x0)<eps)
                break;
            x0=x;
        }
        printf("%.3lf\n",x);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/mypsq/p/4469554.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值