zoukankan      html  css  js  c++  java
  • 【提高组】最小生成树

    P1991 无线通讯网

    #include<bits/stdc++.h>
    #define For(i,l,r) for(int i=l;i<=r;i++) 
    using namespace std;
    const int N=505;
    struct node{
        int t1,t2;double v;
    }e[N*N];
    struct NODE{
        double x,y;
    }st[N];
    int s,p,cnt,f[N],fa[N];
    double ans;
    inline double dis(int i,int j){
        return sqrt((st[i].x-st[j].x)*(st[i].x-st[j].x)+(st[i].y-st[j].y)*(st[i].y-st[j].y));
    }
    inline int getfa(int x){
        if(f[x]!=x) return getfa(f[x]);
        return x;
    }
    inline bool cmp(node x,node y){return x.v<y.v;}
    int main(){
        scanf("%d%d",&s,&p);s=p-s;
        For(i,1,p) f[i]=i;
        For(i,1,p) scanf("%lf%lf",&st[i].x,&st[i].y);
        For(i,1,p){
            For(j,i+1,p){
                e[++cnt].t1=i,e[cnt].t2=j,e[cnt].v=dis(i,j);
            }
        }
        sort(e+1,e+cnt+1,cmp);int a,b;
        for(int i=1;i<=cnt,s>0;i++){
            a=e[i].t1,b=e[i].t2;
            fa[a]=getfa(a),fa[b]=getfa(b);
            if(fa[a]!=fa[b]){
                f[fa[a]]=fa[b];s--;ans=max(ans,e[i].v);
            }
        }
        printf("%.2lf",ans);
        return 0;
    }
    View Code

    P1265 公路修建

    #include<bits/stdc++.h>
    #define For(i,l,r) for(int i=l;i<=r;i++)
    #define inf 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    const int N=5005;
    int n,m;
    ll d[N];
    bool vis[N];
    double ans;
    struct node{
        ll x,y;
        ll operator*(const node &b)const{return (x-b.x)*(x-b.x)+(y-b.y)*(y-b.y);}
    }c[N];
    inline ll read(){
        ll f=1,sum=0;
        char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
        while(isdigit(ch)){sum=(sum<<1)+(sum<<3)+(ch^48);ch=getchar();}
        return f*sum;
    }
    inline void prim(){
        memset(d,inf,sizeof(d));
        memset(vis,0,sizeof(vis));
        d[1]=0;
        For(i,1,n-1){
            int x=0;
            For(j,1,n){if(!vis[j]&&(x==0||d[j]<d[x])) x=j;}
            vis[x]=1;
            For(j,1,n) {if(!vis[j]) d[j]=min(d[j],c[x]*c[j]);}
        }
    }
    int main(){
        n=read();
        For(i,1,n){c[i].x=read(),c[i].y=read();}
        prim();
        For(i,1,n) ans+=sqrt((double)d[i]);
        printf("%.2f
    ",ans);
        return 0;
    }
    View Code
  • 相关阅读:
    在 XD 和 Axure 中使用 iconfont
    chartjs 曲线图 纪要
    js ajax 等的的一些纪要
    程序员的方向
    sqlserver 常用的练习50例子
    (function(){})()原理
    layer弹出层详解
    sqlserver 表值函数 保存使用
    关于批量下载线程池与信号机制的使用
    tp5.1 phpstudy composer 配置等
  • 原文地址:https://www.cnblogs.com/jian-song/p/11661981.html
Copyright © 2011-2022 走看看