旅行

来源:互联网 发布:传奇3数据库软件 编辑:程序博客网 时间:2024/04/27 17:13

Description

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

Input

 第一行包含两个正整数,N和M。
  接下来的M行每行包含三个正整数:x,y和v。表示景点x到景点y之间有一条双向公路,车辆必须以速度v在该公路上行驶。
  最后一行包含两个正整数s,t,表示想知道从景点s到景点t最大最小速度比最小的路径。s和t不可能相同。

Output

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

Sample Input

样例14 21 2 13 4 21 4样例23 31 2 101 2 52 3 81 3样例33 21 2 22 3 41 3

Sample Output

样例1IMPOSSIBLE校例25/4样例32

Hint

1<N<=500
1<=x,y<=N,0<v<30000,x≠y
0<M<=5000

HNOI


题解:用并查集判断是否可以到达,快排,然后枚举。(dfs和bfs和spfa我写爆了)


  • #include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 5005;const int INF = 0x3f3f3f3f;int N,M,rk[maxn],father[maxn]; struct Edge{    int u,v,w;}edge[maxn]; bool cmp(struct Edge x,struct Edge y){    return x.w < y.w;} void init(){    memset(rk,0,sizeof(rk));    memset(father,0,sizeof(father));    for (int i = 0;i <= N;i++)    {        father[i] = i;    }} int find(int x){    int r = x;    while (r != father[r])    {        r = father[r];    }    int i = x,j;    while (i != r)    {        j = father[i];        father[i] = r;        i = j;    }    return r;} void unite(int x,int y){    x = find(x);    y = find(y);    if (x != y)    {        if (rk[x] < rk[y])        {            father[x] = y;        }        else        {            father[y] = x;            if (rk[x] == rk[y])            {                rk[x]++;            }        }    }} int gcd(int a,int b){    return b?gcd(b,a%b):a;} int main(){    while (~scanf("%d%d",&N,&M))    {        int s,t,fz,fm;        double res = INF;        for (int i = 0;i < M;i++)        {            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);        }        scanf("%d%d",&s,&t);        sort(edge,edge + M,cmp);        for (int i = 0;i < M;i++)        {            init();            for (int j = i;j < M;j++)            {                unite(edge[j].u,edge[j].v);                if (find(s) == find(t))                {                    double tmp = 1.0*edge[j].w/edge[i].w;                    if (tmp < res)                    {                        res = tmp;                        fz = edge[j].w;                        fm = edge[i].w;                    }                }            }        }        int com = gcd(fz,fm);        res == INF?printf("IMPOSSIBLE\n"):fm/com == 1?printf("%d\n",fz/com):printf("%d/%d\n",fz/com,fm/com);    }    return 0;}