Title Description: Portal
Idea realization:
See the shortest path: The algorithms we can associate are Dixtra's algorithm, Freud's algorithm and bfs. In fact, this problem Freud's algorithm is more suitable, but here as a bfs problem to solve. Since the points that the hospital may establish are 1 ~ n, the bfs is taken from 1 ~ n as the starting point, and the shortest path is respectively found, and then the smallest path is selected from these shortest paths, and the starting point of the path is the hospital Building point.
Code:
1 #include <bits / stdc ++. H> 2 using namespace std; 3 4 // Although this is a tree structure, in fact, you can also use the adjacency list to perform bfs, and use each node as the starting point to perform bfs 5 / / Tree building method is also possible, but it is troublesome. You have to build a tree before bfs 6 7 // The new idea solves the problem of the depth of the node: design a structure, save the subscript and depth of the current node, and update the current node: Depth of previous node + 18 int minn = 1 << 30 ; 9 int n = 0 ; 10 int mapp [ 105 ] [ 105 ] = { 0 }; 11 bool v [ 105 ]; //Used to determine whether a point has been visited 12 int w [ 105 ] = { 0 }; // Number of people stored 13 int bfs ( int index) { // index subscript, 14 memset (v, 0 , sizeof (v )); 15 int sum = 0 ; 16 int size = 1 ; // size is the number of nodes in the current layer, each time you visit a node size-know that the size is reduced to 0 means that all the nodes of the current layer have been traversed, At this time, depth step ++ 17 // Because only after traversing the rightmost point of the current layer, the depth step will increase by 1, and at the same time, it will enter the next layer 18 int temp = 0 ; //temp records the number of nodes in the next layer of the current layer. When size is 0, it means to enter the next layer to traverse. 19 // So the temp value is assigned to size, and the temp situation re-accumulates the nodes of the next layer Points 20 queue < int > q; 21 q.push (index); // Starting into the team 22 v [index] = 1 ; 23 int step = 1 ; // Record the bfs search layer 24 while (! Q.empty ( )) { 25 int cur = q.front (); // cur is the current node subscript 26 q.pop (); 27 for ( int i = 1 ; i <= n; i ++){ 28 if(mapp[cur][i]==0||v[i]) continue; 29 sum+=w[i]*step; 30 q.push(i); //入队 31 v[i]=1; 32 33 if(size>0){ 34 temp++; 35 } 36 } 37 size--; 38 if(size==0){ 39 size=temp; 40 temp = 0 ; // reset 0 41 step ++ ; 42 } 43 } 44 return sum; 45 } 46 int main () { 47 cin >> n; 48 for ( int i = 1 ; i <= n; i ++ ) { 49 int v, u; 50 cin >> w [i]; 51 cin >> u >> v; 52 53 // If you enter 0 again, 1 54 if (u) mapp [i] [u ] =1 ; // Indicated connection 55 if (u) mapp [u] [i] = 1 ; // Undirected graph is symmetrical 56 if (v) mapp [i] [v] = 1 ; 57 if (v) mapp [v] [i] = 1 ; 58 } 59 60 for ( int i = 1 ; i <= n; i ++) { // Set the starting point of each bfs to the hospital position 61 minn = min (minn, bfs (i) ); 62 } 63 cout << minn; 64 return 0 ; 65 }