poj2728 desert king

http://www.elijahqi.win/archives/1136
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

做完一题最重要的就是再针对这个做一次复盘hhhhh

因为是完全图所以堆优化prim和kruskal复杂度相同 弃用

改用n^2的暴力prim

首先还是再复习下prim的思想 首先我们需要钦定一个点作为起始点,然后更新附近所有能更新的点 再次做的时候每次循环一步之内步长最小的点作为起点,然后去更新那些没访问过且还可以更新的点

这个题呢要求花费/长度最小

那么我们不妨假设我们的ans 就是最小的 则有ans<=花费/长度

那么我们只需要判断花费-长度*ans能否>=0就好了

如果>=0说明我们当前很有可能是答案我们的ans还可以再大一些反之则说明我们的ans不够小 需要缩小右端点

#include<cstdio>
#include<cmath>
#include<cstring>
#define N 1100
inline int read(){
    int x=0;char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
inline int abs(int x){if (x<0) return -x;else return x;} 
inline bool judge1(double a,double b){return b-a>1e-5;}
double f[N],dis[N][N],height[N][N];bool visit[N];int n,x[N],y[N],z[N];
inline double prim(double ans){
    double tmp=0;memset(f,0x7f,sizeof(f));memset(visit,0,sizeof(visit));f[1]=0;
    for (int owo=1;owo<=n;++owo){
        int st=0;
        for (int i=1;i<=n;++i) if (!visit[i]&&f[i]<f[st]) st=i;
        tmp+=f[st];visit[st]=true;
        for (int i=1;i<=n;++i) if (!visit[i]&&height[st][i]-ans*dis[st][i]<f[i])f[i]=height[st][i]-ans*dis[st][i];
    }
    return tmp;
}
int main(){
    freopen("poj2728.in","r",stdin);
    while (~scanf("%d",&n)&&n){
        for (int i=1;i<=n;++i) x[i]=read(),y[i]=read(),z[i]=read();
        for (int i=1;i<=n;++i)
            for (int j=i+1;j<=n;++j){
                dis[i][j]=dis[j][i]=sqrt((double)(x[i]-x[j])*(x[i]-x[j])+(double)(y[i]-y[j])*(y[i]-y[j]));
                height[i][j]=height[j][i]=abs(z[i]-z[j]);
            }
        double l=0,r=1e7;
        while (judge1(l,r)){
            double mid=(l+r)/2;
            if (prim(mid)>=1e-5) l=mid;else r=mid;
        }
        printf("%.3f\n",l);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值