Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 22717 | Accepted: 6374 |
Description
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
Output
Sample Input
4
0 0 0
0 1 1
1 1 2
1 0 3
0
Sample Output
1.000
题解:先是超时,然后wa,最小生成树生疏了。。。
这个就是01分数规划的变形,即找到K求hi-li*K的最小生成树使得k最小;
有N个村庄,给出每个村庄的坐标和海拔,,benifit为两点之间的距离,cost为两点的高度差,现在要求一棵树使得 cost / benift 最小,即求一个最优比例生成树
第一次遇见这种生成树,在网上找了个解法
假设sigma(h)/sigma(l)==K 均值K可取,即: sigma(h)==K*sigma(l)
sigma(h)==K*(l1+l2+l3+...lm)
sigma(h)==K*l1+K*l2+K*l3+...K*lm
把原来的每个边的h都减去K*l
即hi'=hi-li'==hi-li*K
然后问题可以转换到求hi'这些边的最小生成树了
如果hi'这些边得最小生成树权值和<=0.0,说明K这个均值可取
对于k,二分求解即可
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long LL;
const int MAXN=;
double vis[MAXN],low[MAXN];
int N;
double R;
struct Node{
double x,y,h;
};
Node dt[MAXN];
double len[MAXN][MAXN],cost[MAXN][MAXN];
double getl(Node a,Node b){
double x=b.x-a.x,y=b.y-a.y;
return sqrt(x*x+y*y);
} bool prime(){
double total;
mem(vis,);
for(int i=;i<N;i++)low[i]=cost[][i]-R*len[][i];
total=;
vis[]=;//0没有被标记为1。。。错了半天;
for(int i=;i<N;i++){
double temp=INF;
int k;
for(int j=;j<N;j++)
if(!vis[j]&&low[j]<temp)temp=low[j],k=j;
if(temp==INF)break;
total+=temp;
vis[k]=;
for(int j=;j<N;j++)
if(!vis[j]&&low[j]>cost[k][j]-R*len[k][j])low[j]=cost[k][j]-R*len[k][j];
}
//printf("total=%lf R=%lf\n",total,R);
if(total>)return true;
else return false;
}
int main(){
while(scanf("%d",&N),N){
mem(len,INF);
mem(cost,INF);
double mxl=-INF,mil=INF,mxc=-INF,mic=INF;
for(int i=;i<N;i++)
scanf("%lf%lf%lf",&dt[i].x,&dt[i].y,&dt[i].h); for(int i=;i<N;i++){
for(int j=i+;j<N;j++){
len[j][i]=len[i][j]=getl(dt[i],dt[j]);
cost[j][i]=cost[i][j]=abs(dt[i].h-dt[j].h);
mxl=max(mxl,len[i][j]);
mxc=max(mxc,cost[i][j]);
mil=min(mil,len[i][j]);
mic=min(mic,cost[i][j]);
}
}
//printf("%lf %lf %lf %lf\n",mil,mic,mxl,mxc);
double l=mic/mxl,r=mxc/mil;//要是从0到mx会超时; // printf("%lf %lf\n",l,r);
while(r-l>1e-){
R=(l+r)/;
if(prime())l=R;
else r=R;
}
printf("%.3f\n",l);
}
return ;
}