-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprim.cpp
More file actions
40 lines (32 loc) · 1009 Bytes
/
Copy pathprim.cpp
File metadata and controls
40 lines (32 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <bits/stdc++.h>
#define N 10
#define INF 0x3f3f3f3f
using namespace std;
int n; // n表示点数
int g[N][N]; // 邻接矩阵,存储所有边
int dist[N]; // 存储其他点到当前最小生成树的距离
bool st[N]; // 存储每个点是否已经在生成树中
// 如果图不连通,则返回INF(值是0x3f3f3f3f), 否则返回最小生成树的树边权重之和
//复杂度O(n^2 + m)
int prim(){
memset(dist, 0x3f, sizeof dist);
int res = 0;
for(int i = 0; i < n; i++){
int t = -1; //第一次的时候,一般给t赋值-1,标识刚开始
for(int j = 1; j <= n; j++){
if(!st[j] && (t == -1 || dist[t] > dist[j])){
t = j;
}
}
if(i && dist[t] == INF) return INF;
if(i) res += dist[t];
st[t] = true;
for(int j = 1; j <= n; j++){
dist[j] = min(dist[j], g[t][j]);
}
}
return res;
}
int main(){
return 0;
}