Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const
- s = 5001;
- var
- g:array[0..s,0..s] of longint;
- used:array[0..s] of boolean;
- d:array[0..s] of longint;
- i,j,n,m,u,v,x,y,z,min,mst:longint;
- begin
- read(n,m);
- for i:=1 to m do
- begin
- read(x,y,z);
- g[x][y]:=z;
- g[y][x]:=z;
- end;
- for i:=2 to n do
- d[i]:=maxlongint;
- for i:=1 to n do
- begin
- min:=maxlongint;
- for j:=1 to n do
- if (d[j] < min) and (not used[j]) then
- begin
- min:=d[j];
- u:=j;
- end;
- used[u]:=true;
- inc(mst,d[u]);
- for j:=1 to n do
- if (d[j] > g[u][j]) and (not used[j]) then
- d[j]:=g[u][j]
- end;
- write(mst);
- end.
Advertisement
Add Comment
Please, Sign In to add comment