POJ 2728 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<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define inone(x) scanf("%d",&x)
#define intwo(x,y) scanf("%d%d",&x,&y)
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)
const int N = 1e3 + 10;
const int INF = 0x7FFFFFFF;
const double eps = 1e-5;
int n, m, x[N], y[N], z[N];
double d[N][N], c[N][N], D[N];
int vis[N];

bool check(double k)
{
	rep(i, 0, n) D[i] = INF, vis[i] = 0;
	double res = D[1] = 0;
	while (true)
	{
		int x = 0;
		rep(i, 1, n)
		{
			if (vis[i]) continue;
			if (D[i] < D[x]) x = i;
		}
		if (!x) break;
		vis[x] = 1; res += D[x];
		rep(i, 1, n)
		{
			D[i] = min(D[i], c[x][i] - k * d[x][i]);
		}
	}
	return res > eps;
}

int main()
{
	while (inone(n), n)
	{
		rep(i, 1, n) inthr(x[i], y[i], z[i]);
		rep(i, 1, n) rep(j, 1, n)
		{
			d[i][j] = sqrt(1.0*(x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]));
			c[i][j] = z[i] < z[j] ? z[j] - z[i] : z[i] - z[j];
		}
		double l = 0, r = 1000000;
		while (r - l > eps)
		{
			double mid = (l + r) / 2;
			if (check(mid)) l = mid; else r = mid;
		}
		printf("%.3lf\n", r);
	}
	return 0;
}

迭代:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define inone(x) scanf("%d",&x)
#define intwo(x,y) scanf("%d%d",&x,&y)
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)
const int N = 1e3 + 10;
const int INF = 0x7FFFFFFF;
const double eps = 1e-5;
int n, m, x[N], y[N], z[N];
double d[N][N], c[N][N], D[N], a[N], b[N];
int vis[N];

double check(double k)
{
	rep(i, 0, n) D[i] = INF, vis[i] = a[i] = b[i] = 0;
	double s1 = 0, s2 = D[1] = 0;
	while (true)
	{
		int x = 0;
		rep(i, 1, n)
		{
			if (vis[i]) continue;
			if (D[i] < D[x]) x = i;
		}
		if (!x) break;
		vis[x] = 1; s1 += a[x]; s2 += b[x];
		rep(i, 1, n)
		{
			if (D[i] > c[x][i] - k * d[x][i])
			{
				D[i] = c[x][i] - k * d[x][i];
				a[i] = c[x][i]; b[i] = d[x][i];
			}
		}
	}
	return s1 / s2;
}

int main()
{
	while (inone(n), n)
	{
		rep(i, 1, n) inthr(x[i], y[i], z[i]);
		rep(i, 1, n) rep(j, 1, n)
		{
			d[i][j] = sqrt(1.0*(x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]));
			c[i][j] = z[i] < z[j] ? z[j] - z[i] : z[i] - z[j];
		}
		double bef = 0, now;
		while (fabs((now = check(bef)) - bef) > eps) bef = now;
		printf("%.3lf\n", now);
	}
	return 0;
}
事实证明迭代比二分要快得多。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值