水题.枚举最大边后用并查集维护连通性.
AC code:
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=10010;
int n,m,S,T,a,b;
int f[N];
struct Edge{
int u,v,w;
friend bool operator<(Edge x,Edge y){
return x.w<y.w;
}
}E[N];
int find(int x){
if(f[x]==x) return x;
f[x]=find(f[x]);
return f[x];
}
int gcd(int x,int y){
return y?gcd(y,x%y):x;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++) scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].w);
scanf("%d%d",&S,&T);
sort(E+1,E+m+1);
for(int i=2;i<=m;i++){
for(int j=1;j<=n;j++) f[j]=j;
f[find(E[i].u)]=find(E[i].v);
if(find(S)==find(T)){
printf("1\n");
return 0;
}
for(int j=i-1;j>=1;j--){
f[find(E[j].u)]=find(E[j].v);
if(find(S)==find(T)){
if((!a)||(double)E[i].w/E[j].w<(double)a/b){
a=E[i].w;b=E[j].w;
}
break;
}
}
}
if(!a) printf("IMPOSSIBLE\n");
else{
int g=gcd(a,b);
a/=g;b/=g;
if(b==1) printf("%d\n",a);
else printf("%d/%d\n",a,b);
}
return 0;
}