HYSBZ 1050(队列-大小边比值最大的路径)

内容目录

已知边,判断2点连通性

要用并查集……千万别搜啊~

Program ee;
var
   edge:array[1..10000,1..3] of longint;
   s,t,n,m,i,j,pmax,pmin:longint;
   father:array[1..1000] of longint;

procedure swap(var a,b:longint);
var
   p:longint;
begin
   p:=a;
   a:=b;
   b:=p;
end;
procedure qsort(l,r:longint);
var
   i,j,m,p:longint;
begin
   i:=l;
   j:=r;
   m:=edge[(l+r) shr 1,3];
   repeat
      while edge[i,3]<m do inc(i);
      while edge[j,3]>m do dec(j);
      if i<=j then
      begin
         swap(edge[i,1],edge[j,1]);
         swap(edge[i,2],edge[j,2]);
         swap(edge[i,3],edge[j,3]);
         inc(i);dec(j);
      end;
   until i>j;
   if l<j then qsort(l,j);
   if i<r then qsort(i,r);

end;
function gcd(a,b:longint):longint;
begin
   if b=0 then exit(a) else exit(gcd(b,a mod b));
end;
function getfather(x:longint):longint;
begin
   if father[x]=x then exit(x) else father[x]:=getfather(father[x]);
   exit(father[x]);
end;
begin
   pmax:=50000000;pmin:=1;
   read(n,m);
   for i:=1 to m do read(edge[i,1],edge[i,2],edge[i,3]);
   read(s,t);
   qsort(1,m);
   i:=1;

   for i:=1 to m do
   begin
      for j:=1 to n do father[j]:=j;
      j:=i;
      while (getfather(s)<>getfather(t)) and (j<=m) do
      begin
         if getfather(edge[j,2])<>getfather(edge[j,1]) then father[father[edge[j,2]]]:=father[father[edge[j,1]]];
         inc(j);
      end;
      if getfather(s)=getfather(t) then
      begin
         dec(j);
         if (pmax/pmin>edge[j,3]/edge[i,3]) then
         begin
            pmax:=edge[j,3];
            pmin:=edge[i,3];
         end;
      end;

   end;



   if pmax=50000000 then writeln('IMPOSSIBLE')
   else if (pmax mod pmin=0) then writeln(pmax div pmin)
   else
   begin
      i:=gcd(pmax,pmin);
      pmax:=pmax div i;pmin:=pmin div i;
      writeln(pmax,'/',pmin);
   end;

end.