poj 2728 Desert King(01分数规划--最优比率生成树)

Desert King
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 26572   Accepted: 7370

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个村庄,每个村庄都有一个坐标和一个高度,现在要给所有的村庄供水,要求每两个村庄之间都有且只有一条路径,建造水管的长度为两点之间的距离,费用为两个村庄的高度差,现要求费用和与长度和比值最小,求最小方案

经典01分数规划问题:

http://blog.csdn.net/jaihk662/article/details/77505318

两个村庄间只允许一条路径也就意味着是个生成树

所以还是这个公式:

poj 2728 Desert King(01分数规划--最优比率生成树)_第1张图片

其中a[i]相当于费用,b[i]相当于长度

不过这道题是求最小比值,不是最大比值,那就优先选择d[i]小的加在一起判断是否大于0就行了

如果大于0说明当时的cnt过小以至于不合法

用二分2800ms,Dinkelbach迭代218ms

#include
#include
#include
#include
typedef struct
{
	int x, y;
	int h;
}Point;
typedef struct
{
	double cost;
	double len;
}Road;
Road road[1005][1005];
Point s[1005];
int flag[1005], pre[1005];
double bet[1005];
int main(void)
{
	int n, i, j, temp;
	double cnt, k, sa, sb;
	while(scanf("%d", &n), n!=0)
	{
		for(i=1;i<=n;i++)
			scanf("%d%d%d", &s[i].x, &s[i].y, &s[i].h);
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=i-1;j++)
			{
				road[i][j].len = sqrt(1.0*(s[i].x-s[j].x)*(s[i].x-s[j].x)+(s[i].y-s[j].y)*(s[i].y-s[j].y));
				road[i][j].cost = abs(s[i].h-s[j].h);
				road[j][i] = road[i][j];
			}
		}
		cnt = 0;
		while(1)
		{
			memset(flag, 0, sizeof(flag));
			for(i=1;i<=n;i++)
			{
				bet[i] = road[1][i].cost-cnt*road[1][i].len;
				pre[i] = 1;
			}
			flag[1] = 1;
			sa = sb = 0;
			for(i=2;i<=n;i++)
			{
				k = 10000000;
				for(j=2;j<=n;j++)
				{
					if(flag[j]==0 && bet[j]

你可能感兴趣的:(#,最短路与最小生成树)