新年趣事之红包(Vijos1069)

本文介绍了一种使用最小生成树解决特定问题的贪心算法实现方法。通过计算任意两点间的距离,并运用最小生成树算法来找到连接所有点的最短路径。
算法:贪心

分析:虽然放在了DP里面,但是怎么都不觉得这是一道DP的题目。
      先求出任意两点的距离,然后用最小生成树的方法做。

      cai的代码也有错误啊~

program Vijos1069;

const
 maxn=800;
 
type
 atp=record
  x,y:real;
 end;
 
var
 n:longint;
 ans:real;
 a:array [0..maxn] of atp;
 d:array [0..maxn,0..maxn] of real;
 b:array [0..maxn] of boolean;
 
procedure init;
var
 i:longint;
begin
 ans:=0;
 fillchar(b,sizeof(b),false);
 readln(n);
 for i:=1 to n do readln(a[i].x,a[i].y);
end;

procedure main;
var
 i,j,minp:longint;
 min:real;
begin
 for i:=1 to n do for j:=1 to n do d[i,j]:=sqrt(sqr(a[i].x-a[j].x)+sqr(a[i].y-a[j].y));
 b[1]:=true;
 for i:=2 to n do
  begin
   min:=maxlongint;
   for j:=2 to n do 
    begin
     if (not b[j]) and (d[1,j]<min) then
      begin
       min:=d[1,j];
       minp:=j;
      end;
    end;
   b[minp]:=true;
   ans:=ans+min;
   for j:=2 to n do if (not b[j]) and (d[minp,j]<d[1,j]) then d[1,j]:=d[minp,j];
  end;
end;

begin
 assign(input,'VJ1069.in'); reset(input);
 assign(output,'VJ1069.out'); rewrite(output);
 
 init;
 main;
 writeln(ans:0:3);
 
 close(input); close(output);
end.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值