BZOJ1083: [SCOI2005]繁忙的都市
对于第一问,要求用最少的边将所有点联通,那么一定是树的形式(树:无环连通图),n个节点就有n-1条边
对于第二问,随意指定一个节点作为源节点,跑一边最小生成树即可(蒟蒻用的是Prim)
【Code】
var
i,j,k,x,y,z,n,m,ans:longint;
map:Array[0..301,0..301]of integer;
function max(i,j:integer):integer;
begin
if i>j then exit(i);exit(j);
end;
procedure init;
begin
readln(n,m);
for i:=1 to n+1 do
for j:=1 to n+1 do
map[i,j]:=32767;
for i:=1 to m do begin
readln(x,y,z);
map[x,y]:=z; map[y,x]:=z;
end;
write(n-1,' ');
end;
procedure prim(v0:integer);
var
lowcost,closest:Array[0..301]of integer;
min,k:integer;
begin
for i:=1 to n+1 do begin
lowcost[i]:=map[v0,i];
closest[i]:=v0;
end;
lowcost[v0]:=0;
for i:=1 to n-1 do begin
min:=32767;
for j:=1 to n do
if (lowcost[j]<>0) then begin
min:=lowcost[j];
k:=j;
end;
ans:=max(ans,lowcost[k]);
lowcost[k]:=0;
for j:=1 to n do
if (map[j,k]
lowcost[j]:=map[j,k];
closest[j]:=k;
end
end
end;
begin
init;
prim(1);
writeln(ans);
end.