zoukankan      html  css  js  c++  java
  • 剑鱼行动(普里姆算法)

    题意

    Description

    给出N个点的坐标,对它们建立一个最小生成树,代价就是连接它们的路径的长度,现要求总长度最小。N的值在100以内,坐标值在[-10000,10000].结果保留二位小数

    Input

    5 ---------------5个点
    0 0 ---------------5个点点的坐标
    0 1
    1 1
    1 0
    0.5 0.5

    Output

    2.83




    分析

    先算出点与点之间的距离

    距离=sqrt(sqr(x[i]-x[j])+sqr(y[i]-y[j]))



    var
    n,i,j,k,t:longint;
    tj,min:real;
    a:array[0..200,0..200]of real;
    x,y:array[0..200]of real;
    f:array[0..200]of longint;
    begin
        readln(n);
        fillchar(a,sizeof(a),0);
        fillchar(f,sizeof(f),0);
        for i:=1 to n do
        readln(x[i],y[i]);
        for i:=1 to n do
        for j:=1 to n do
        a[i,j]:=sqrt(sqr(x[i]-x[j])+sqr(y[i]-y[j]));
        f[1]:=1;
        tj:=0;
        for i:=1 to n-1 do
        begin
            min:=maxlongint;
            for j:=1 to n do
            if f[j]=1 then
              for k:=1 to n do
              if f[k]=0 then
              if (a[j,k]<min)and(a[j,k]<>0) then
              begin
                  min:=a[j,k];
                  t:=k;
              end;
            if min<>maxint then
            begin
                tj:=tj+min;
                f[t]:=1;
            end;
        end;
        write(tj:0:2);
    end.



  • 相关阅读:
    springboot自定义jar打包不能导入到项目中
    ajax上传文件+传json数据
    cooladmin vitevue3 打包部署 nginx代理设置
    istio 安装
    logging
    orm 多对多
    shell 多线程
    舒服的jenkins子节点
    phpfpm coredump
    drf 自定义异常
  • 原文地址:https://www.cnblogs.com/YYC-0304/p/9500147.html
Copyright © 2011-2022 走看看