• POJ2728 Desert King (最优比率生成树)


    Desert King

    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 23729   Accepted: 6631

    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

    在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少?

    这个其实应该是属于01数分规划里面的

    迭代很快但是我不会 就二分吧
    二分这个比率
    double cost(double mid,int i,int j){
    return mid*mp[i][j]-1.0*(abs(a[i].z-a[j].z));
    }
    上面这个是二分枚举比率的式子
    if (ret>eps) return 1;
    return 0;

    这个是判断条件

    01其实对应的是选和不选


     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cmath>
     5 #include <ctype.h>
     6 #include <set>
     7 #include <map>
     8 #include <queue>
     9 #include <stack>
    10 #include <iostream>
    11 using namespace std;
    12 #define bug printf("******
    ");
    13 const int maxn = 1e6 + 10;
    14 #define rtl   rt<<1
    15 #define rtr   rt<<1|1
    16 const double eps=1e-6;
    17 int n,vis[1010];
    18 double mp[1010][1010],p[1010];
    19 struct node {
    20     double x,y,z;
    21 }a[1010];
    22 double cost(double mid,int i,int j){
    23     return mid*mp[i][j]-1.0*(abs(a[i].z-a[j].z));
    24 }
    25 int pri(double mid) {
    26     for (int i=0 ;i<=n ;i++) vis[i]=0,p[i]=-100000000;
    27     p[0]=0;
    28     double ret=0;
    29     for (int i=0 ;i<n ;i++ ) {
    30         int idx=-1;
    31         double maxx=-110000000.0;
    32         for (int j=0 ;j<n ;j++){
    33             if (vis[j]) continue;
    34             if (p[j]>maxx) {
    35                 maxx=p[j];
    36                 idx=j;
    37             }
    38         }
    39         if (idx==-1) break;
    40         vis[idx]=1;
    41         ret+=maxx;
    42         for (int j=0 ;j<n ;j++) {
    43             if (vis[j]) continue;
    44             p[j]=max(p[j],cost(mid,idx,j));
    45         }
    46     }
    47     if (ret>eps) return 1;
    48     return 0;
    49 }
    50 int main() {
    51     while(scanf("%d",&n),n){
    52         for (int i=0 ;i<n ;i++)
    53             scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].z);
    54         for (int i=0 ;i<n ;i++)
    55             for (int j=i ;j<n ;j++)
    56                 mp[i][j]=mp[j][i]=sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
    57         double high=100,low=0,mid;
    58         int m=50;
    59         while(m--){
    60             mid=(low+high)/2;
    61             if (pri(mid)) high=mid;
    62             else low=mid;
    63         }
    64         printf("%.3f
    ",low);
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    UITextView 实现placeholder的方法
    15个重要的Android代码
    Java 网络文件传输
    iPhone打包步骤
    (转)android开发中WebView的使用(附完整程序)
    来自老外用jquery实现的内容隐藏代码
    AJAX实现页面选项卡、隔行换色、弹出层功能代码
    JavaScript防FLASH效果的下拉菜单导航代码
    CSS打造全兼容(IE6、IE7、FF)的下拉导航菜单
    很简单的CSS黑色线条滑动门代码
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9415782.html
Copyright © 2020-2023  润新知