Jungle RoadsTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 4725 Accepted Submission (s): 3471
Problem Description
The Head Elder of the tropical island of Lagrishan have a problem. A burst of foreign aid money is spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly and so the large road network are 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 with use now and the cost in aacms per month to maintain them. Of course there needs to be some-to-get between all the villages on maintained roads, even if the route was not as Shor T as before. The chief Elder would like to tell the Council of the elders what would is the smallest amount they could spend in aacms per m Onth to maintain roads this would connect all the villages. The villages is labeled A through I in the maps above. The map on the right shows the roads that could is maintained most cheaply, for 216 aacms per month. Your task is to write a program that would solve such problems.
The input consists of one to the 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 <, and the VI Llages is labeled with the first n letters of the alphabet, capitalized. Each data set was completed with n-1 lines this start with the 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 was the village label for the other end of the road followed by the monthly maintenance cost in AACM s for the road. Maintenance costs is positive integers less than 100. All data fields in the row is separated by single blanks. The road network would always be allow to travel between all the villages. The network would never have more than roads. No Village'll has more than roads going to other villages (before or after the alphabet). In the sample input below, the first data set goes with the map above.
The output is an integer per line for each data set:the minimum cost of aacms per month to maintain a road system that C Onnect all the villages. Caution:a Brute force solution that examines every possible set of roads would not finish within the one minute time limit .
Sample Input
9A 2 B I 25B 3 c ten H i 8C 2 D 55D 1 E 44E 2 F g 38F 0G 1 H 35H 1 I 353A 2 B ten C 40B 1 C 200
Sample Output
21630
There is nothing to say, that is, the topic is a bit difficult to understand. The first line is an n, the first letter of the second line is the village designator, the second number m is the number of villages connected to a, and the following m letters and numbers are the weights of the villages and roads connected to a.
Code:
#include <cstdio> #include <algorithm> #include <iostream> #define MAX 30#define INF 100000000using namespace std; struct edge{int x, Y, W;} Edge[max*4];int F[max]; bool CMP (const edge &E1, const edge &e2) {return E1.W<E2.W;} int find (int x) {int r = x; while (r! = F[r]) {r = F[r];} int temp; while (x! = F[x]) {temp = f[x];f[x] = r; x = temp;} return r;} void Init () {for (int i = 0; i < MAX; ++i) {f[i] = i;}} int Kruskal (int n) {sort (edge,edge+n,cmp); int sum = 0; init (); for (int i = 0; i < n; ++i) {int x = find (edge[i].x), Y = Find (EDGE[I].Y), if (x! = y) {sum + = EDGE[I].W; F[x] = F[y];}} return sum;} int main () {int n; ~scanf ("%d", &n) && n) {int index = 0; for (int i = 0; i < n-1; ++i) {char t;int m; c In>>t>>m;int x = t ' A '; for (int i = 0; i < m; ++i) {int w; cin>>t>>w; edge[index].x = x; edge[ Index].y = t ' A '; edge[index++].w = w;}} int sum = Kruskal (index); Cout<<sum<<endl;} return 0;}
HDU 1301 Jungle Roads A very water topic AC rate you know, but it's hard to understand. English slag O (╯-╰) Okruskal algorithm