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

    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

    还是01分数规划问题,枚举l,然后求一下最小生成树,嗯,还是很裸啦,然后借机学了一下prim,一直只会Kru(╮(╯▽╰)╭)。

      1 #include <iostream>
      2 #include <cstdlib>
      3 #include <cstdio>
      4 #include <cmath>
      5 #define inf 1000000000
      6 #define eqs 1e-7 
      7 const int N = 1000 + 11 ;
      8 using namespace std ;
      9 int n  ;
     10 struct id
     11 {
     12     int x , y , h ;
     13 } vill[N] ; 
     14 double edge[N][N]  , cost[N] ;
     15 int near[N] ; 
     16 
     17 double ffabs( double a )
     18 {
     19     if( a < 0 ) return -a ; return a ;
     20 }
     21 
     22 double dis( int a , int b )
     23 {
     24 
     25  
     26    return sqrt(1.0 * (vill[a].x - vill[b].x) * (vill[a].x - vill[b].x) + 1.0 * (vill[a].y - vill[b].y) * (vill[a].y - vill[b].y));  
     27 
     28 }
     29 
     30 double prim( int sc , double l )
     31 {
     32     double Cost = 0 , len = 0 ;
     33     for( int i = 1 ; i <= n ; ++i )
     34     {
     35         near[i] = sc ;
     36         cost[i] = abs( vill[sc].h - vill[i].h ) - edge[sc][i] * l ;
     37     }
     38     near[sc] = -1 ;
     39     for( int i = 1 ; i < n ; ++i )
     40     {
     41         double mi = inf ;
     42         int v = -1 ;
     43         for( int j = 1 ; j <= n ; ++j )
     44             if( near[j] != -1  && cost[j] < mi )
     45             {
     46                 v = j ;
     47                 mi = cost[j] ;
     48             }    
     49         if( v != -1 )
     50         {
     51             Cost += abs( vill[near[v]].h - vill[v].h ) ;
     52             len += edge[near[v]][v] ;
     53             near[v] = -1 ;
     54             for( int j = 1 ; j <= n ; ++j )
     55             {
     56                 double tmp = abs( vill[v].h - vill[j].h ) - edge[v][j] * l ;
     57                 if( near[j] != -1 && tmp < cost[j] )
     58                 {
     59                     cost[j] = tmp ;
     60                     near[j] = v ;
     61                 }
     62             }
     63         }
     64     }
     65     return Cost / len ;
     66 }
     67 
     68 
     69 
     70 
     71 void Init( )
     72 {
     73     
     74     for( int x = 1 ; x <= n ; ++x ) 
     75         scanf( "%d%d%d" , &vill[x].x , &vill[x].y , &vill[x].h ) ;
     76     for( int x = 1 ; x <= n ; ++x )
     77         for( int y = 1 ; y <= n ; ++y )
     78             edge[x][y] = dis( x , y ) ;
     79 }
     80 
     81 void Solve( )
     82 {
     83     double ans = 0 , tmp ; 
     84     while( 1 )
     85     {
     86         tmp = prim( 1 , ans ) ;
     87         if( fabs( ans - tmp ) < eqs ) break ;
     88     //    printf( "%.3lf
    " , tmp ) ;
     89         ans = tmp ;  
     90     }
     91     printf( "%.3f
    " , tmp ) ;
     92 }
     93 
     94 int main( )
     95 {
     96     while( ~scanf( "%d" , &n ) && n )
     97     {
     98         Init( ) ;
     99         Solve( ) ;
    100     }
    101     return 0 ;
    102 }

     

  • 相关阅读:
    Bug
    [转]C# 向web网站GET、POST 数据
    使用JavaScript触发ASP.NET Validator验证
    Asp.net 布尔运算符
    HTML 后退功能JS
    [转]C# 获取硬盘序列号 Volume Serial Number
    ASP.NET中,Gridview如何将源数据中的
    显示成回车

    ASP.NET 验证控件
    与或非的运算法则
    [转]WinForm开发,窗体显示和窗体传值相关知识总结
  • 原文地址:https://www.cnblogs.com/Ateisti/p/6028023.html
Copyright © 2011-2022 走看看