1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include <cstdio> #include <cstdlib> #include <cstring> #include <cstdlib> #include <algorithm> #include <cctype> #include <cmath> #define ll long long #define ri register int using std::sort; using std::min; using std::max; using std::swap; template <class T>inline void read(T &x){ x=0;int ne=0;char c; while(!isdigit(c=getchar()))ne=c=='-'; x=c-48; while(isdigit(c=getchar()))x=(x<<3)+(x<<1)+c-48; x=ne?-x:x;return ; } const int maxm=5005; const int maxn=505; const int inf=0x7fffffff; struct Edge{ int x,y,dis; bool operator <(const Edge &b)const{ return dis<b.dis; } }edge[maxm]; int num_edge=0; int n,m,s,t; int fa[maxn]; int get(int x){return fa[x]==x?fa[x]:(fa[x]=get(fa[x]));} int gcd(int a,int b){ return b?gcd(b,a%b):a; } int main(){ int x,y,v,xx,yy; bool flag=0; read(n),read(m); for(ri i=1;i<=m;i++){ read(x),read(y),read(v); edge[i].x=x,edge[i].y=y,edge[i].dis=v; } read(s),read(t); sort(edge+1,edge+1+m); int mx,cnt=0; double mi=inf; int fz,fm; for(ri i=1;i<=m;i++){ mx=-inf,flag=0; for(ri j=1;j<=n;j++)fa[j]=j; for(ri j=i;j<=m;j++){ x=edge[j].x,y=edge[j].y,v=edge[j].dis; xx=get(x),yy=get(y); if(xx==yy)continue; fa[xx]=yy; mx=max(mx,v); if(get(s)==get(t)){ flag=1;break; } } if(i==1&&get(s)!=get(t)){ puts("IMPOSSIBLE"); return 0; } else if(flag){ double tmp=(double)mx/edge[i].dis; if(tmp<mi){ flag=1; mi=tmp; fm=edge[i].dis,fz=mx; } } } int GCD=gcd(fz,fm); fm=fm/GCD,fz=fz/GCD; if(fm==1)printf("%d\n",fz); else printf("%d/%d\n",fz,fm); return 0; }
|