Desert King (POJ 2728 optimal ratio spanning tree 0-1 score planning)

Source: Internet
Author: User

Language:DefaultDesert King
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 22113 Accepted: 6187

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

Source

Beijing 2005



Test instructions: Connecting n villages together, telling the three-dimensional coordinates of each village, the distance between the villages in the horizontal direction, the height difference in the vertical direction, and the minimum cost and length of the village.

Ideas: Classic 01 Score planning questions, refer to this great God's explanation should be able to understand: http://www.cnblogs.com/Fatedayt/archive/2012/03/05/2380888.html


Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define PI ACOs ( -1.0) #define EPS 1e-6# Define Lson rt<<1,l,mid#define Rson rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A; I <= b; i++) #define FREE ( I,A,B) for (i = A, I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i-- ) #define MEM (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d",         &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF Printf#define DBG  PF ("hi\n") typedef long long ll;using namespace std; #define INF 0x3f3f3f3f#define mod 1000000009const int maxn = 1005;const int MAXN = 2005;const int maxm = 200010;const inT N = 1005;double X[MAXN],Y[MAXN],Z[MAXN];d ouble Dist[maxn],mp[maxn][maxn],len[maxn][maxn],cost[maxn][maxn];bool Vis [Maxn];int pre[maxn];int n;double Dis (int i,int j) {return sqrt ((X[i]-x[j]) * (X[i]-x[j]) + (Y[i]-y[j]) * (Y[i]-y[j]);}    Double Prim (double r) {int i,j,now;    Double mi,c=0,l=0;        for (i=0;i<n;i++) {dist[i]=inf;        for (j=0;j<n;j++) {mp[i][j]=cost[i][j]-r*len[i][j];        }} for (i=0;i<n;i++) {dist[i]=mp[i][0];        pre[i]=0;    Vis[i]=false;    } dist[0]=0;    Vis[0]=true;        for (i=1;i<n;i++) {mi=inf;now=-1;                for (j=0;j<n;j++) {if (!vis[j]&&mi>dist[j]) {mi=dist[j];            Now=j;        }} if (Now==-1) break;        Vis[now]=true;        C+=cost[pre[now]][now];        L+=len[pre[now]][now];  for (j=0;j<n;j++) {if (!vis[j]&&dist[j]>mp[now][j]) {              DIST[J]=MP[NOW][J];            Pre[j]=now; }}} return c/l;}    int main () {#ifndef Online_judge freopen ("C:/users/lyf/desktop/in.txt", "R", stdin), #endif int i,j;        while (SF (n)) {if (n==0) break;        for (i=0;i<n;i++) scanf ("%lf%lf%lf", &x[i],&y[i],&z[i]);                for (i=0;i<n;i++) {for (j=0;j<n;j++) {Len[i][j]=dis (i,j);            Cost[i][j]=fabs (Z[i]-z[j]);    }} double r=0,rate;            R iteration Initial value is 0 while (1) {rate=r;            R=prim (R);        if (Fabs (r-rate) <eps) break;    } printf ("%.3f\n", R); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Desert King (POJ 2728 optimal ratio spanning tree 0-1 score planning)

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.