标签:
Prime算法,每次挑选一个距离原点最近的节点,然后收缩(visited为true)
跟Dijkstra真的很像
代码:
1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 6 #define MAX_POINT 1024 7 8 int N; 9 int g[MAX_POINT][MAX_POINT]; 10 bool visited[MAX_POINT]; 11 12 int prime() { 13 int res = 0; 14 int left = N - 1; 15 visited[1] = true; 16 while (left) { 17 int k = 0; 18 for (int i = 1; i <= N; i++) 19 if (!visited[i] && g[1][i] < g[1][k]) 20 k = i; 21 visited[k] = true; 22 left--; 23 res += g[1][k]; 24 g[1][k] = g[k][1] = 0; 25 for (int i = 1; i <= N; i++) 26 if (!visited[i] && g[1][i] > g[1][k] + g[k][i]) 27 g[1][i] = g[i][1] = g[1][k] + g[k][i]; 28 } 29 30 return res; 31 } 32 33 int main() { 34 memset(g, 1, sizeof(g)); 35 memset(visited, false, sizeof(visited)); 36 scanf("%d", &N); 37 for (int i = 1; i <= N; i++) 38 for (int j = 1; j <= N; j++) 39 scanf("%d", &(g[i][j])); 40 printf("%d\n", prime()); 41 42 return 0; 43 }
标签:
原文地址:http://www.cnblogs.com/boring09/p/4396540.html