Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
Output
Sample Input
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
Sample Output
28
kruscal
1 #include <iostream> 2 #include <queue> 3 using namespace std; 4 int weight; 5 int fa[100100]; 6 7 void init(){ 8 for(int i = 1; i < 100100; i++){ 9 fa[i] = i; 10 } 11 weight = 0; 12 } 13 14 struct Edge{ 15 int from, to, w; 16 }; 17 18 bool operator < (const Edge &a, const Edge &b){ 19 return a.w > b.w; 20 } 21 22 int find(int x){ 23 if(fa[x] != x){ 24 fa[x] = find(fa[x]); 25 } 26 return fa[x]; 27 } 28 29 void mix(int x, int y){ 30 int fx = find(x); 31 int fy = find(y); 32 if(fx != fy) 33 fa[fy] = fx; 34 } 35 36 priority_queue<Edge> q; 37 38 void kruscal(){ 39 while(!q.empty()){ 40 Edge cmp = q.top(); 41 q.pop(); 42 if(find(cmp.from) != find(cmp.to)){ 43 weight += cmp.w; 44 mix(cmp.from, cmp.to); 45 } 46 } 47 } 48 int main(){ 49 int n; 50 while(cin >> n){ 51 init(); 52 for(int i = 1; i <= n; i++){ 53 for(int j = 1; j <= n; j++){ 54 int w; 55 cin >> w; 56 Edge e; 57 e.from = i; 58 e.to = j; 59 e.w = w; 60 q.push(e); 61 } 62 } 63 kruscal(); 64 cout << weight << endl; 65 } 66 return 0; 67 }
prim
1 #include <iostream> 2 #include <queue> 3 #include <stdlib.h> 4 #include <cstring> 5 #include <time.h> 6 using namespace std; 7 #define MAXN 1000 8 #define INF 0x3f3f3f3f 9 #define NOT_USED 0 10 #define USED 1 11 12 struct Edge{ 13 int from,to,weight; 14 }; 15 16 int n, cnt; // n:点个数 cnt:边条数 17 int g[MAXN][MAXN]; 18 int node[MAXN]; 19 int weight; 20 21 bool operator < (const Edge &a, const Edge &b) { 22 return a.weight > b.weight; 23 } 24 25 void init(){ 26 memset(g , 0 , sizeof(g) ); 27 memset(node, NOT_USED, sizeof(node) ); 28 weight = 0; 29 } 30 31 void prim(int start_index){ 32 node[start_index] = USED; 33 priority_queue <Edge> q; 34 for (int i = 1; i <= n; i++) { 35 if (0 != g[start_index][i]) { 36 Edge e; 37 e.from = start_index; 38 e.to = i; 39 e.weight = g[start_index][i]; 40 q.push(e); 41 } 42 } 43 while (!q.empty()) { 44 Edge tmp = q.top(); 45 q.pop(); 46 if (NOT_USED == node[tmp.from] || NOT_USED == node[tmp.to]){ 47 node[tmp.from] = USED; 48 node[tmp.to] = USED; 49 weight += tmp.weight; 50 for (int i = 1; i <= n; i++){ 51 if (NOT_USED == node[i] && 0 != g[i][tmp.to]) { 52 Edge add; 53 add.from = tmp.to; 54 add.to = i; 55 add.weight = g[i][tmp.to]; 56 q.push(add); 57 } 58 } 59 } 60 } 61 } 62 63 int main(){ 64 while(cin >> n){ 65 init(); 66 // for (int i = 1; i <= cnt; i++){ 67 // int a, b, val; 68 // cin >> a >> b >> val; 69 // g[a][b] = val; 70 // g[b][a] = val; 71 // } 72 for(int i = 1; i <= n; i++){ 73 for(int j = 1; j <= n; j++){ 74 int w; 75 cin >> w; 76 g[i][j] = w; 77 } 78 } 79 prim(1); 80 cout << weight << endl; 81 } 82 return 0; 83 }