码迷,mamicode.com
首页 > 其他好文 > 详细

Jungle Roads

时间:2017-05-01 12:48:13      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:his   head   exp   completed   lan   aac   continue   img   from   

                                                                 

                                                                      技术分享

 

                                                               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

The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.

Output

The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.

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

标签:his   head   exp   completed   lan   aac   continue   img   from   

原文地址:http://www.cnblogs.com/jxust-jiege666/p/6791568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!