最小生成树 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;
}
时间: 2024-12-26 13:07:18

最小生成树 prime+heap的相关文章

最小生成树 prime poj1258

题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set&

poj1258 Agri-Net +hdu 1233 还是畅通工程 (最小生成树Prime算法)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43215   Accepted: 17683 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

hdu 1875 最小生成树 prime版

最小生成树prime版 大致的步骤 首先选取一个到集合最近的点 然后标记起在集合内部 然后更新最短距离 畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24846    Accepted Submission(s): 8035 Problem Description 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不

POJ - 1258(最小生成树.prime)

题目链接: http://poj.org/problem?id=1258 题目: Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 68462   Accepted: 28369 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet conn

最小生成树 prime kruskal

带权图分为有向和无向 无向图的最短路径又叫做最小生成树,有prime算法和kruskal算法: 有向图的最短路径算法,有dijkstra算法和floyd算法. 生成树的概念:联通图G的一个子图如果是一棵包含G的所有顶点的树,则该子图称为G的生成树 生成树是联通图的极小连通子图.所谓极小是指:若在树中任意增加一条边,则 将出现一个回路:若去掉一条边,将会使之编程非连通图.生成树各边的权 值总和称为生成素的权.权最小的生成树称为最小生成树,常用的算法有prime算法和kruskal算法. 最小生成树

最小生成树 prime poj1287

poj1287 裸最小生成树 AC代码 1 #include "map" 2 #include "queue" 3 #include "math.h" 4 #include "stdio.h" 5 #include "string.h" 6 #include "iostream" 7 #include "algorithm" 8 #define abs(x) x &g

hdu1875(最小生成树prime)

思路:一开始想用贪心来着,发现贪心有缺陷,然后就用了最小生成树来写,这里用了prime算法,首先,先建个图,两点之间的边的权值就是两个点的距离,然后直接prime模板 代码 #include<iostream> #include<algorithm> #include<cstring> #include<math.h> #include<cstdio> const int inf=0x7fffffff; using namespace std;

POJ - 2485(最小生成树.prime)

题目链接: http://poj.org/problem?id=2485 题目: Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36525   Accepted: 16329 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the

poj 2031 Building a Space Station【最小生成树prime】【模板题】

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5699   Accepted: 2855 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar