zoukankan      html  css  js  c++  java
  • POJ-1251-Jungle Roads(Prim算法-最小生成树-模板题目)

    POJ-1251-Jungle Roads

    本代码模板抄录自kuangbin模板集

    
    /*
    Prim算法求最小生成树的权值
    耗费邻接矩阵 cost[][],标号从 0 开始,0∼n-1
    返回最小生成树的权值,返回 -1 表示原图不连通
    */
    const int inf = 0x3f3f3f3f;
    const int maxn = 110;
    bool vis[maxn];
    //编号为i的结点通过权值为lowc[i]的边连入了最小生成树
    int lowc[maxn];
    
    //(权值的邻接矩阵,结点总个数)
    int Prim(int cost[][maxn],int n){
        int 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++){
            int 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;
    }
    

    下面是模板题目的解法

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    /*
    Prim 求最小生成树的权值
    耗费矩阵 cost[][],标号从 0 开始,0∼n-1
    返回最小生成树的权值,返回 -1 表示原图不连通
    */
    
    const int inf = 0x3f3f3f3f;
    const int maxn = 110;
    bool vis[maxn];
    
    //编号为i的结点通过权值为lowc[i]的边连入了最小生成树
    int lowc[maxn];
    
    //(权值的邻接矩阵,结点总个数)
    int Prim(int cost[][maxn],int n){
        int 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++){
            int 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;
    }
    
    int cost[maxn][maxn];//权值的邻接矩阵
    int n;//结点总个数
    
    int readin(){
        scanf("%d",&n);
        if(n==0)return 0;
        getchar();
        for(int i=0;i<n;i++){
            for(int k=0;k<n;k++){
                cost[i][k] = inf;
            }
        }//完成初始化
        for(int i=0;i<n-1;i++){
            char ch;int len;
            cin>>ch>>len;
            //cout<<ch<<" "<<len<<endl;
            while(len--){
                char p;int v;
                cin>>p>>v;
                cost[ch-'A'][p-'A']=cost[p-'A'][ch-'A']=v;
            }
            getchar();
        }
        return 1;
    }
    
    int main(){
        //freopen("in.txt","r",stdin);
        while(readin()){
            //cout<<"OK"<<endl;
            cout<<Prim(cost,n)<<endl;
            //
            /*for(int i=0;i<n;i++){
                cout<<i<<"=>"<<lowc[i]<<endl;
            }*///可以通过输出lowc确定最小生成树的形态
            break;
        }
        return 0;
    }
    

    给一个测试样例

    9
    A 2 B 12 I 25
    B 3 C 10 H 40 I 8
    C 2 D 18 G 55
    D 1 E 44
    E 2 F 60 G 38
    F 0
    G 1 H 35
    H 1 I 35
    0
    

    ans = 216

    关于样例的图解释:

    OK

  • 相关阅读:
    Laravel Passport token过期后判断refresh_token是否过期
    js 数组随机排序
    jquery的animate关于background-position属性
    css hack 汇整
    顶部导航--向上滚动的时候出现,向下滚动的时候隐藏
    手机端全局样式表整理(mobile)
    AR专用汉明码
    css常用命名规则
    晚11点
    当 IDENTITY_INSERT 设置为 OFF 时,不能为表‘XXX’中的标识列插入显式值。
  • 原文地址:https://www.cnblogs.com/savennist/p/13806243.html
Copyright © 2011-2022 走看看