BZOJ 1050 舒适的路线 [codevs1001]

1050: [HAOI2006]旅行comf

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 2851  Solved: 1558

Description

  给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000)。给你两个顶点S和T
,求一条路径,使得路径上最大边和最小边的比值最小。如果S和T之间没有路径,输出”IMPOSSIBLE”,否则输出
这个比值,如果需要,表示成一个既约分数。 备注: 两个顶点之间可能有多条路径。

Input

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

Output

  如果景点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
/**************************************************************
    Problem: 1050
    User: loi_francis
    Language: C++
    Result: Accepted
    Time:3200 ms
    Memory:4936 kb
****************************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf = 2147483640;
int fa[233333], tot = 0;
struct edge
{
    int from, to, cost;
}es[233333];
bool cmp(edge a, edge b)
{
    return a.cost > b.cost;
}
int find(int x)
{
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int main()
{
    int n, m;
    cin>>n>>m;
    for (int i = 1; i <= m; i++)
    {
        int f, t, d;
        scanf("%d%d%d", &f, &t, &d);
        es[i].from = f;
        es[i].to = t;
        es[i].cost = d;
    }
    sort (es + 1, es + m + 1, cmp);
    int s, t;
    double ans = inf, qq, w;
    int lsa, lsb;
    scanf("%d%d", &s, &t);
    for (int i = 1; i <= m; i++)
    {
        for (int j = 1; j <= n; j++)
            fa[j] = j;
        fa[es[i].from] = es[i].to;
        for (int j = i; j <= m; j++)
        {
            int x = es[j].from, y = es[j].to;
            if (find(x) != find(y))
                fa[find(x)] = find(y);
            if (find(s) == find(t))
            {
                qq = (double)es[i].cost;
                w = (double)es[j].cost;
                if (ans > qq / w)
                {
                    ans = qq / w;
                    lsa = es[i].cost;
                    lsb = es[j].cost;
                }
                break;
            }
        }
    }
    if (ans == inf)
    {
        puts("IMPOSSIBLE");
        return 0;
    }
    for (int i = 2; i <= lsb; i++)
    {
        if (lsa % i == 0 && lsb % i == 0)
        {
            lsb /= i;
            lsa /= i;
            i --;
        }
    }
    cout << lsa;
    if (lsb != 1)
        cout << "/" << lsb;
    return 0;
}

 

上一篇
下一篇