首页 > 代码库 > Jungle Roads
Jungle Roads
Jungle Roads
The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.
Input
Output
Sample Input
9 A 2 B 12 I 25 B 3 C 10 H 40 I 8 C 2 D 18 G 55 D 1 E 44 E 2 F 60 G 38 F 0 G 1 H 35 H 1 I 35 3 A 2 B 10 C 40 B 1 C 20 0
Sample Output
216 30
prim
1 #include <iostream> 2 #include <queue> 3 #include <stdlib.h> 4 #include <cstring> 5 #include <time.h> 6 #include <stdio.h> 7 using namespace std; 8 #define MAXN 1000 9 #define INF 0x3f3f3f3f 10 #define NOT_USED 0 11 #define USED 1 12 13 struct Edge{ 14 int from,to,weight; 15 }; 16 17 int n, cnt; // n:点个数 cnt:边条数 18 int g[MAXN][MAXN]; 19 int node[MAXN]; 20 int weight; 21 22 bool operator < (const Edge &a, const Edge &b) { 23 return a.weight > b.weight; 24 } 25 26 void init(){ 27 memset(g , 0 , sizeof(g) ); 28 memset(node, NOT_USED, sizeof(node) ); 29 weight = 0; 30 } 31 32 void prim(int start_index){ 33 node[start_index] = USED; 34 priority_queue <Edge> q; 35 for (int i = 1; i <= n; i++) { 36 if (0 != g[start_index][i]) { 37 Edge e; 38 e.from = start_index; 39 e.to = i; 40 e.weight = g[start_index][i]; 41 q.push(e); 42 } 43 } 44 while (!q.empty()) { 45 Edge tmp = q.top(); 46 q.pop(); 47 if (NOT_USED == node[tmp.from] || NOT_USED == node[tmp.to]){ 48 node[tmp.from] = USED; 49 node[tmp.to] = USED; 50 weight += tmp.weight; 51 for (int i = 1; i <= n; i++){ 52 if (NOT_USED == node[i] && 0 != g[i][tmp.to]) { 53 Edge add; 54 add.from = tmp.to; 55 add.to = i; 56 add.weight = g[i][tmp.to]; 57 q.push(add); 58 } 59 } 60 } 61 } 62 } 63 64 // int main(){ 65 // while(cin >> n){ 66 // init(); 67 // // for (int i = 1; i <= cnt; i++){ 68 // // int a, b, val; 69 // // cin >> a >> b >> val; 70 // // g[a][b] = val; 71 // // g[b][a] = val; 72 // // } 73 // for(int i = 1; i <= n; i++){ 74 // for(int j = 1; j <= n; j++){ 75 // int w; 76 // cin >> w; 77 // g[i][j] = w; 78 // } 79 // } 80 // prim(1); 81 // cout << weight << endl; 82 // } 83 // return 0; 84 // } 85 86 int main () { 87 while(cin >> n && n){ 88 init(); 89 getchar(); 90 for(int i = 1; i < n;i++){ 91 char c; 92 int t; 93 cin >> c >> t; 94 // cout << c << t; 95 getchar(); 96 while(t--){ 97 char s; 98 int w; 99 cin >> s >> w; 100 getchar(); 101 // cout << s << w; 102 // Edge e; 103 // e.from = c - ‘A‘; 104 // e.to = s - ‘A‘; 105 // e.weight = w; 106 // q.push(e); 107 g[c - ‘A‘ + 1][s - ‘A‘ + 1] = g[s - ‘A‘ + 1][c - ‘A‘ + 1] = w; 108 } 109 } 110 prim(1); 111 cout << weight << endl; 112 } 113 return 0; 114 }
Kruscal
1 #include <iostream> 2 #include <string.h> 3 #include <queue> 4 #include <stdio.h> 5 using namespace std; 6 #define MAXN 1000 7 8 struct Edge{ 9 int from,to,weight; 10 }; 11 12 bool operator < (const Edge &a, const Edge &b){ 13 return a.weight > b.weight; 14 } 15 16 int n, cnt; 17 int parent[MAXN]; 18 int weight = 0; 19 priority_queue <Edge> q; 20 21 void init(){ 22 for (int i = 0; i < MAXN; i++) { 23 parent[i] = i; 24 } 25 weight= 0; 26 } 27 28 int find(int x){ 29 int r = x; 30 while (parent[r] != r){ 31 r = parent[r]; 32 } 33 int i = x, j; 34 while (parent[i] != r){ 35 j = parent[i]; 36 parent[i] = r; 37 i = j; 38 } 39 return r; 40 } 41 42 void mix(int x, int y){ 43 int fx = find(x); 44 int fy = find(y); 45 if (fx != fy){ 46 parent[fy] = fx; 47 } 48 } 49 50 void kruscal () { 51 while (!q.empty()){ 52 Edge e = q.top(); 53 q.pop(); 54 if (find(e.from) != find(e.to)){ 55 weight += e.weight; 56 mix(e.from, e.to); 57 } 58 } 59 } 60 61 int main () { 62 while(cin >> n && n){ 63 init(); 64 getchar(); 65 for(int i = 1; i < n;i++){ 66 char c; 67 int t; 68 cin >> c >> t; 69 // cout << c << t; 70 getchar(); 71 while(t--){ 72 char s; 73 int w; 74 cin >> s >> w; 75 getchar(); 76 // cout << s << w; 77 Edge e; 78 e.from = c - ‘A‘; 79 e.to = s - ‘A‘; 80 e.weight = w; 81 q.push(e); 82 } 83 } 84 kruscal(); 85 cout << weight << endl; 86 } 87 return 0; 88 }
Jungle Roads