Desert King(最优比率生成树)
| 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 ;
}
總結
以上是生活随笔為你收集整理的Desert King(最优比率生成树)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅述实验室气流烘干器的使用注意事项
- 下一篇: 从JDK源码级深入剖析main方法的运行