zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    [转载]C#流,字节数组,字符串
    [C#错误]未找到类型或命名空间名称" " (是否缺少 using 指令或程序集引用?)
    批处理删除带空格的长目录或文件夹
    注释换行 引号内的字符串没有正确结束
    oracle sqlplus运行sql文件
    Target runtime Apache Tomcat v6.0 is not defined.错误解决方法
    Tomcat:Caused by: java.lang.OutOfMemoryError: PermGen space
    虚拟机vmware启动太快无法进入bios的解决方法
    MySQL Error 1130 Host 'localhost' is not allowed to connect to this MySQL server
    如何远程判断服务器的操作系统?
  • 原文地址:https://www.cnblogs.com/zjdx1998/p/3746757.html
Copyright © 2011-2022 走看看