网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
09月22日漏签0天
noip吧 关注:25,175贴子:642,082
  • 看贴

  • 图片

  • 吧主推荐

  • 视频

  • 游戏

  • 14回复贴,共1页
<<返回noip吧
>0< 加载中...

求二叉堆优化过的Prim代码

  • 只看楼主
  • 收藏

  • 回复
  • CMS_Flash
  • 提高三等
    5
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
网上好像都是朴素的,我自己写了个就是过不去
非常感谢!!


  • Heltion
  • NOI金牌
    12
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
用二叉堆实现优先队列么 不如学白书priority_queue


2025-09-22 16:16:02
广告
不感兴趣
开通SVIP免广告
  • plmoknijbuhv26
  • 进队爷
    13
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
朴素的就挺好啊。。稀疏图用kruskal


  • 计0划0环0境
  • NOI银牌
    11
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
kruskal大法好
你要实在过不去把排序换成桶排
O(n)


  • 罗不理
  • 提高一等
    7
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
。。。丢两个模板上来您自己选吧
const maxn=200000;
maxe=200000;
type edge=record
a,b:longint;
len:int64;
end;
type arr=record
x,y,z:longint;
end;
var edges:array[0..maxe]of edge;
p,r,check:array[0..maxn]of int64;
heart:array [0..maxn] of arr;
n,e:longint;
procedure swap(a,b:longint);
begin
edges[0]:=edges[a];
edges[a]:=edges[b];
edges[b]:=edges[0];
end;
procedure qsort(l,r:longint);
var x,i,j:longint;
begin
x:=edges[random(r-l+1)+l].len;
i:=l;j:=r;
repeat
while edges[i].len<x do inc(i);
while edges[j].len>x do dec(j);
if i<=j then
begin
swap(i,j);
inc(i);dec(j);
end
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end;
function min(x,y:int64):int64;
begin
if x<y then exit(x)
else exit(y);
end;
procedure init;
var i,j:longint;
begin
readln(n,e);
for i:=1 to e do read(edges[i].a,edges[i].b,edges[i].len);
for i:=1 to n do p[i]:=i;
randomize;
end;
function find(i:longint):longint;
begin
if p[i]=i then exit(i);
if p[p[i]]=0 then exit(p[i]);
find:=find(p[i]);
p[i]:=find;
end;
procedure union(a,b:longint);
var t:longint;
begin
a:=find(a);
b:=find(b);
if r[a]>r[b] then begin t:=a;a:=b;b:=t end;
if r[a]=r[b]then inc(r[a]);
p[a]:=b;
end;
procedure kruskal;
var en:longint;
count,xx,yy:longint;
tot,ans:int64;
begin
count:=0; en:=0; tot:=0; ans:=maxlongint;
while count<n-1 do
begin
inc(en);
with edges[en] do
begin
if find(a)<>find(b) then
begin
union(a,b);
inc(tot,len);
inc(count);
end;
end;
end;
writeln(tot);
end;
begin
init;
kruskal;
end.
上面是kruskal+ufs的
type node=record
x,y,next:longint;
end;
var pos,closest,lowest,heap:array [1..100] of longint;
a:array [1..100,1..100] of longint;
v:array [1..100] of boolean;
tot,n,ans:longint;
function init:boolean;
var i,j,x,y,z:longint;
begin
read(n);
for i:=1 to n do
begin
read(x,y,z);
if a[x,y]=0 then
begin
a[x,y]:=z;
a[y,x]:=z;
end
else
if a[x,y]>z then
begin
a[x,y]:=z;
a[y,x]:=z;
end;
end;
exit(true);
end;
procedure swap(var i,j:longint);
var t:longint;
begin
t:=i;
i:=j;
j:=t;
end;
procedure down(l:longint);
begin
while l*2<=tot do
begin
l:=l*2;
if l<tot then
if lowest[heap[l]]>lowest[heap[l+1]] then inc(l);
if lowest[heap[l]]>=lowest[heap[l div 2]] then exit;
swap(pos[heap[l]],pos[heap[l div 2]]);
swap(heap[l],heap[l div 2]);
end;
end;
procedure up(l:longint);
begin
while l>1 do
begin
if lowest[heap[l]]>=lowest[heap[l div 2]] then exit;
swap(pos[heap[l]],pos[heap[l div 2]]);
swap(heap[l],heap[l div 2]);
l:=l div 2;
end;
end;
procedure make_heap;
var i,j:longint;
begin
for i:=1 to n do
begin
heap[i]:=i;
pos[i]:=i;
end;
for i:=1 to n do
begin
lowest[i]:=a[1,i];
up(pos[i]);
end;
end;
procedure heap_prim;
var i,j,k:longint;
begin
tot:=n;
fillchar(v,sizeof(v),true);
v[1]:=false;
for i:=1 to n-1 do
begin
k:=heap[1];
swap(pos[heap[1]],pos[heap[tot]]);
swap(heap[1],heap[tot]);
dec(tot);
inc(ans,lowest[k]);
lowest[k]:=maxlongint;
v[k]:=false;
down(1);
for j:=1 to n do
if (a[k,j]<lowest[j]) and v[j] then
begin
lowest[j]:=a[k,j];
up(pos[j]);
end;
end;
writeln(ans);
end;
begin
while init do
begin
make_heap;
heap_prim;
end;
end.
prim+heap


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 14回复贴,共1页
<<返回noip吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示