zoukankan      html  css  js  c++  java
  • poj-2728Desert King(最优比率生成树)

    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


    题意:
    在这么一个图中求一棵生成树,这棵树点权和边权之比最大是多少?
    
    
       题解:枚举rate,然后来解最大生成树,就可以了,w[u]-line[i]*rate,这样来搞。
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<queue>
     7 #define N 1007
     8 #define eps 0.000001
     9 using namespace std;
    10 
    11 int n;
    12 double dis[N][N],h[N][N],line[N],p[N][N];
    13 bool vis[N];
    14 struct Node
    15 {
    16     double x,y,h;
    17 }a[N];
    18 
    19 double get_dis(int x,int y)
    20 {return sqrt((a[x].x-a[y].x)*(a[x].x-a[y].x)+(a[x].y-a[y].y)*(a[x].y-a[y].y));}
    21 /*struct cmp
    22 {
    23     bool operator()(int x,int y)
    24     {return line[x]>line[y];}
    25 };*/
    26 double prim(double num)
    27 {
    28     for (int i=1;i<=n;i++)
    29         for (int j=1;j<=n;j++)
    30             p[i][j]=h[i][j]-dis[i][j]*num;
    31     //priority_queue<int,vector<int>,cmp>q;
    32     //while(!q.empty()) q.pop();
    33     memset(vis,0,sizeof(vis));
    34     memset(line,127,sizeof(line));
    35     line[1]=0;
    36     //for (int i=1;i<=n;i++) q.push(i);
    37     for (int i=1;i<=n;i++)
    38     {
    39         int u=0;
    40         for (int j=1;j<=n;j++)
    41             if (!vis[j]&&line[j]<line[u]) u=j;
    42         vis[u]=1;
    43         for (int j=1;j<=n;j++)
    44             if (!vis[j]) line[j]=min(line[j],p[u][j]);
    45     }
    46     double sum=0;
    47     for (int i=1;i<=n;i++)
    48         sum+=line[i];
    49     return sum;
    50 }
    51 int main()
    52 {
    53     while(~scanf("%d",&n)&&n)
    54     {
    55         for (int i=1;i<=n;i++)
    56             scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].h);
    57         for (int i=1;i<=n;i++)
    58             for (int j=1;j<=n;j++)
    59             {
    60                 dis[i][j]=get_dis(i,j);
    61                 h[i][j]=fabs(a[i].h-a[j].h);
    62             }
    63         double l=0.0,r=100.0;
    64         while(r-l>=eps)
    65         {
    66             double mid=(l+r)*1.0/2;
    67             if (prim(mid)>=0) l=mid;
    68             else r=mid;     
    69         }
    70         printf("%.3f
    ",l);
    71     }
    72 }
    
    
  • 相关阅读:
    python返回列表最大值(java返回数组最大值)
    Mysql的5种索引添加类型
    阿里云中quick bi用地图分析数据时维度需转换为地理区域类型
    根据变量查找元素,并修改数值的实践
    Linux 通过命令设置网络
    mysql 实现 上一行减去下一行
    Spark 安装与启动
    Kafka 入门之集群部署遇到问题
    rmp使用方法
    Mysql 导入数据的一种方法
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7678281.html
Copyright © 2011-2022 走看看