• 最小生成树 prime+heap


    改一个错误真不容易,刚开始没有加上used数组,没有判断每个顶点是否已经加入到数组当中,结果同一个顶点被pop不止一次。

    struct edge{int to,cost;};
    typedef pair<int,int> P;//first集合X到i的最短距离,second点的编号
    int ranchNumber,roadNumber;//顶点个数,道路个数
    int mincost[10005];//每个顶点到集合x的最短距离
    bool used[10005];//每个顶点是否已经加入到集合中
    vector<edge> g[100005];
    //顶点编号从0开始;
    int heapPrime(){
        priority_queue<P,vector<P>,greater<P> > que;
        fill(mincost,mincost+10005,INF);
        fill(used,used+10005,false);
        mincost[0]=0;
        int ans=0;
        que.push(P(mincost[0],0));
        for(int j=0;j<ranchNumber;j++){
            P p=que.top();
            while(used[p.second]==true){
                que.pop();
                p=que.top();
            }
            que.pop();
            used[p.second]=true;
            ans+=p.first;
            int e=p.second;
            for(int i=0;i<g[e].size();i++){
                edge ee=g[e][i];
                if(mincost[ee.to]>ee.cost){
                    mincost[ee.to]=ee.cost;
                    que.push(P(mincost[ee.to],ee.to));
                }
            }
        }
        return ans;
    }
  • 相关阅读:
    Shell脚本sed命令
    Shell脚本常用unix命令
    Shell的case语句
    3.5.2 数值之间的转换
    3.5.1 数学函数与常量
    3.5 运算符
    3.4.2 常量
    3.4.1 变量初始化
    3.4 变量
    Python异常捕捉的一个小问题
  • 原文地址:https://www.cnblogs.com/TWS-YIFEI/p/6358076.html
Copyright © 2020-2023  润新知