CCF-CSP 201812-4 Data Center (Kruskal minimum spanning tree)

Kruskal minimum spanning tree

  Code comments analyzed roughly idea. Solution of the root node must communicate with all other nodes, to meet the nature of the spanning tree. From the comments of the analysis, the optimal solution is a minimum spanning tree.

  Kruskal's algorithm is actually a greedy algorithm, each time a small edge weights are selected to construct a spanning tree, so that the final spanning tree edge weights smallest sum. If the edge does not cause the newly added communication point map is increased, the edge should be dropped, which is necessary to determine whether the edge vertices at both ends in the same connected component - by disjoint-set implementation. According to the nature of the spanning tree, when exactly N-1 is added to the spanning tree edges, the structure is completed. (N is the number of vertices)

 

  Why transfer time is the maximum time on the path? Reference may be pipelined, i.e., a transceiver of each node is asynchronous, for a node, not all the data after the data to be received, transmitted and then received. This is similar to dual-port buffer, the write port (data sent from the receiving end) and read ports (transmitted to the transmitting end) writing and reading data at different rates.

 

PS: I encountered a rare problem solving: If the identifier is defined in the program Rank , the CSP report compilation errors. Rank_1 later changed by other identifiers.

This may be due sentenced to question the system takes up rank this identifier, resulting in redefinition errors at compile time.

 

AC Code:

  1  / * 
  2  * Rank 1. If the variable name is defined, the compiler error packet CSP, later changed by rank_1. Reasons unknown!
  3  * 
   4  * 2. The title of "transmission time of the tree structure" substantially maximum transmission time on the communication path. The goal is to seek to minimize the total transmission time tree: 
   5  * on the communication path when the sum of the minimum transmission time, wherein the maximum transmission time is minimal (the transmission time is not negative). 
  6  * thus ask "optimum structure" artwork minimum spanning tree. 
  7  * 3. The use kruskal algorithm, based on the data given by the size of the subject, deal with disjoint-set according to the rank of a simple or combined path compression optimization. 
  8  * The only condition is that the increase rank: two equal sub-tree corresponding to the set rank. Because the merger set
   9  * always rank rank to merge small tree large tree, the total rank will not be any change, unless the two trees of the same rank, only to increase overall rank 1. 
10  * Found the present embodiment, simply by using the simple combined rank optimization, execution time is 1/40 of the original!
11  * 4. path compression algorithm may also be considered, that is, each Father () search, parent node will be searched through the point of their direct root.
12  * recursive: Line 45.
13 * Found the present embodiment, using a simple path compression optimization, execution time is 1/40 of the original!  
14   * / 
15   
16 #include <the iostream>
 . 17 #include <algorithm>
 18 is  
. 19 #include <the fstream>
 20 is  
21 is  struct Edge {
 22 is      int U, V;
 23 is      int T;
 24      
25      inline BOOL  operator <( const Edge & E) const {
 26 is          return T < et;
 27      }
 28      inline BOOL  operator == ( const Edge &e) const {
 29         return u==e.u || v==e.v;
 30     }
 31 };
 32 
 33 using namespace std;
 34 
 35 static int *parent;
 36 static int *rank_1;
 37 
 38 static int father(int p) {
 39 #if 0  // not optimized 
 40     while (parent[p] != p) {
 41         p = parent[p];
 42     }
 43     return p;
 44 #else
 45     return (parent[p] == p) ? p : (parent[p] = father(parent[p]));
 46 #endif 
 47 }
 48 
 49 int main(void) {
 50     ios::sync_with_stdio(false);
 51     
 52 /*#if 0 
 53     ifstream fin("input.txt");
 54     cin.rdbuf(fin.rdbuf());
 55 #endif*/ 
 56     
 57     int n, m, root;
 58     cin >> n >> m >> root;
 59     
 60     Edge *e = new Edge[m];
 61     parent = new int[n+1];
 62     rank_1 = new int[n+1];
 63     
 64     for(int i=0; i<m; ++i) {
 65         cin >> e[i].v >> e[i].u >> e[i].t;
 66     }
 67     for(int i=1; i<=n; ++i) {
 68         parent[i] = i;
 69         rank_1[i] = 1;
 70     }
 71     
 72     sort(e, e+m);
 73     
 74     int k = 0;
 75     int ans = 0;
 76     
 77     father(e[0].u);
 78     
 79     for(int i=0; i<m; ++i) {
 80         int pv = father(e[i].v);
 81         int pu = father(e[i].u);
 82         if (pv != pu) {
 83             if (rank_1[pv] < rank_1[pu]) {
 84                 parent[pv] = pu; // union pv -> pu 
 85             }
 86             else {
 87                 parent[pu] = pv; // union pu -> pv
 88                 if (rank_1[pu] == rank_1[pv]) {
 89                     ++rank_1[pv];
 90                 }
 91             }
 92             
 93             ++k;
 94             
 95             if (e[i].t > ans) {
 96                 ans = e[i].t;
 97             }
 98         }
 99         if (k==n-1) break;
100     }
101     
102     cout << ans << endl;
103     
104     return 0;
105 } 

 

Guess you like

Origin www.cnblogs.com/sci-dev/p/11489022.html