题目描述 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
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int Father[505];
struct node
{
int a,b,w;
bool operator<(const node &t)const
{
return w<t.w;
}
};
int Find(int x)
{
if(x!=Father[x])
{
Father[x]=Find(Father[x]);
}
return Father[x];
}
void Union(int x,int y)
{
x=Find(x);
y=Find(y);
if(x!=y)
{
Father[x]=y;
}
}
int gcd(int x,int y)
{
if(x%y==0)
{
return y;
}
return gcd(y,x%y);
}
int main()
{
int N,M,s,t;
node num[5005];
while (scanf("%d%d",&N,&M)!=EOF)
{
for (int i=0; i<M; i++)
{
scanf("%d%d%d",&num[i].a,&num[i].b,&num[i].w);
}
scanf("%d%d",&s,&t);
double min=100000.0;
int ans[2]={-1,-1};
sort(num, num+M);
for (int i=0; i<M; i++) //枚举
{
for (int j=1; j<=N; j++)
{
Father[j]=j;
}
for (int j=i; j<M; j++)
{
Union(num[j].a, num[j].b);
if(Find(s)==Find(t))
{
if(min*num[i].w>=num[j].w*1.0)
{
min=num[j].w*1.0/num[i].w;
ans[0]=num[i].w;
ans[1]=num[j].w;
break;
}
}
}
}
if(ans[0]==-1)
{
cout<<"IMPOSSIBLE"<<endl;
}
else
{
int x=gcd(ans[1],ans[0]);
ans[1]/=x;
ans[0]/=x;
if(ans[1]%ans[0]==0)
{
cout<<ans[1]/ans[0]<<endl;
continue;
}
cout<<ans[1]<<"/"<<ans[0]<<endl;
}
}
return 0;
}