prim

 1 priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> >q;
 2 int prime() {
 3     ans = 0;
 4     q.push(make_pair(0, 1));
 5     while (q.size()) {
 6         int c = q.top().first, x = e[q.top().second].t;
 7         q.pop();
 8         if (!v[x]) {
 9             v[x] = 1;
10             ans += c;
11             add(e[i].f, e[i].t, e[i].w);
12             add(e[i].t, e[i].f, e[i].w);
13             for (int i = h[x]; i; i = e[i].n) {
14                 if (minc[e[i].t] > e[i].w && !v[e[i].t]) {
15                     minc[e[i].t] = e[i].w;
16                     q.push(make_pair(e[i].w, i));
17                 }
18             }
19         }
20     }
21 }
原文地址:https://www.cnblogs.com/Accpted/p/11203429.html