POJ_P2728 Desert King(最优比率生成树)

本文介绍了一个经典的算法问题,即如何构建最优的渠道网络以最小化单位长度的成本。问题通过01分数规划结合最小生成树的方法解决,确保了从首都到每个村庄的唯一路径,并实现了成本与长度比的最小化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

POJ传送门

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 23163 Accepted: 6491
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

Source
Beijing 2005

Sol:
就是01分数规划+最小生成树

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;
#define N 1005
#define eps 1e-4
#define INF 1e8
float x[N],y[N],z[N];
float a[N],b[N],d[N];
bool v[N];
int n;float ans,L,p,q;
void Prim(){
    for(int i=0;i<n;i++) 
        a[i]=fabs(z[i]-z[0]),
        b[i]=sqrt((x[i]-x[0])*(x[i]-x[0])+(y[i]-y[0])*(y[i]-y[0])),
        d[i]=a[i]-L*b[i];
    memset(v,0,sizeof(v));
    v[0]=1;int s=1;p=q=0;float MinV,t1,t2;int MinE;
    while(s<n){
        MinV=INF;
        for(int i=0;i<n;i++) if(!v[i]&&d[i]<MinV) MinV=d[i],MinE=i;
        p+=a[MinE],q+=b[MinE],v[MinE]=1;++s;
        for(int i=0;i<n;i++){
            if(!v[i]){
                t1=fabs(z[i]-z[MinE]),
                t2=sqrt((x[i]-x[MinE])*(x[i]-x[MinE])+(y[i]-y[MinE])*(y[i]-y[MinE]));
                if(t1-L*t2<d[i]) d[i]=t1-L*t2,a[i]=t1,b[i]=t2;
            }
        }
    }
    L=p/q;
}
inline void Solve(){
    for(int i=0;i<n;i++) scanf("%f%f%f",&x[i],&y[i],&z[i]);
    ans=1,L=0;
    while(fabs(ans-L)>eps) 
        ans=L,Prim();
    printf("%.3f\n",ans);
}
int main(){while(~scanf("%d",&n)&&n) Solve();}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值