# poj 2728 Desert King 【最优比例生成树 0-1分数规划】 【二分 or 迭代 + MST】

Desert King
 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22289 Accepted: 6253

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

AC代码：

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define MAXN 1010
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;
int N;
double Map[MAXN][MAXN];
struct Node
{
double x, y, h;
};
Node num[MAXN];
double cost[MAXN][MAXN], len[MAXN][MAXN];
double dis(Node a, Node b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double Max;
void getMap()//求出cost 和 len
{
Max = 0;
for(int i = 0; i < N; i++)
{
for(int j = i+1; j < N; j++)
{
cost[i][j] = cost[j][i] = fabs(num[i].h - num[j].h);
len[i][j] = len[j][i] = dis(num[i], num[j]);
Max = max(Max, cost[i][j] / len[i][j]);
}
}
}
double low[MAXN];
bool vis[MAXN];
double prime()//求最小生成树
{
for(int i = 0; i < N; i++)
{
vis[i] = false;
low[i] = Map[0][i];
}
vis[0] = true;
double ans = 0;
for(int i = 1; i < N; i++)
{
double Min = INF;
int next = 0;
for(int j = 0; j < N; j++)
{
if(!vis[j] && Min > low[j])
{
next = j;
Min = low[j];
}
}
if(Min == INF) break;
vis[next] = true;
ans += Min;
for(int j = 0; j < N; j++)
{
if(!vis[j])
low[j] = min(low[j], Map[next][j]);
}
}
return ans;
}
bool judge(double o)
{
for(int i = 0; i < N; i++)
{
for(int j = i+1; j < N; j++)//重新计算Map值
Map[i][j] = Map[j][i] = cost[i][j] - o * len[i][j];
}
return prime() >= 0;//判断构造最小生成树的 Map值总和是否大于或等于0
}
int main()
{
while(scanf("%d", &N), N)
{
for(int i = 0; i < N; i++)
scanf("%lf%lf%lf", &num[i].x, &num[i].y, &num[i].h);
getMap();
double l = 0, r = Max, mid;
while(r - l >= eps)
{
mid = (l + r) / 2;
if(judge(mid))
l = mid;
else
r = mid;
}
printf("%.3lf\n", l);
}
return 0;
}



1，先给x赋初值（任意N-1条边的花费总和与长度总和的比值），用x值构造Map值，Map[i][j] = cost[i][j] - x * len[i][j]，并用变量x0存储x的值；

2，以构造出的各边的Map值，求最小生成树。在这里用两个变量sumcost和sumlen记录最小生成树中所有边的总花费和总长度。

3，结果sumcost / sumlen是我们用x0（即先前的x）求出的更优的值，更新x = sumcost / sumlen。

4，一直重复执行1、2、3，直到x >= x0 —— 即求出的更优值x 没有上一次的值x0小。

AC代码：

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define MAXN 1010
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;
int N;
double Map[MAXN][MAXN];
struct Node
{
double x, y, h;
};
Node num[MAXN];
double cost[MAXN][MAXN], len[MAXN][MAXN];
double dis(Node a, Node b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
void getMap()//求出cost 和 len
{
for(int i = 0; i < N; i++)
{
for(int j = i+1; j < N; j++)
{
cost[i][j] = cost[j][i] = fabs(num[i].h - num[j].h);
len[i][j] = len[j][i] = dis(num[i], num[j]);
}
}
}
double low[MAXN];
bool vis[MAXN];
int pre[MAXN];//记录该点在最小生成树中的前驱
double prime()//求最小生成树
{
for(int i = 0; i < N; i++)
{
vis[i] = false;
low[i] = Map[0][i];
pre[i] = 0;
}
vis[0] = true;
double sumcost = 0, sumlen = 0;
for(int i = 1; i < N; i++)
{
double Min = INF;
int next = 0;
for(int j = 0; j < N; j++)
{
if(!vis[j] && Min > low[j])
{
next = j;
Min = low[j];
}
}
vis[next] = true;
sumcost += cost[pre[next]][next];//记录花费
sumlen += len[pre[next]][next];//记录长度
for(int j = 0; j < N; j++)
{
if(!vis[j] && low[j] > Map[next][j])
{
low[j] = Map[next][j];
pre[j] = next;
}
}
}
return sumcost / sumlen;
}
void newMap(double o)
{
for(int i = 0; i < N; i++)
{
for(int j = i+1; j < N; j++)//重新计算Map值
Map[i][j] = Map[j][i] = cost[i][j] - o * len[i][j];
}
}
int main()
{
while(scanf("%d", &N), N)
{
for(int i = 0; i < N; i++)
scanf("%lf%lf%lf", &num[i].x, &num[i].y, &num[i].h);
getMap();
double x0;
double sumcost = 0, sumlen = 0;
for(int i = 1; i < N; i++)//任选N-1条边
sumcost += cost[0][i], sumlen += len[0][i];
double x = sumcost / sumlen;
while(1)
{
x0 = x;
newMap(x);//按x的值 重新计算Map值
x = prime();//更新
if(fabs(x0 - x) < eps)//新值没有减少
break;
}
printf("%.3lf\n", x);
}
return 0;
}