forked from yogykwan/acm-challenge-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoj1258.cpp
56 lines (51 loc) · 1.09 KB
/
poj1258.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* POJ 1258: Agri-Net
* 题意:给出邻接矩阵,选出部分边,使得整个图连通。求边的最小总花费。
* 类型:最小生成树
* 算法:本题为稠密图,Prim算法求最小生成树更优,复杂度O(n^2)。(/Kruskal算法亦可)
*/
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int mat[110][110];
bool vis[110];
int d[110];
int Prim(int n) {
int ans = 0;
int p = 0;
vis[0] = 1;
for(int j = 1; j < n; ++j) {
d[j] = mat[p][j];
}
for(int i = 1; i < n; ++i) {
p = -1;
for(int j = 1; j < n; ++j) {
if(vis[j]) continue;
if(p == -1 || d[j] < d[p]) {
p = j;
}
}
ans += d[p];
vis[p] = 1;
for(int j = 1; j < n; ++j) {
if(vis[j]) continue;
d[j] = min(d[j], mat[p][j]);
}
}
return ans;
}
int main() {
int n, i, j;
while(scanf("%d", &n) != EOF) {
memset(vis, 0, sizeof(vis));
for(i = 0; i < n; ++i) {
for(j = 0; j < n; ++j) {
scanf("%d", &mat[i][j]);
}
}
int ans = Prim(n);
printf("%d\n", ans);
}
return 0;
}