zoukankan      html  css  js  c++  java
  • 【POJ2728】Desert King

    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.

    题目大意

    平面上给出$n$个点,两两之间都有连边,一条边有两个权值:距离和高度差,求一个生成树使得$frac{sum dist_i}{sum height_i}$最大

    思路

    『POJ2976』一样,判断时改成prim就可以了

    /************************************************
    *Author        :  lrj124
    *Created Time  :  2018.10.01.20:38
    *Mail          :  1584634848@qq.com
    *Problem       :  poj2728
    ************************************************/
    #include <cstdio>
    #include <cmath>
    using namespace std;
    const int maxn = 1000 + 10;
    double a[maxn][maxn],b[maxn][maxn],tmp[maxn][maxn],Min[maxn];
    struct Node { int x,y,z; } p[maxn];
    bool vis[maxn];
    int n,e[maxn];
    inline bool prim(double x) {
        for (int i = 1;i <= n;i++)
            for (int j = 1;j <= n;j++) tmp[i][j] = a[i][j]-x*b[i][j];
        for (int i = 0;i <= n;i++) {
            vis[i] = false;
            Min[i] = 1000000000;
        }
        Min[1] = 0;
        e[1] = 0;
        double ans = 0;
        for (int i = 1;i <= n;i++) {
            int minnum = 0;
            for (int j = 1;j <= n;j++)
                if (Min[minnum] > Min[j] && !vis[j]) minnum = j;
            vis[minnum] = true;
            ans += tmp[e[minnum]][minnum];
            for (int j = 1;j <= n;j++)
                if (tmp[minnum][j] < Min[j] && !vis[j]) {
                    Min[j] = tmp[minnum][j];
                    e[j] = minnum;
                }
        }
        return ans <= 0;
    }
    int main() {
        //freopen("poj2728.in","r",stdin);
        //freopen("poj2728.out","w",stdout);
        while (scanf("%d",&n) , n) {
            for (int i = 1;i <= n;i++) scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
            for (int i = 1;i <= n;i++)
                for (int j = 1;j <= n;j++) {
                    a[i][j] = fabs(p[i].z-p[j].z);
                    b[i][j] = sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y));
                }
            double l = 0,r = 1000000000;
            while (r-l >= 1e-6) {
                double mid = (l+r)/2;
                if (prim(mid)) r = mid;
                else l = mid;
            }
            printf("%.3f
    ",l);
        }
        return 0;
    }
  • 相关阅读:
    webpack4.x 入门一篇足矣
    面试精选之Promise
    六月前端知识集锦(每月不可错过的文章集锦)
    SpringBoot整合MyBatis与MySql8.0
    tomcat报错:This is very likely to create a memory leak问题解决
    配置tomcat服务器内存大小中的Xms、Xmx、PermSize、MaxPermSize 详解
    不用FTP,直接Windows与Linux下互传文件
    SpringBoot项目单元测试
    web.xml 中的listener、 filter、servlet 加载顺序及其详解
    linux下重启weblogic(关闭和启动)
  • 原文地址:https://www.cnblogs.com/lrj124/p/9735758.html
Copyright © 2011-2022 走看看