• P2502 [HAOI2006]旅行 最小生成树


    思路:枚举边集,最小生成树

    提交:1次

    题解:枚举最长边,添加较小边。

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #define R register int
    using namespace std;
    #define ull unsigned long long
    #define ll long long
    #define pause (for(R i=1;i<=10000000000;++i))
    #define In freopen("NOIPAK++.in","r",stdin)
    #define Out freopen("out.out","w",stdout)
    namespace Fread {
    static char B[1<<15],*S=B,*D=B;
    #ifndef JACK
    #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
    #endif
    inline int g() {
        R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix;
        if(ch==EOF) return EOF; do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix;
    } inline bool isempty(const char& ch) {return (ch<=36||ch>=127);}
    inline void gs(char* s) {
        register char ch; while(isempty(ch=getchar()));
        do *s++=ch; while(!isempty(ch=getchar()));
    }
    } using Fread::g; using Fread::gs;
    
    namespace Luitaryi {
    const int N=510,M=5010;
    int n,m,s,t,up,dn;
    double anss=1E+9;
    int fa[N];
    struct edge { int u,v,w;
        inline bool operator < (const edge& that) const{return w<that.w;}
    }e[M];
    inline int getf(int x) {return x==fa[x]?x:fa[x]=getf(fa[x]);}
    inline void main() {
        n=g(),m=g();
        for(R i=1;i<=m;++i) e[i].u=g(),e[i].v=g(),e[i].w=g();
        sort(e+1,e+m+1); s=g(),t=g();
        for(R i=1;i<=m;++i) { R ans=0;//枚举下界,最小的边 
            for(R j=1;j<=n;++j) fa[j]=j;
            for(R j=i;j<=m;++j) {//往上枚举,直到两点连通 
                R uf=getf(e[j].u),vf=getf(e[j].v);
                fa[uf]=vf;
                if(getf(s)==getf(t)) {ans=j; break;}
            } if(i==1&&ans==0) return (void)printf("IMPOSSIBLE
    ");
            if(ans==0) break; register double tmp=1.0*e[ans].w/e[i].w;
            if(tmp<anss) anss=tmp,up=e[ans].w,dn=e[i].w;
        } R tmp=__gcd(up,dn); if(tmp==dn) printf("%d
    ",up/dn);
        else printf("%d/%d
    ",up/tmp,dn/tmp);
    }
    }
    signed main() {
        Luitaryi::main();
        return 0;
    } 

    2019.07.20

  • 相关阅读:
    bzoj4734
    51nod1056
    51nod1048
    51nod1248
    51nod1044
    51nod1132
    51nod1146
    51nod1226
    Spring Boot: Jdbc javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify
    sqlserver命令创建数据库和表 demo
  • 原文地址:https://www.cnblogs.com/Jackpei/p/11217346.html
Copyright © 2020-2023  润新知