Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我的Prim怎么会超时?

Posted by JiangLY at 2005-05-06 22:25:38 on Problem 2421
const 
  nv=100;
type
  r=record
      vex1,vex2:1..nv;
      w:longint;
    end;
  set1=set of 1..nv;
  settype=array[1..nv] of set1;
var
  n,l,q,a,b:integer;
  stev:settype;
  ans:longint;
  m:array[1..nv*nv] of r;
procedure init;
var
  i,j:integer;
begin
  readln(n);
  l:=0;
  ans:=0;
  for i:=1 to n do
    begin
      for j:=1 to n do
        begin
          inc(l);
          m[l].vex1:=i;
          m[l].vex2:=j;
          read(m[l].w);
        end;
      readln;
    end;
  readln(q);
  for i:=1 to q do
    begin
      readln(a,b);
      m[(a-1)*n+b].w:=0;
    end;
end;
procedure sort;
var
  i,j:integer;
  k:r;
begin
  for i:=1 to l-1 do
    for j:=i+1 to l do
      if m[i].w>m[j].w then
        begin
          k:=m[i];
          m[i]:=m[j];
          m[j]:=k;
        end;
end;
function find(var setv:settype;vex:integer):integer;
var
  i:integer;
begin
  i:=1;
  while (i<=nv)and(not(vex in setv[i])) do inc(i);
  if i<=nv then find:=i
           else find:=0;
end;
procedure kruskal(var setv:settype);
var
  i,n,u,v,u1,v1,k:integer;
begin
  k:=1;
  n:=nv;
  for i:=1 to nv do setv[i]:=[i];
  while (l>1)and(k<=l) do
    begin
      u:=m[k].vex1;
      v:=m[k].vex2;
      u1:=find(stev,u);
      v1:=find(stev,v);
      if u1<>v1 then
        begin
          inc(ans,m[k].w);
          stev[u1]:=stev[u1]+stev[v1];
          stev[v1]:=[];
          dec(l);
        end;
      inc(k);
    end;
end;
begin
  while not eof do
    begin
      init;
      sort;
      kruskal(stev);
      writeln(ans);
    end;
end.

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator