[最小生成树][迭代]POJ#2728 Desert King解题笔记

题面

传送门

Desert King

Time Limit: 3000MS Memory Limit: 65536K Total
Submissions: 38055 Accepted: 10132 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

解题笔记

题目大意

给定若干村庄的xyz坐标,xy平面上的距离为视作水渠距离,z轴方向上的距离视作花费,要求求出联通所有村庄的总花费比总距离的最小值

思路

最小生成树,但是有所改动,是一道最优比率题。(或者你也可以看作线性规划的01分数规划题,那么这个思路应该叫Dinkelbach算法)二分时间应该会长一些,我选择的迭代法,据题意可以列出
r a t e = ∑ A l t i t u d e i ∑ d i s t a n c e i ⋅ c h e c k e d i rate=\frac{\sum Altitude_i}{\sum distance_i} \cdot checked_i rate=distanceiAltitudeicheckedi
其中 A l t i t u d e i Altitude_i Altitudei是每条边的海拔落差,即花费; d i s t a n c e i distance_i distancei是每条边的水平距离, c h e c k e d i checked_i checkedi代表是否选择这条边,为0或1。变形可得:
ϵ = ( ∑ A l t i t u d e i − r a t e ⋅ ∑ d i s t a n c e i ) ⋅ c h e c k e d i \epsilon =(\sum Altitude_i - rate \cdot \sum distance_i) \cdot checked_i ϵ=(Altitudeiratedistanceicheckedi
便可以通过迭代跑最小生成树控制误差 ϵ \epsilon ϵ

踩坑

这道题卡了很久,做题过程中几经波折,值得记录下。一开始想到的是二分+最短路,实现了一下,最短路用的kruskal,发现TLE,网上找题解说是迭代法比较快,又学习了一番,实现了,发现还是TLE,决定改用prim,prim顺手写了个优先队列,然后就…又TLE了T-T。然后又去学习,才反应过来题中是个完全图的原因,优先队列( O ( ( n + m ) l o g 2 n ) O((n+m)log_2n) O(n+mlog2n),节点数为n,边为m,下同)的优化是负的,写个普通的搜索( O ( n 2 ) O(n^2) O(n2))反而会比优先队列快。因为完全图 m = n ( n − 1 ) = n 2 − n m=n(n-1)=n^2-n m=n(n1)=n2n则优先队列 O ( n 2 l o g 2 n ) O(n^2log_2 n) O(n2log2n)是普通搜索( O ( n 2 ) O(n^2) O(n2))个 l o g 2 n log_2 n log2n倍,在100%数据下近10倍,难怪会TLE。
总结教训就是稠密图不要用优先队列优化,或者说做数据量较大的题时需要对比一下时间复杂度,优中选优,才能避免像这题一样卡时间。

AC代码

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;

typedef long long ll;



struct village
{
    int x,y,z;
};


struct edge
{
    int alt;
    double dis,w;
    // edge(int v_, int alt_, double dis_, double w_):
    //     v(v_),alt(alt_),dis(dis_),w(w_){}
};

const int maxn = 1001;
const double eps=1e-3;
int n;
edge e[maxn][maxn];
edge dis[maxn];
village vil[maxn];
bool visited[maxn];

inline double cald(village a, village b){
    return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

int findMin(){
    double min=1e9;
    int k=-1;
    for(int i = 1; i <= n; i++)
        if(!visited[i]&&dis[i].w<min)
			k=i,min=dis[i].w;
	return k;
}

double prim(int s, double r){
    double dissum=0, altsum=0;
    for(int i = 1; i <= n; i++){
        for(int j =1; j <=i; j++)
            e[i][j].w=e[j][i].w=(double)e[i][j].alt-r*e[i][j].dis;
    dis[i]=e[s][i];
    visited[i]=false;
    }
    dis[s].w=0;
    visited[s]=true;
    for(int i=1,j; i<n; i++){
        j=findMin();
        if(j==-1)break;
        // if(dis[tmp.sn]!=tmp.dis)continue;
        visited[j]=1;\
        dissum+=dis[j].dis;
        altsum+=dis[j].alt;
        for(int k=1; k<=n; k++){
            if(!visited[k]&&dis[k].w>e[j][k].w)
			{	dis[k].alt=e[j][k].alt;
				dis[k].dis=e[j][k].dis;
				dis[k].w=e[j][k].w;
			}
        }
        
    }
    return altsum/dissum;
}

int main(){
    double preRate,rate;
    while(scanf("%d",&n) && n!=0){
        for(int i = 1; i <=n; i++){
            scanf("%d%d%d",&vil[i].x,&vil[i].y,&vil[i].z);
            for(int j = 1; j <= i; j++){
                e[i][j].alt=e[j][i].alt=abs(vil[i].z-vil[j].z);
                e[j][i].dis=e[i][j].dis=cald(vil[i],vil[j]);
            }
        }
        preRate=0;
        while(1)
		{	rate=prim(1,preRate);
			if(fabs((double)preRate-rate)<eps)	break;
			preRate=rate;
		}
        printf("%.3lf\n",rate);
    }
    // system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

McDuck_Spirit

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值