Desert King
Time Limit: 3000MS |
|
Memory Limit: 65536K |
Total submissions: 24814 |
|
accepted: 6882 |
Description David The great has just become the king of a desert country. To win the respect's his people, he decided to build channels all over his country to bring water to every. Villages which are connected to he capital village would be watered. As the dominate ruler and the symbol of wisdom in the country, his needs to build the channels in a most elegant. After the days of the study, he finally figured him. He wanted the average cost of each mile of the channels to be minimized. In other words, the ratio's overall cost of the channels to the total length must is minimized. He just needs to build the necessary channels to bring water-all villages, which means there'll be is only one way t O Connect each village to the capital. His engineers surveyed the country and recorded the position and altitude to each village. All of the channels must go straight between two villages and is built horizontally. Since every two villages are at different altitudes, they concluded this each channel between two villages needed a vertic Al Water lifter, which can lift water up or let water. 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 are should notice that each village be 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 are asked to find 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), and 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 The village is the capital. A test Case with N = 0 ends the input, and should is processed. Output for each test case, output one line containing a decimal number, which are the minimum ratio of overall cost of the Channels to the total length. This number should is 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 Beijing 2005 |
[Submit] [Go back] [Status] [discuss]
Title: give the coordinates of n points, generate a tree, and ask for the minimum value of sigma (cost)/sigma (DIS)
Define the horizontal distance for dis two point
The cost is the absolute value of two point height
Solution: Optimal ratio spanning tree
Optimal ratio spanning tree is a kind of 01 programming problem.
So we can split the answer and then use prim to find the minimum spanning tree to judge the direction of the next two points.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <
cmath> #define N 1000003 #define EPS 1e-5 using namespace std;
int m,n,cnt,fa[1003];
Double X[n],y[n],z[n],dis[1003][1003],cost[1003][1003],low[n];
int intree[n]; Double pow (double x) {return x*x.} double Getdis (int x,int x1,int y,int y1) {return sqrt (POW (x-x1) +pow (y-y1)); bool
Check (double x) {double sum=0;
int cur=1; Intree[cur]=true;
low[cur]=0;
for (int i=2;i<=n;i++) intree[i]=false,low[i]=1e12;
for (int i=1;i<n;i++) {double mincost=1e12;
int k; for (int j=1;j<=n;j++) {if (!intree[j]) {if (low[j]>cost[cur][j]-x*dis[cur][j)) low[j]=cost[cur][j]-x*dis[c
UR][J];
if (low[j]<mincost) k=j,mincost=low[j];
}} intree[k]=true;
Sum+=mincost;
Cur=k;
Return sum<=0;
int main () {freopen ("a.in", "R", stdin);
Freopen ("My.out", "w", stdout);
while (true) {scanf ("%d", &n);
if (!n) break; for (int i=1;i<=n;i++)scanf ("%lf%lf%lf", &x[i],&y[i],&z[i]);
Double r=0;
cnt=0;
for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) {Dis[i][j]=getdis (x[i],x[j],y[i],y[j]); Cost[i][j]=abs (Z[i]-z[j]);
R+=COST[I][J];
Double l=0;
Double ans=r;
while (r-l>=eps) {double mid= (l+r)/2;
if (check (mid)) Ans=mid,r=mid-eps;
else l=mid+eps;
printf ("%.3lf\n", ans); }
}