-
Notifications
You must be signed in to change notification settings - Fork 0
/
poj1258-slow.c
72 lines (59 loc) · 1.51 KB
/
poj1258-slow.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Problem: 1258, Agri-Net, minimum spanning tree
* User: LoiteringLeo
* Memory: 512K Time: 16MS
* Language: GCC Result: Accepted
*/
#include <stdio.h>
#include <string.h>
#define MAX_FARM 500
#define MAX_DIST 1000001
#define INF (MAX_DIST + 1)
int dist[MAX_FARM][MAX_FARM]; /* Adjacent matrix */
int mincost[MAX_FARM]; /* Minimum cost to MST */
int flag[MAX_FARM]; /* Is the farm belong to MST? */
int prim(int (*dist)[MAX_FARM], int n)
{
int sum = 0;
int i;
int j;
int min_dist;
int candidate;
for (i = 0; i < n; i++) {
mincost[i] = dist[0][i];
flag[i] = 0;
}
for (i = 1; i < n; i++) {
min_dist = MAX_DIST;
candidate = -1;
for (j = 1; j < n; j++) {
if (flag[j] == 0 && min_dist > mincost[j]) {
min_dist = mincost[j];
candidate = j;
}
}
sum += min_dist;
flag[candidate] = 1;
for (j = 1; j < n; j++) {
if (flag[j] == 0 && dist[candidate][j] < mincost[j]) {
mincost[j] = dist[candidate][j];
}
}
}
return sum;
}
int main()
{
int farm_cnt;
while (scanf("%d", &farm_cnt) != EOF) {
int row = 0;
int col = 0;
for (row = 0; row < farm_cnt; row++) {
for (col = 0; col < farm_cnt; col++) {
scanf("%d", &dist[row][col]);
}
}
printf("%d\n", prim(dist, farm_cnt));
}
return 0;
}