zoukankan      html  css  js  c++  java
  • POJ2728 Desert King —— 最优比率生成树 二分法

    题目链接:http://poj.org/problem?id=2728

    Desert King
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 26878   Accepted: 7459

    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

     
     
     
    题解:
     
     
    代码一:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 using namespace std;
    14 typedef long long LL;
    15 const double EPS = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 2e18;
    18 const int MAXN = 1e3+10;
    19 
    20 int n;
    21 double x[MAXN], y[MAXN], z[MAXN];
    22 double dis[MAXN][MAXN], height[MAXN][MAXN], d[MAXN][MAXN];
    23 //dis为两点间的距离, height为两点间的高度差。d[i][j] = height[i][j] - L*dis[i][j],作为图的边权。
    24 
    25 bool vis[MAXN];
    26 double cost[MAXN];
    27 double prim()
    28 {
    29     ms(vis, 0);
    30     for(int i = 2; i<=n; i++)
    31         cost[i] = d[1][i];
    32     vis[1] = true;
    33 
    34     double sum  = 0;
    35     for(int i = 1; i<=n-1; i++)
    36     {
    37         int k;
    38         double minn = INF;
    39         for(int j = 2; j<=n; j++)
    40             if(!vis[j] && minn>cost[j])
    41                 minn = cost[k=j];
    42 
    43         vis[k] = true;
    44         sum += cost[k];     //加上边权
    45         for(int j = 2; j<=n; j++)
    46             if(!vis[j])
    47                 cost[j] = min(cost[j], d[k][j]);
    48     }
    49     return sum;
    50 }
    51 
    52 bool test(double  L)
    53 {
    54     for(int i = 1; i<=n; i++)
    55     for(int j = 1; j<=n; j++)
    56         d[i][j] = height[i][j]-L*dis[i][j];
    57 
    58     return prim()>=0;
    59 }
    60 
    61 int main()
    62 {
    63     while(scanf("%d", &n) && n)
    64     {
    65         for(int i = 1; i<=n; i++)
    66             scanf("%lf%lf%lf", &x[i], &y[i], &z[i]);
    67 
    68         for(int i = 1; i<=n; i++)
    69         for(int j = 1; j<=n; j++)
    70         {
    71             dis[i][j] = sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
    72             height[i][j] = fabs(z[i]-z[j]);
    73         }
    74 
    75         double l = 0, r = 100.0;
    76         while(l+EPS<=r)
    77         {
    78             double mid = (l+r)/2;
    79             if(test(mid))
    80                 l = mid + EPS;
    81             else
    82                 r = mid - EPS;
    83         }
    84         printf("%.3f
    ", r);
    85     }
    86 }
    View Code
  • 相关阅读:
    (Vue中)cehart在同一个dom上画图图切换时饼图有折线图的坐标系
    linux(centos7)修改服务器时间
    centos6 yum源不能使用
    Linux 使用 history 来减少重复命令的几个实用技巧。
    7个Shell 拿来就用脚本实例!
    keepalived的配置解析&安装与爬坑
    linux最全命令使用手册
    linux各种误删文件恢复方法(经典强推)
    位运算符
    SQL 书写、执行顺序
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7571460.html
Copyright © 2011-2022 走看看