zoukankan      html  css  js  c++  java
  • Desert King POJ

    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




    第一道分数规划的题目
    二分一个k,判断有没有一个生成树 的权值和是小于等于0的





     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<algorithm>
     7 #include<set>
     8 #include<map>
     9 #include<vector>
    10 #include<queue>
    11 using namespace std;
    12 #define ll long long
    13 #define sqr(x) (1.0*(x)*(x))
    14 #define RG register
    15 #define MAX 1111
    16 inline int read()
    17 {
    18     RG int x=0,t=1;RG char ch=getchar();
    19     while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    20     if(ch=='-')t=-1,ch=getchar();
    21     while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
    22     return x*t;
    23 }
    24 int n,x[MAX],y[MAX],z[MAX];
    25 double dis[MAX];
    26 double len[MAX][MAX],cost[MAX][MAX],g[MAX][MAX];
    27 bool vis[MAX];
    28 bool check(double mid)
    29 {
    30     for(int i=0;i<=n;++i)dis[i]=1e20,vis[i]=false;
    31     dis[1]=0;double tot=0;
    32     for(int i=1;i<=n;++i)
    33         for(int j=1;j<=n;++j)
    34             g[i][j]=cost[i][j]-mid*len[i][j];
    35     for(int i=1;i<=n;++i)
    36     {
    37         int u=0;
    38         for(int j=1;j<=n;++j)if(!vis[j]&&dis[j]<dis[u])u=j;
    39         vis[u]=true;tot+=dis[u];
    40         for(int j=1;j<=n;++j)
    41             if(!vis[j])
    42                 dis[j]=min(dis[j],g[u][j]);
    43     }
    44     return tot<=0;
    45 }
    46 int main()
    47 {
    48     while(n=read())
    49     {
    50         for(int i=1;i<=n;++i)x[i]=read(),y[i]=read(),z[i]=read();
    51         for(int i=1;i<=n;++i)
    52             for(int j=1;j<=n;++j)
    53                 len[i][j]=sqrt(sqr(x[i]-x[j])+sqr(y[i]-y[j])),cost[i][j]=abs(z[i]-z[j]);
    54         
    55         double l=0,r=1e5;
    56         while(r-l>=1e-5)
    57         {
    58             double mid=(l+r)/2;
    59             if(check(mid))r=mid;
    60             else l=mid;
    61         }
    62         printf("%.3f
    ",l);
    63     }
    64     return 0;
    65 }





























  • 相关阅读:
    部署至Oracle数据库的注意事项
    当在centos上面部署项目时,mysql的一些驱动安装不上,
    drf利用redis做缓存是发生一点错误提示
    python字符串前面加个u代表什么
    在学习数据分析时,安装anaconda时遇到一点问题
    在使用scrapy框架爬取sina时,常见保存
    在linux上scrapyd部署scrapy项目时出现service_identity,并且不能识别其中的一个opentype模块
    如何在mysql增加一个和root权限一样的用户,安排
    mysq的root密码忘记,原来还有更优雅的解法
    mysql远程连接阿里云的Ubuntu服务器
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/11042180.html
Copyright © 2011-2022 走看看