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 }





























  • 相关阅读:
    C++中的函数名称粉碎机制和它的逆向应用
    C++中const关键字的功能总结
    探究printf函数对单精度浮点数的处理
    利用共用体和位段获得IEEE标准编码的浮点型数据各部分数值
    C++中使用switch..case语句的易出错陷阱和规避方法
    浮点型数据(float, double)存储IEEE标准解析和应用
    React实用技巧
    Web前端之iframe详解
    开源的api文档管理系统
    编写自己的代码库(css3常用动画的实现)
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/11042180.html
Copyright © 2011-2022 走看看