1. 程式人生 > >【POJ 2728 Desert King】

【POJ 2728 Desert King】

ice ota num lines pla htm urn com 0ms

Time Limit: 3000MS
Memory Limit: 65536K

Total Submissions: 27109
Accepted: 7527

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

Source

Beijing 2005

【翻譯】題目大意:有n個村莊要連在一起,村與村之間的長度為他們之間的水平距離,連在一起的花費是兩村的高度差。現求所花費和與長度和之比最小。

分析:

①Sigma的比值可以想到使用01分數規劃。

②01分數規劃有兩種實現方式:

(1)dichotomy[有點慢] (2)Dinkelbach[超級快]

③所以整個程序就是01分數規劃然後使用Prim求出最小生成樹。

④Prim很快,代碼又好寫。

#include<math.h>
#include<stdio.h>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define G(i,j) (g[i][j].cost-r1*g[i][j].len)
const int N=1010;int vis[N];
double Sqr(double a){return a*a;};
double Abs(double a){return a<0?-a:a;}
struct E{double cost,len;}g[N][N],e[N];
int n,x[N],y[N],h[N],T=-1e9;double d[N],r1,r2;
double Dis(int i,int j){return sqrt(Sqr(x[i]-x[j])+Sqr(y[i]-y[j]));}

void Prim()
{
	double C=0,D=0;d[1]=1e15;int v;++T;
	
	go(u,2,n)d[u]=G(1,u),e[u]=g[1][u];go(i,2,n){v=1;
	go(u,2,n)if(vis[u]!=T&&d[u]<d[v])v=u;vis[v]=T;C+=e[v].cost;
	go(u,2,n)if(vis[u]!=T&&G(v,u)<d[u])d[u]=G(v,u),e[u]=g[v][u];D+=e[v].len;}
	
	r2=C/D;
}

int main()
{
	while(scanf("%d",&n),n)
	{
		go(i,1,n)scanf("%d%d%d",x+i,y+i,h+i);
		go(i,1,n)go(j,1,n)g[i][j]=(E){Abs(h[i]-h[j]),Dis(i,j)};
		r1=r2=0;while(Prim(),Abs(r1-r2)>1e-5)r1=r2;printf("%.3f\n",r1);
	}
	return 0;
}//Paul_Guderian

And every young mammal has multitudinous opportunities.————Judy·Hopps

【POJ 2728 Desert King】