f:array[0..1000] of longint;
i,j,k,m,n :longint;
procedure init;
var x,y:integer;
begin
fillchar(a,sizeof(a),0);
readln(n,m);
for i:=1 to m do
begin
read(x,y);
readln(a[x,y]);
a[y,x]:=a[x,y];
end;
end;
procedure work;
var mark:array[1..1000] of boolean;
minj:longint;
begin
fillchar(mark,sizeof(mark),0);
mark[1] :=true;
f[0]:=maxlongint;
k:=0;
f[1]:=0;
for i:=2 to n do
if a[1,i]<>0 then f[i]:=a[1,i] else f[i]:=maxlongint;
for i:=1 to n-1 do
begin
minj:=0;
for j:=2 to n do
if not(mark[j]) and (f[j]<f[minj]) then minj:=j;
if minj<>0 then
begin
k:=k+f[minj];
mark[minj]:=true;
for j:=1 to n do
if not(mark[j]) and (a[minj,j]<>0) and (f[j]>a[minj,j])
then f[j]:=a[minj,j];
end;
end;
writeln(k);
end;
begin
init;
work;
end.
procedure prim(v0:integer);
var
lowcost,closest:array[1..maxn] of integer;
i,j,k,min:integer;
begin
for i:=1 to n do begin
lowcost[i]:=cost[v0,i];
closest[i]:=v0;
end;
for i:=1 to n-1 do begin
{寻找离生成树最近的未加入顶点k}
min:=maxlongint;
for j:=1 to n do
if (lowcost[j]<min) and (lowcost[j]<>0) then begin
min:=lowcost[j];
k:=j;
end;
lowcost[k]:=0; {将顶点k加入生成树}
{生成树中增加一条新的边k到closest[k]}
{修正各点的lowcost和closest值}
for j:=1 to n do
if cost[k,j]<lowcost[j] then begin
lowcost[j]:=cost[k,j];
closest[j]:=k;
end;
end;
end;{prim}
================
在本题中只要有lowcost即可,closest没有用。
var n,m,i,j,k,ko,a,x,y:longint;
ans:longint;
len:array[1..1000]of longint;
te:array[1..1000]of boolean;
g:array[1..1000,1..1000]of longint;
begin
readln(n,m);
for i:=1 to n do
for j:=1 to n do g[i,j]:=36000;
for i:=1 to m do
begin
readln(x,y,a);
g[x,y]:=a;
g[y,x]:=a;
end;
te[1]:=true;
ans:=0;
for i:=2 to n do len[i]:=g[1,i];
for k:=2 to n do
begin
ko:=36000;
for j:=2 to n do
if not te[j] and (ko>len[j]) then
begin
i:=j;
ko:=len[j];
end;
inc(ans,ko);
te[i]:=true;
for j:=2 to n do
if not te[j] and (g[i,j]<len[j]) then len[j]:=g[i,j];
end;
writeln(ans);
end.
b:array[1..1000] of longint;
f:array[1..1000] of boolean;
a:array[1..1000,1..1000] of longint;
t,jl,j,n,e,i,tmp1,tmp2,l,sum:longint;
procedure dd;
var
i,j:longint;
begin
t:=maxlongint;
for i:=1 to n do if a[jl,i]<b[i] then b[i]:=a[jl,i];
for i:=1 to n do if (f[i]=false)and(b[i]<t) then begin t:=b[i];jl:=i;end;
f[jl]:=true;
end;
begin
readln(n,e);
for i:=1 to n do
for j:=1 to n do a[i,j]:=maxlongint;{初始化}
for i:=1 to e do
begin
readln(tmp1,tmp2,l);{读邻接矩阵}
if (l<a[tmp1,tmp2])or(a[tmp1,tmp2]=maxlongint) then
begin
a[tmp1,tmp2]:=l;
a[tmp2,tmp1]:=l;
end;
end;
for i:=1 to n do b[i]:=maxlongint;
jl:=1;
f[1]:=true;{集合里一开始只有顶点1}
for i:=1 to n-1 do{只需要N-1条边即可生成}
begin
dd;
sum:=sum+t;
end;
write(sum);
end.
#include<stdio.h>
int hash[1001]={0};
long a[1001][1001]={0};
long b[1001]={0};
main()
{
long i,j,k,l,m,n;
long min=0;
long mins=200000000;
long vi,vj;
scanf("%ld%ld",&n,&m);
for(i=0;i<m;i++)
{
scanf("%ld%ld%ld",&vi,&vj,&k);
vi--;vj--;
a[vi][vj]=k;
a[vj][vi]=k;
}
for(i=1;i<n;i++)
{
b[i]=a[0][i];
if(b[i]!=0&&b[i]<mins)
{
mins=b[i];
vi=i;
}
}
l=2;
hash[0]=1;
hash[vi]=1;
min+=b[vi];
while(l<n)
{mins=200000000;
vj=vi;
for(i=1;i<n;i++)
{
if((a[vj][i]<b[i]||b[i]==0)&&a[vj][i]!=0&&hash[i]==0)
b[i]=a[vj][i];
if(b[i]<mins&&b[i]!=0&&hash[i]==0)
{
vi=i;
mins=b[i];
}
}
hash[vi]=1;
min+=b[vi];
l++;
}
printf("%ld",min);
}
————————————————————————
测试结果1: 通过本测试点|有效耗时172:ms
测试结果2: 测试结果错误.错误结果为:59271
正确结果应为:86387
测试结果3: 测试结果错误.错误结果为:59271
正确结果应为:139949
测试结果4: 测试结果错误.错误结果为:59271
正确结果应为:83199
测试结果5: 测试结果错误.错误结果为:59271
正确结果应为:165204
测试结果6: 测试结果错误.错误结果为:59271
正确结果应为:42880
测试结果7: 测试结果错误.错误结果为:59271
正确结果应为:37870
测试结果8: 测试结果错误.错误结果为:59271
正确结果应为:159982
测试结果9: 测试结果错误.错误结果为:59271
正确结果应为:50493
测试结果10: 通过本测试点|有效耗时62:ms
提交代码: var n,m,i,j:longint;
selected:array[1..1000] of longint;
e:array[1..1000,1..2] of longint;
value:array[1..1000] of longint;
t:array[1..1000] of longint;
min,mine,valuet:longint;
begin
read(n,m);
for i:=1 to m do readln(e[i,1],e[i,2],value[i]);
fillchar(selected,sizeof(selected),0);
fillchar(t,sizeof(t),0);
valuet:=0;
for i:=1 to n-1 do
begin
min:=maxlongint;
mine:=0;
for j:=1 to m do
if selected[j]=0 then
if ((t[e[j,1]]=0) xor (t[e[j,2]]=0)) or (i=1) then
if value[j]<min then
begin
min:=value[j];
mine:=j;
end;
selected[mine]:=1;
t[e[mine,1]]:=1;
t[e[mine,2]]:=1;
valuet:=valuet+min;
end;
writeln(valuet);
end.