Luogu P2502 travel SSL_1226 (concurrent search)

;

Title Description

Z town is a beautiful place, attracting tourists from all over the world to visit. There are N scenic spots (No. 1, 2, 3,...) near Z town , N), these scenic spots are connected by M roads, all roads are two-way, and there may be multiple roads between the two scenic spots. Perhaps in order to protect the tourism resources of the place, town Z has a strange rule that for a given road Ri, any vehicle speed on the road must be Vi. The speed change is so fast that the tourists are uncomfortable. Therefore, when they go from one scenic spot to another, they all want to choose the route with the maximum speed and the minimum speed ratio as small as possible, that is, the so-called most comfortable route.

I / O format

Input format:
The first line contains two positive integers, N and M.

The next M lines contain three positive integers: x, y, and V. Indicates that there is a two-way road between attraction x and attraction y, on which vehicles must travel at speed v.

The last line contains two positive integers s, t, indicating the path from scenic spot s to scenic spot T with the minimum speed ratio. S and t may not be the same.

Output format:
If there is no path from scenic spot s to scenic spot t, output "IMPOSSIBLE". Otherwise, a number is output, indicating the minimum speed ratio. If necessary, output a reduced score.

Example of input and output

Input example ා 1:
4 2
1 2 1
3 4 2
1 4
Output example:
IMPOSSIBLE
Enter sample 2:
3 3
1 2 10
1 2 5
2 3 8
1 3
Output sample 2:
5/4
Input sample 3:
3 2
1 2 2
2 3 4
1 3
Output sample 3:
2
explain

[data range]

1

var
   x,y,v:array [0..5001] of longint;
   f:array [0..501] of longint;
   s,t,p,q,min,max,i,j,n,m:longint;

procedure qsort(l,r:longint);
var
   i,j,mid,t:longint;
begin
    if l>=r then exit;
    i:=l; j:=r;
    mid:=v[(l+r) div 2];
       repeat
            while v[i]<mid do inc(i);
            while v[j]>mid do dec(j);
            if i<=j then
               begin
                   x[0]:=x[i];x[i]:=x[j];x[j]:=x[0];
                   y[0]:=y[i];y[i]:=y[j];y[j]:=y[0];
                   v[0]:=v[i];v[i]:=v[j];v[j]:=v[0];
                   inc(i); dec(j);
               end;
       until i>j;
    qsort(i,r);
    qsort(l,j);
end;

function find(a:longint):longint;
begin
    if f[a]=a then exit(a);
    f[a]:=find(f[a]);
    exit(f[a]);
end;

function gys(ab,xy:longint):longint;
begin
    if xy=0 then exit(ab)
            else gys:=gys(xy,ab mod xy);
end;

begin
    readln(n,m);
    for i:=1 to m do readln(x[i],y[i],v[i]);
    readln(s,t);
    qsort(1,m);
    min:=1; max:=50000;
    for i:=1 to m do
        begin
           for j:=1 to n do f[j]:=j;
           j:=i-1;
           p:=v[i];
           while (j<=m) and (find(s)<>find(t)) do
                 begin
                      inc(j);
                      f[find(x[j])]:=f[find(y[j])];
                      q:=v[j];
                 end;
           if find(s)<>find(t) then break;
           if q/p<max/min then
              begin
                   min:=p;
                   max:=q;
              end;
        end;
    if max=50000
       then writeln('IMPOSSIBLE')
       else begin
                p:=gys(max,min);
                if max mod min=0 then writeln(max div min)
                                 else writeln(max div p,'/',min div p);
            end;
end.

Posted by cvjacques on Tue, 19 May 2020 08:11:24 -0700