# poj2728-最小比率生成树/0-1分数规划/二分/迭代

 Total Submissions: 28700 Accepted: 7915

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

n个村庄，村庄在不同坐标和海拔，现在要对所有村庄供水，只要两个村庄之间有一条路即可，建造水管距离为坐标之间的欧几里德距离，费用为海拔之差，现在要求方案使得费用与距离的比值最小，很显然，这个题目是要求一棵最优比率生成树。

int vis[maxn];
int x[maxn], y[maxn], z[maxn];
double dis[maxn], cost[maxn][maxn],dist[maxn][maxn];
int n;
double judge(double k)
{
memset(vis, 0, sizeof(vis));
for (int i = 2; i <= n; i++)
{
dis[i] = cost[1][i] - k * dist[1][i];
}
vis[1] = 1;
dis[1] = 0;
int m;
double sum = 0.0;
for (int i = 2; i <= n; i++)
{
double minn = INF;
for (int j = 1; j <= n; j++)
{
if (!vis[j] && dis[j] < minn)
{
minn = dis[j];
m = j;
}
}
vis[m] = 1;
sum += minn;
for (int j = 1; j <= n; j++)
{
if (!vis[j] && dis[j] > cost[m][j] - dist[m][j] * k)
dis[j] = cost[m][j] - dist[m][j] * k;
}
}
return sum;
}
int main()
{
while (scanf_s("%d", &n) != EOF)
{
if (n == 0)
break;
for (int i = 1; i <= n; i++)
{
scanf_s("%d%d%d", &x[i], &y[i], &z[i]);
}
for(int i=1;i<=n;i++)
{
for (int j = 1; j <= n; j++)
{
double tmp = (x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]);
cost[i][j] = fabs(z[i] - z[j]);
dist[i][j] = sqrt(tmp);
}
}

double l = 0.0;
double r = 10000.0;
while (r - l > 1e-8)
{
double mid = (l + r) / 2.0;
if (judge(mid) > 0)
l = mid;
else
r = mid;

}
printf("%.3f\n", l);
}
return 0;
}

迭代
double prim(double k)
{
memset(vis, 0, sizeof(vis));
double c = 0;
double d = 0;
for (int i = 1; i <= n; i++)
pre[i] = 1;
dis[1] = 0;
vis[1] = 0;
for (int i = 2; i <= n; i++)
{
dis[i] = cost[1][i] - k * dist[1][i];
}
int m;
for (int i = 2; i <= n; i++)
{
int minn = INF;
for (int j = 1; j <= n; j++)
{
if (!vis[j] && dis[j] < minn)
{
minn = dis[j];
m = j;
}
}
vis[m] = 1;
c += cost[pre[m]][m];
d += dist[pre[m]][m];
for (int j = 1; j <= n; j++)
{
if (!vis[j] && dis[j] > cost[m][j] - k * dist[m][j])
{
dis[j] = cost[m][j] - k * dist[m][j];
pre[j] = m;

}
}
}
return c / d;

}
int main()
{
while (scanf_s("%d", &n) != EOF && n)
{
for (int i = 1; i <= n; i++)
{
scanf_s("%d%d%d", &x[i], &y[i], &z[i]);
}
for (int i = 1; i <= n; i++)
{

for (int j = 1; j <= n; j++)
{
dist[i][j] = sqrt((x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]));
cost[i][j] = fabs(z[i] - z[j]);
}
}
double a = 0;
while (1)
{
double b = prim(a);
if (fabs(a - b) < 1e-8)
{
printf("%.3f\n", a);
break;
}
else
a = b;
}
}
return 0;
}