• HAOI2006(BZOJ1050) 旅行comf


    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不可能相同。

    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

    【数据范围】
    1< N < = 500
    1 < = x, y < = N,0 < v < 30000,x ≠ y
    0 < M < =5000
     
     
     
    题目分析:T_T...这么水的题我还想了好长时间。。果然还是弱爆了T_T... 我们可以枚举边,每次加边,用并查集维护连通性。。即可。。
     
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    const int N = 510;
    const int M = 5010;
    const int Maxint = 2147483647;
    using namespace std;
    #define For(i,n) for(int i=1;i<=n;i++)
    #define Rep(i,l,r) for(int i=l;i<=r;i++)
    struct EDGE{
        int s,t,next,w;
    }E[M];
    int head[N],es=1,fa[N];
    int Max,Min = Maxint,n,m,x,y,w,s,t;
    double MIN = Maxint;
    
    void makelist(int s,int t,int w){
        E[es].s = s;E[es].t = t;E[es].w = w;
        E[es].next = head[s];
        head[s]=es++;
    }
    
    void init(){
        scanf("%d%d",&n,&m);
        For(i,m){
            scanf("%d%d%d",&x,&y,&w);
            makelist(x,y,w);
        }
        scanf("%d%d",&s,&t);
    }
    
    bool cmp(EDGE A,EDGE B){
        return A.w<B.w;
    }
    
    int find(int root){
        if(root!=fa[root]) fa[root] = find(fa[root]);
        return fa[root];
    }
    
    int gcd(int a,int b){
        if(!b) return a;
        else   return gcd(b,a%b);
    }
    
    int main(){
        init();
        sort(E+1,E+m+1,cmp);
        For(i,m){
            For(j,n) fa[j] = j;
            Rep(j,i,m){
                int fx = find(E[j].s) , fy = find(E[j].t);
                if(fx!=fy) fa[fx] = fy;
                fx = find(s); fy = find(t);
                if(fx==fy)
                    if((double)E[j].w/(double)E[i].w<MIN){
                        MIN = (double)E[j].w / (double)E[i].w;
                        Min = E[i].w;
                        Max = E[j].w;
                        break;
                    }
            } 
        }
        if(Min==Maxint)  puts("IMPOSSIBLE");
        else{
            int gcds = gcd(Max,Min);
            if(Max%Min) printf("%d/%d
    ",Max/gcds,Min/gcds);
            else        printf("%d
    ",Max/Min);
        }
        return 0;
    }
    

      

  • 相关阅读:
    【转】前端防止 JS 调试技巧
    反爬虫 js怎样判断是真实点击事件还是模拟点击事件?
    js 前端 滑动验证
    【转】pyspider运行卡死在result_worker starting 的解决办法
    【转】pyspider all命令报错如下:ImportError: cannot import name 'DispatcherMiddleware' from 'werkzeug.wsgi'
    【转】pyspider中async关键字问题
    【转】Windows python3.7 下安装运行pyspider
    如何修改11g RAC集群名称
    Exadata健康检查工具EXAchk
    XD刷机中执行reclaimdisks.sh的作用
  • 原文地址:https://www.cnblogs.com/zjdx1998/p/3746757.html
Copyright © 2020-2023  润新知