zoukankan      html  css  js  c++  java
  • HDU

    d.c个小岛,通过建立桥,使其全部可达。求所有的桥的最小长度和。

    s.最小生成树,数据改成double就行了

    c.Prim算法:cost[a][b]和cost[b][a]都得赋值。

    /*
    Prim算法
    Prim求MST
    耗费矩阵cost[][],标号从0开始,0~n-1
    返回最小生成树的权值,返回-1表示原图不连通
    */
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    using namespace std;
    
    const int INF=0x3f3f3f3f;
    const int MAXN=110;
    bool vis[MAXN];
    double lowc[MAXN];
    //点是 0 n-1
    double Prim(double cost[][MAXN],int n){
        double ans=0;
        memset(vis,false,sizeof(vis));
        vis[0]=true;
        for(int i=1;i<n;i++)lowc[i]=cost[0][i];
        for(int i=1;i<n;i++){
            double minc=INF;
            int p=-1;
            for(int j=0;j<n;j++)
                if(!vis[j]&&minc>lowc[j]){
                    minc=lowc[j];
                    p=j;
                }
            if(minc==INF)return -1;//原图不连通
            ans+=minc;
            vis[p]=true;
            for(int j=0;j<n;j++)
                if(!vis[j]&&lowc[j]>cost[p][j])
                    lowc[j]=cost[p][j];
        }
        return ans;
    }
    
    double cost[MAXN][MAXN];
    
    int p[128][2];
    int tol;
    
    void make(){
        for(int i=0;i<tol;++i){
            for(int j=0;j<tol;++j){
                cost[i][j]=sqrt((p[i][0]-p[j][0])*(p[i][0]-p[j][0])+
                                (p[i][1]-p[j][1])*(p[i][1]-p[j][1]));
                if(cost[i][j]<10||cost[i][j]>1000)
                    cost[i][j]=INF;
            }
        }
    }
    
    int main(){
        int T;
        int C;
        //int x,y;
    
        scanf("%d",&T);
        while(T--){
            tol=0;
            scanf("%d",&C);
            for(int i=0;i<C;++i){
                scanf("%d%d",&p[tol][0],&p[tol][1]);
                ++tol;
            }
            make();
    
            double ans=Prim(cost,C);
    
            if(ans==-1)printf("oh!
    ");
            else printf("%.1f
    ",ans*100);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    文件同步
    Renesas PPP Mode
    PYTHON 常用API ***
    Python DB
    Python VIL Realse
    C BIN加密
    Python VIL Service Bin
    Python 定期检查Build_setting的编译情况
    Python 字串处理
    IP分片(IP Fragment)
  • 原文地址:https://www.cnblogs.com/gongpixin/p/5019895.html
Copyright © 2011-2022 走看看