Desert King POJ - 2728(最优比率生产树/(二分+生成树))

时间:2022-05-25 01:27:12
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个村庄,每个村庄给你x、y、z坐标,表示他的三维位置,两个村庄之间距离为不算z轴欧几里得距离、建一条路的花费
为两个村庄z坐标差值,构建一条路网络,使的每个点都被连接(生成树),且让其花费/距离最小(01分数规划) 思路:二分比例,然后用最小(最大)生成树,比较是否符合情况。
例如:花费/距离<=mid == 花费-mid*距离 <= 0, 使用最小生成树(边权:花费-mid*距离),看看是否和小于零(满足) 注:我用前向星不知道为什么超时了,看网上题解改的邻接矩阵,知道的大佬orz请通知一声
 #include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#define inf 1e18;
using namespace std; int n;
int x[],y[],z[];
const double eps = 1e-; bool vis[];
double maps[][];
double cost[][]; double dis(int i,int j)
{
return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
} double dist[]; bool prim(double mid)
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)dist[i]=inf;
dist[]=;
for(int i=;i<n;i++)
{
int minn = inf;
int id = -;
for(int j=;j<=n;j++)
{
if(!vis[j] && (id == - || dist[j] < dist[id]))id=j;
}
if(id == -)break;
vis[id]=; for(int j=;j<=n;j++)
{
if(!vis[j])dist[j] = min(dist[j],cost[id][j]-mid*maps[id][j]);
}
}
double ans = ;
for(int i=;i<=n;i++)ans += dist[i];
if(ans <= )return ;
return ;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
memset(maps,,sizeof(maps));
for(int i=;i<=n;i++)
{
scanf("%d%d%d",&x[i],&y[i],&z[i]);
}
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
maps[i][j] = maps[j][i] = dis(i,j);
cost[i][j] = cost[j][i] = abs(z[i]-z[j]);
}
}
double l=,r=;
while(r - l >= eps)
{
double mid =(r-l)/+l;
if(prim(mid))r = mid;
else l = mid;
}
printf("%.3f\n",(l+r)/);
}
}

前向星超时代码:

 #include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#define inf 1e18;
using namespace std; int n;
int x[],y[],z[];
const double eps = 1e-;
struct Node
{
int y,next;
double val,c;
}node[];
int cnt,head[];
bool vis[]; void add(int x,int y,double val,double c)
{
node[++cnt].y=y;
node[cnt].val=val;
node[cnt].c=c;
node[cnt].next=head[x];
head[x]=cnt;
} double dis(int i,int j)
{
return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
} double dist[]; bool prim(double mid)
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)dist[i]=inf;
dist[]=;
for(int i=;i<n;i++)
{
int minn = inf;
int id = -;
for(int j=;j<=n;j++)
{
if(!vis[j] && (id == - || dist[j] < dist[id]))id=j;
}
if(id == -)break;
vis[id]=;
for(int j=head[id];j;j=node[j].next)
{
int to = node[j].y;
if(!vis[to])dist[to] = min(dist[to],node[j].c-mid*node[j].val);
}
}
double ans = ;
for(int i=;i<=n;i++)ans += dist[i];
if(ans <= )return ;
return ;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
cnt = ;
//printf("================================\n");
memset(head ,,sizeof(head));
for(int i=;i<=n;i++)
{
scanf("%d%d%d",&x[i],&y[i],&z[i]);
}
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
double td = dis(i,j);
double tz = abs(z[i]-z[j]);
add(i,j,td,tz);
add(j,i,td,tz);
}
}
double l=,r=;
while(r - l >= eps)
{
double mid =(r-l)/+l;
if(prim(mid))r = mid;
else l = mid;
}
printf("%.3f\n",(l+r)/);
}
}