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

kruskal变形

Posted by diaoyuan at 2011-07-20 15:04:58 on Problem 2253
程序如下:
program frogger;
var   o,k,y,i,j,s,x1,x2,n,g1,g2,i2,j2:integer;
x,x3:real;bb,b1:boolean;
a:array[0..40000] of real;
b,q:array[0..40000,1..2] of integer;
p:array[1..40000] of integer;
procedure qsort(l,r:integer);
 begin
 i:=l;
 j:=r;
 x:=a[(l+r) div 2];
 repeat
  while a[i]<x do inc(i);
  while a[j]>x do  dec(j);
  if i<=j then
   begin
   x3:=a[i];
   a[i]:=a[j];
   a[j]:=x3;
   y:=b[i,1];
   b[i,1]:=b[j,1];
   b[j,1]:=y;
    y:=b[i,2];
   b[i,2]:=b[j,2];
   b[j,2]:=y;

   inc(i);
   dec(j);
   end;
   until i>j;

  if i<r then qsort(i,r);
  if l<j then qsort(l,j);
  end;

function find(x:integer):integer;
 begin
  s:=x;
  while p[s]>=0 do s:=p[s];
  while s<>x do
   begin
    y:=p[x];
    p[x]:=s;
    x:=y;
   end;
   find:=s;
 end;

 procedure union(r1,r2:integer);
  begin
  x1:=find(r1);
  x2:=find(r2);
  y:=p[x1]+p[x2];
  if p[x1]>p[x2] then
   begin
    p[x1]:=x2;
    p[x2]:=y;
   end
   else
   begin
    p[x2]:=x1;
    p[x1]:=y;
   end;
  end;

 begin

   b1:=true;  o:=0;
  while b1  do
  begin
 readln(n); if n=0 then break;o:=o+1;
   fillchar(p,sizeof(p),0);

  for i:=1 to n do p[i]:=-1;
  for i:=1 to n do readln(q[i,1],q[i,2]);
 k:=0;
 for i:=1 to n-1 do
 for j:=i+1   to n do

 if i<> j then
  begin

   k:=k+1;
   a[k]:=sqrt(sqr(abs(q[i,1]-q[j,1]))+sqr(abs(q[i,2]-q[j,2])));
   b[k,1]:=i;
   b[k,2]:=j;
  end;
  qsort(1,k);
  bb:=true;
  n:=1;
  while bb do
   begin
    i2:=b[n,1];
    j2:=b[n,2];
    g1:=find(i2);
    g2:=find(j2);
    if g1<>g2 then
     begin
      n:=n+1;

      union(i2,j2);
       g1:=find(1);
    g2:=find(2);
    if g1=g2 then
     begin
    bb:=false;
    write('Scenario #');writeln(o);
    writeln('Frog Distance = ',a[n-1]:0:3);
      writeln;
      end;
     end
      else n:=n+1;

   end;
  readln;
  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