Topic Connection:
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 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
Output
Test instructions: In such a graph to find a spanning tree, how much is the unit length of this tree the minimum cost? Ideas: minimum spanning tree expression can be written like this ∑x[i]*dis[i]-minsum>=0; (X[i] is 0 or 1, requires a spanning tree) This topic ans<= (∑cost[i]*x[i])/( ∑dis[i]*x[i]). Deformation can be ∑x[i]* (Cost[i]-dis[i]*ans) -0>=0;cost[i]-dis[i]*ans is equivalent to the smallest spanning tree dis[i]; two-point ans, Check to run while the prime algorithm can see the minimum spanning tree and whether the >=0, the final answer can be obtained, but also the use of iterative methods; &NBSP;AC Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #include <stack>using namespace std; #define for (i,j,n) for (int i=j;i<=n;i++) #define MST (SS,B) memset (Ss,b,sizeof (SS)); typedef long LONG ll;template<class t> void Read (t&num) {char CH; bool F=false; For (Ch=getchar (); ch< ' 0 ' | | Ch> ' 9 '; f= ch== '-', Ch=getchar ()); for (num=0; ch>= ' 0 ' &&ch<= ' 9 '; num=num*10+ch-' 0 ', Ch=getchar ()); F && (num=-num);} int stk[70], tp;template<class t> inline void print (T p) {if (!p) {puts ("0"); return;} while (p) stk[++ TP] = p%10, p/=10; while (TP) Putchar (stk[tp--] + ' 0 '); Putchar (' \ n ');} Const LL Mod=1e9+7;const double Pi=acos ( -1.0); const double INF=1E18;CONST int n=15e4+10;const int Maxn=1e3+10;const Doubl E eps=1e-5;int n;double cost[maxn][maxn],dis[maxn][maxn],x[maxn],y[maxn],z[maxn];int VIS[MAXN];d ouble get_dis (int A, int b) {return sqrt ((X[a]-x[b]) * (X[a]-x[b]) + (y[a]-y[b]) * (Y[A]-Y[B));} int check (double x) {MST (vis,0); Double SUM=0,LOWCOST[MAXN]; Vis[1]=1; for (I,1,n) lowcost[i]=cost[1][i]-x*dis[1][i]; for (i,2,n) {double temp=inf;int k=-1; for (J,2,n) {if (!vis[j]&&lowcost[j]<temp) {k=j; TEMP=LOWCOST[J]; }} if (K==-1) break; Vis[k]=1; Sum+=temp; for (J,2,n) {if (!vis[j]&&cost[k][j]-x*dis[k][j]<lowcost[j]) lowcost[j]=cost[k][j]- X*DIS[K][J]; }} if (sum>=0) return 1; return 0;} int main () {while (1) {read (n); if (n==0) break; for (i,1,n) {scanf ("%lf%lf%lf", &x[i],&y[i],&z[i]); } for (I,1,n) for (j,i+1,n) {Dis[i][j]=dis[j][i]=get_dis (i,j); Cost[i][j]=cost[j][i]=abs (Z[i]-z[j]); } DouBLE l=0.0,r=100.0; while (r-l>=eps) {double mid= (l+r)/2; if (check (mid)) L=mid; else R=mid; } printf ("%.3f\n", R); } return 0;}
Poj-2728desert King (optimal ratio spanning tree)