wikioi1001 舒适的路线

来源:互联网 发布:ipad怎么删除软件 编辑:程序博客网 时间:2024/06/10 07:28
题目描述 Description

Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光。
Z小镇附近共有
N(1<N≤500)个景点(编号为1,2,3,…,N),这些景点被M(0<M≤5000)条道路连接着,所有道路都是双向的,两个景点之间可能有多条道路。也许是为了保护该地的旅游资源,Z小镇有个奇怪的规定,就是对于一条给定的公路Ri,任何在该公路上行驶的车辆速度必须为Vi。频繁的改变速度使得游客们很不舒服,因此大家从一个景点前往另一个景点的时候,都希望选择行使过程中最大速度和最小速度的比尽可能小的路线,也就是所谓最舒适的路线。

输入描述 Input Description

第一行包含两个正整数,N和M。
接下来的M行每行包含三个正整数:x,y和v(1≤x,y≤N,0 最后一行包含两个正整数s,t,表示想知道从景点s到景点t最大最小速度比最小的路径。s和t不可能相同。

输出描述 Output Description

如果景点s到景点t没有路径,输出“IMPOSSIBLE”。否则输出一个数,表示最小的速度比。如果需要,输出一个既约分数。

样例输入 Sample Input

样例1
4 2
1 2 1
3 4 2
1 4

样例2
3 3
1 2 10
1 2 5
2 3 8
1 3

样例3
3 2
1 2 2
2 3 4
1 3

样例输出 Sample Output

样例1
IMPOSSIBLE

样例2
5/4

样例3
2

数据范围及提示 Data Size & Hint

N(1<N≤500)

M(0<M≤5000)


题解:将边排序,每次枚举一条边当做最大的边,在比它小的边里找到能使s,t连接的最大的一条(保证比值最小),对于是否联通用并查集即可

const  maxn=5000;  maxm=50000;var  edge:array[0..maxn,1..3]of longint;  fa,r:array[1..maxn]of longint;  n,m,s,t,p,i,j:longint;  ans:real;  ansx,ansy:longint;function find(x:longint):longint;begin  if fa[x]=x then exit(x);  fa[x]:=find(fa[x]);  find:=fa[x];end;function gcd(x,y:longint):longint;begin  if x mod y=0 then exit(y);  gcd:=gcd(y,x mod y);end;procedure union(x,y:longint);var  u,v:longint;begin  u:=find(x);v:=find(y);  if u<>v then  begin    if r[u]<=r[v] then    begin      fa[u]:=v;      if r[u]=r[v] then inc(r[v]);    end    else fa[v]:=u;  end;end;procedure qsort(l,r:longint);var  i,j,key:longint;begin  if l>r then exit;  i:=l;j:=r;  key:=edge[(l+r)shr 1,3];  repeat    while edge[i,3]<key do inc(i);    while edge[j,3]>key do dec(j);    if i<=j then    begin      edge[0]:=edge[i];edge[i]:=edge[j];edge[j]:=edge[0];      inc(i);dec(j);    end;  until i>j;  qsort(l,j);  qsort(i,r);end;procedure init;var  i:longint;begin  readln(n,m);  for i:=1 to m do    readln(edge[i,1],edge[i,2],edge[i,3]);  readln(s,t);  ans:=maxlongint;end;procedure clean;var  i:longint;begin  for i:=1 to n do    fa[i]:=i;end;begin  init;  qsort(1,m);  for i:=m downto 1 do  begin    clean;    for j:=i downto 1 do      if find(edge[j,1])<>find(edge[j,2]) then      begin        union(edge[j,1],edge[j,2]);        if find(s)=find(t) then break;      end;    if find(s)<>find(t) then break;    if edge[i,3]/edge[j,3]<ans then    begin      ansx:=edge[i,3];ansy:=edge[j,3];      ans:=edge[i,3]/edge[j,3];    end;  end;  if ans>100000000 then writeln('IMPOSSIBLE') else  begin    p:=gcd(ansx,ansy);    ansx:=ansx div p;ansy:=ansy div p;    if ansx mod ansy=0 then writeln(ansx div ansy) else    writeln(ansx div p,'/',ansy div p);  end;end.


0 0