POJ2728--Desert King

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
 
  
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define maxn 2080
#define inf 0x3f3f3f3f
double ddd[maxn];
double dis[maxn][maxn];
double h[maxn][maxn];
double graph[maxn][maxn];
bool vis[maxn];
#define eps 1e-4
int n;
struct Point
{
	double x,y,z;
}point[maxn];
inline double qlen(Point a,Point b)
{
	return sqrt((b.y-a.y)*(b.y-a.y)+(b.x-a.x)*(b.x-a.x));
}
inline void create(double mid)
{
	for(int i = 1;i <= n;i++)
	{
		for(int j = 1;j <= n;j++)
		{
			graph[i][j] = h[i][j] - mid*dis[i][j];
		}
	}
}
inline double qh(Point a,Point b)
{
	return fabs(a.z - b.z);
}
double prim()
{
	double ans = 0;
	memset(vis,0,sizeof(vis));
	for(int i = 1;i <= n;i++)	ddd[i] = graph[1][i];
	ddd[1] = 0;vis[1] = 1;
	for(int i = 1;i  graph[pos][j])
			{
				ddd[j] = graph[pos][j];
			}
		}
	}
	return ans;
}
int main()
{
	//freopen("in.txt","r",stdin);
	while(scanf("%d",&n)!=EOF && n)
	{
		for(int i = 1;i <= n;i++)
		{
			scanf("%lf%lf%lf",&point[i].x,&point[i].y,&point[i].z);
		}
		for(int i = 1;i <= n;i++)
		{
			for(int j = i;j <= n;j++)
			{
				dis[i][j] = dis[j][i] = qlen(point[i],point[j]);
				h[i][j] = h[j][i] = qh(point[i],point[j]);
			}
		}///接下来就要进行二分了
		double l = 0,r = 100,mid;
		while(r>=l)
		{
			mid = (l+r)/2;
			create(mid);
			double temp = prim();
			if(fabs(temp) <= 0.0005) break;
			if(temp > 0.0005)		l = mid;
			else r = mid;
		}
		printf("%.3lf\n",mid);
	}
	return 0;
}
			
		

你可能感兴趣的:(二分查找,图论_最小生成树)