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

【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

【分析】

大致題意:給出 nn 個點,每個點以一個三元組 (x,y,z)(x,y,z) 給出,兩個點的距離 disti,j=(xixj)2+(yiyj)2dist_{i,j}=\sqrt {(x_i-x_j)^2+(y_i-y_j)^2},花費 costi,j=zizjcost_{i,j}=|z_i-z_j|,求出一個生成樹,使得生成樹的總花費比上總距離最大,輸出這個最大比

題解:0/10/1 分數規劃簡單題

我們先把所有邊的 distdistcostcost 處理出來,令選邊的編號為 11 ~ tt

假如說現在二分到了一個值 midmid,那麼有 cost1+cost2+...+costtdist1+dist2+...+distt&gt;=mid\frac{cost_1+cost_2+...+cost_t}{dist_1+dist_2+...+dist_t}&gt;=mid

化簡得 (cost1dist1mid)+(cost2dist2mid)+...+(costtdisttmid)&gt;=0(cost_1-dist_1\cdot mid)+(cost_2-dist_2\cdot mid)+...+(cost_t-dist_t\cdot mid)&gt;=0

因此對於每條邊,把邊權設為 costidistimidcost_i-dist_i\cdot mid,再跑一遍最小生成樹即可

PsPs:由於這道題中,圖是稠密圖,所以用 PrimPrim 會更優

【程式碼】

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1005
#define eps 1e-8
#define inf (1ll<<31ll)-1
using namespace std;
int n,x[N],y[N],z[N];bool vis[N];
double d[N],dist[N][N],cost[N][N];
double dis(int i,int j)
{
	double s1=x[i]-x[j];
	double s2=y[i]-y[j];
	return sqrt(s1*s1+s2*s2);
}
bool check(double mid)
{
	int i,j,p;
	double minn,ans=0;
	memset(vis,false,sizeof(vis));
	for(i=1;i<=n;++i)
	  d[i]=cost[1][i]-dist[1][i]*mid;
	vis[1]=true;
	for(i=1;i<n;++i)
	{
		minn=inf;
		for(j=2;j<=n;++j)
		{
			if(!vis[j]&&minn>d[j])
			{
				p=j;
				minn=d[j];
			}
		}
		vis[p]=true;
		ans+=minn;
		for(j=2;j<=n;++j)
		  d[j]=min(d[j],cost[p][j]-dist[p][j]*mid);
	}
	return ans>=0;
}
int main()
{
	int i,j;
	while(~scanf("%d",&n))
	{
		if(!n)  break;
		for(i=1;i<=n;++i)
		{
			scanf("%d%d%d",&x[i],&y[i],&z[i]);
			for(j=1;j<i;++j)
			{
				dist[i][j]=dist[j][i]=dis(i,j);
				cost[i][j]=cost[j][i]=abs(z[i]-z[j]);
			}
		}
		double l=0,r=100;
		while(r-l>eps)
		{
			double mid=(l+r)/2;
			if(check(mid))  l=mid;
			else  r=mid;
		}
		printf("%.3f\n",l);
	}
	return 0;
}