01 Fractional planning poj2728 (optimal scale spanning tree)

Source: Internet
Author: User

Desert King
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 21766 Accepted: 6087

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 is connected to he capital village would be watered. As the dominate ruler and the symbol of wisdom in the country, he needs to build the channels in a most elegant the.

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 would be is only one T O 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, villages and be built horizontally. Since Every, villages is at different altitudes, they concluded, each channel between, villages needed a vertic Al Water lifter, which can lift water up or let water flow down. The length of the channel is the horizontal distance between the villages. The cost of the channel is the height of the lifter. You should notice the village is at a different altitude, and different channels can ' t share a lifter. Channels can intersect safely and no three villages is on the same line.

As King David's prime scientist and programmer, you're asked to find out the best solution to build the channels.

Input

There is several test cases. Each test case starts with a line containing a number N (2 <= N <=), 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 are the altitude. The first village is the capital. A test Case with N = 0 ends the input, and should not being processed.

Output

For each test case, output one line containing a decimal number, which are the minimum ratio of overall cost of the channel s to the total length. This number should is rounded three digits after the decimal point.

Sample Input

40 0 00 1 11 1 21 0 30

Sample Output

1.000 
Test Instructions: There are n villages that give each village geographical coordinates (XI,YI) and altitude hi, requiring some river channels to be repaired between villages, so that these rivers can be connected to all villages, and different villages need to be repaired because of differences in altitude, The cost per pump is the difference in altitude for these two villages, and each pump is used for one channel, requiring all costs/total channel length ratios to be minimal
Analysis: R=sigma (H[i][j])/sigma (L[i][j]), with R as the optimal value, then r>=r; (h[i) [j] represents the cost of repairing each river, L[i][j] represents the length of each channel, Sigma () is the edge of the spanning tree)
i.e.: Sigma (H[i][j])/sigma (L[i][j]) >=r, so: H[r]=sigma (H[I][J) )-r*sigma (L[i][j]) >=0;
Therefore, for each r corresponding to the minimum of H (r) spanning tree >=0
when H (r) <0 reduce the value of r, otherwise increase the value of R, gradually two points to the H (r) =0 can be;
Method 1:2 Sub-method
#include "stdio.h" #include "string.h" #include "math.h" #define INF 0x3f3f3f3f#define M 2009#define EPS 1e-7struct node{in T x,y,h;} P[M];d ouble Dist (node A,node b) {return sqrt ((a.x-b.x) * (a.x-b.x) *1.0+ (A.Y-B.Y) * (A.Y-B.Y));}    Double Dis[m],g[m][m],l[m][m];int use[m];d ouble dij (int n,int s) {double sum=0;    memset (use,0,sizeof (use));    for (int i=1;i<=n;i++) Dis[i]=inf;    dis[s]=0;        for (int i=1;i<=n;i++) {double mini=inf;        int id=-1;                for (int j=1;j<=n;j++) {if (!use[j]&&dis[j]<mini) {mini=dis[j];            Id=j;        }} if (Id==-1) break;        Sum+=dis[id];        Use[id]=1;        for (int j=1;j<=n;j++) {if (!use[j]&&dis[j]>g[id][j]) dis[j]=g[id][j]; }} return sum;} Double Solve (int n,double r) {for (int. i=1;i<=n;i++) {for (int j=i+1;j<=n;j++) g[i][j]=g[j][i ]=fabs (p[i].h*1.0-p[j].h*1.0)-l[i][j]*r+1000000000.0; } return Dij (n,1)-(n-1) *1000000000.0;}    int main () {int n;        while (scanf ("%d", &n), N) {double l=0,r=0,mid;            for (int i=1;i<=n;i++) {scanf ("%d%d%d", &p[i].x,&p[i].y,&p[i].h);        R+=p[i].h; } for (int i=1;i<=n;i++) {for (int j=i+1;j<=n;j++) {l[i][j]=l[j][            I]=dist (P[i],p[j]);            }} while (R-l>eps) {mid= (l+r)/2;            Double Msg=solve (N,MID);            if (msg<0) {r=mid;            } else {l=mid;    }} printf ("%.3lf\n", R); } return 0;}

Method Two: Iterative method

#include "stdio.h" #include "string.h" #include "math.h" #define INF 0x3f3f3f3f#define M 2009#define EPS 1e-6struct node{in T x,y,h;} P[M];d ouble Dist (node A,node b) {return sqrt ((a.x-b.x) * (a.x-b.x) *1.0+ (A.Y-B.Y) * (A.Y-B.Y));}    Double Dis[m],g[m][m],l[m][m];int use[m],pre[m];d ouble dij (int n,int s) {double sum=0,cost=0,leng=0;    memset (use,0,sizeof (use));        for (int i=1;i<=n;i++) {dis[i]=inf;    Pre[i]=-1;    } dis[s]=0;        for (int i=1;i<=n;i++) {double mini=inf;        int id=-1;                for (int j=1;j<=n;j++) {if (!use[j]&&dis[j]<mini) {mini=dis[j];            Id=j;        }} if (Id==-1) break;        Sum+=dis[id];        Use[id]=1;            if (pre[id]!=-1) {cost+=fabs (p[id].h*1.0-p[pre[id]].h);        Leng+=l[id][pre[id]];                } for (int j=1;j<=n;j++) {if (!use[j]&&dis[j]>g[id][j]) { DIS[J]=G[ID][J];            Pre[j]=id; }}} return Cost/leng;} Double Solve (int n,double r) {for (int. i=1;i<=n;i++) {for (int j=i+1;j<=n;j++) {g[i][        J]=g[j][i]=fabs (p[i].h*1.0-p[j].h*1.0)-l[i][j]*r+1000000000.0; }} return Dij (n,1);}    int main () {int n; while (scanf ("%d", &n), N) {for (int i=1;i<=n;i++) scanf ("%d%d%d", &p[i].x,&p[i].y,&p[        I].H);        for (int. i=1;i<=n;i++) for (int j=i+1;j<=n;j++) l[i][j]=l[j][i]=dist (P[i],p[j]);        Double x0=0,x;            while (1) {x=solve (n,x0);            if (Fabs (x-x0) <eps) break;        X0=x;    } printf ("%.3lf\n", X); } return 0;}

  

01 Fractional planning poj2728 (optimal scale spanning tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.