样例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 样例32
代码:
var flag,s1,t1,s,t,n,m,i,j:longint; f,v,x,y:array[0..50000]of longint; function gcd(x,y:longint):longint; begin if x mod y=0 then exit(y); gcd:=gcd(y,x mod y); end; procedure qsort(l,r:longint); var i,j,t:longint; begin i:=l;j:=r;t:=v[random(l+r-1)]; repeat while v[i]>t do inc(i); while v[j]<t 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; if l<j then qsort(l,j); if i<r then qsort(i,r); end; function father(x:longint):longint; begin if f[x]=x then exit(x); f[x]:=father(f[x]); exit(f[x]); end; procedure main(xx:longint); var i,j,k:longint; begin for i:=1 to n do f[i]:=i; for i:=xx to m do begin f[father(x[i])]:=father(y[i]); if father(s)=father(t) then begin inc(flag); if flag=1 then begin s1:=v[xx]; t1:=v[i]; end; if (v[xx]/v[i])<(s1/t1) then begin t1:=v[i]; s1:=v[xx]; end; end; end; end; begin readln(n,m); for i:=1 to m do readln(x[i],y[i],v[i]); qsort(1,m); readln(s,t); for i:=1 to m do main(i); if flag=0 then writeln('IMPOSSIBLE') else if s1 mod t1=0 then writeln(s1 div t1) else writeln(s1 div gcd(s1,t1),'/',t1 div gcd(s1,t1)); end.
