• ZOJ 2728 Desert King(最优比例生成树)


    Desert King
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 20586   Accepted: 5777

    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

    最优比例生成树:http://www.cnblogs.com/lotus3x/archive/2009/03/21/1418480.html
    这是求最小值的情况。
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <string>
     5 #include <cmath>
     6 #include <iomanip> 
     7 using namespace std;
     8 #define maxn 1010
     9 #define INF 9999999
    10 int N;
    11 struct Node{
    12     double x, y, z;
    13 }node[maxn];
    14 double l ,r ,m;
    15 double len[maxn][maxn], cost[maxn][maxn];
    16 int vis[maxn];
    17 double low[maxn];
    18 double cal(double x1, double y1, double x2, double y2){
    19     return (sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)));
    20 }
    21 double prime(double L){
    22     int pos = 1; double result = 0;
    23     memset(vis, 0, sizeof(vis));
    24     vis[pos] = 1; 
    25     for(int i = 1; i <= N; i++){
    26         if(i != pos) low[i] = cost[pos][i] - L*len[pos][i];
    27     }
    28     
    29     for(int i = 1; i <= N-1; i++){
    30         double min = INF;
    31         for(int j = 1; j <= N; j++){
    32             if(!vis[j] && min > low[j]){
    33                 pos = j; min = low[j];
    34             }
    35         } 
    36         result += min;
    37         vis[pos] = 1;
    38         
    39         for(int j = 1; j <= N; j++){
    40             if(!vis[j] && cost[pos][j] - L*len[pos][j] < low[j]) low[j] = cost[pos][j] - L*len[pos][j];
    41         }
    42 
    43     }
    44     return result;
    45 }
    46 int main(){
    47     while(scanf("%d", &N) && N){
    48         for(int i = 1; i <= N; i++){
    49             scanf("%lf%lf%lf", &node[i].x, &node[i].y, &node[i].z);
    50         }
    51         memset(len, 0, sizeof(len));
    52         memset(cost, 0, sizeof(cost));
    53         for(int i = 1; i <= N; i++){
    54             for(int j = 1; j <= N; j++){
    55                 if(i != j){
    56                     len[i][j] = len[j][i] = cal(node[i].x, node[i].y, node[j].x, node[j].y);
    57                     cost[i][j] = cost[j][i] = fabs(node[i].z - node[j].z);
    58                 }
    59             }
    60         } 
    61         l = 0, r = 100.0;
    62         while( r - l > 1e-7){
    63             m = (l+r)/2;
    64             if(prime(m) >= 0) l = m;
    65             else r = m;
    66         }
    67         cout<<fixed<<setprecision(3)<<m<<endl;
    68         
    69     }
    70     
    71     return 0;
    72 }
  • 相关阅读:
    centos7 下安装MongoDB
    centos7 学习笔记
    MongoDB相关资料收集
    centos 下安装.net core
    sql server 2008 r2 中的oracle发布使用笔记
    sql server 与oracle数据互导的一种思路--sql server链接服务器
    Visual Studio 2015正式版/产品密钥 Win10正式版官方原版ISO镜像下载大全&安装激活教程
    Modbus库开发笔记:Modbus ASCII Slave开发
    PID控制器开发笔记之十一:专家PID控制器的实现
    μCUnit,微控制器的单元测试框架
  • 原文地址:https://www.cnblogs.com/titicia/p/3917939.html
Copyright © 2020-2023  润新知