Question number: 201412-4
Question Name: Optimal irrigation
Time limit: 1.0s
Memory Limit: 256.0MB problem description Ray Ray contracted a lot of wheat fields, in order to irrigate the wheat fields, Ray Ray dug a deep well in the first wheat field, all the wheat fields from this well to water diversion.
In order to irrigate, Ray Ray need to establish some canals to connect wells and wheat fields, and Ray Ray can also use some of the wheat fields as "transit stations", using canals to connect different fields of wheat, so as long as a wheat field can be irrigated, the wheat fields connected to it can also be irrigated.
Now Ray Ray know which fields of wheat can be built between canals and the cost of building each channel (note that not all wheat fields can be established between canals). How much is it necessary to irrigate all the wheat fields to build canals? Input format the first line of input contains two positive integers n, m, respectively, representing the number of slices in the wheat field and the amount of canals that Ray Ray can establish. Wheat fields use 1, 2, 3, ... Label in turn.
Next m line, each line contains three integers ai, bi, CI, which indicates that a channel can be established between the AI wheat field and the second Bi wheat field, the cost of which is CI. The output format outputs a line that contains an integer that represents the minimum cost required to irrigate all wheat fields. Sample Input 4 4
1 2 1
2 3 4
2 4 2
3 4 3 Sample Output 6 example illustrates the establishment of the following three canals: Wheat field 1 with wheat field 2, wheat field 2 with wheat field 4, wheat field 4 with wheat Field 3. Evaluation use case size and the first 20% evaluation use cases meet: n≤5.
The first 40% evaluation cases meet: N≤20.
The first 60% evaluation cases meet: n≤100.
All evaluation cases meet: 1≤n≤1000,1≤m≤100,000,1≤ci≤10,000. Problem Solving: Example code (Java):
1 PackageCcf_text2014_12;2 3 ImportJava.util.Scanner;4 5 Public classBESTGUANGAI2 {6 7 Static Final intMAX =Integer.max_value;8 9 Static intPrimalgorithm (int[] Graphmatrix,intVertexnumber,intInitialvertex) {Ten intMincost = 0; One Boolean[] visited =New Boolean[Vertexnumber + 1]; A for(inti = 0; I < Vertexnumber + 1; i++) { -Visited[i] =false; - } the - intPosition =Initialvertex; -Visited[position] =true; - int[] Lowcostinline =New int[Vertexnumber + 1]; + intMin =MAX; - + for(inti = 1; I < Vertexnumber + 1; i++) { ALowcostinline[i] =Graphmatrix[position][i]; at } - for(inti = 1; i < Vertexnumber; i++) { - for(intj = 1; J < Vertexnumber + 1; J + +) { - if(Visited[j] = =false&& Lowcostinline[j] <min) { -Min =Lowcostinline[j]; -Position =J; in } - } toMincost + =min; +Min =MAX; -Visited[position] =true; the for(intj = 1; J < Vertexnumber + 1; J + +) { * if(Visited[j] = =false&& Graphmatrix[position][j] <Lowcostinline[j]) { $LOWCOSTINLINE[J] =Graphmatrix[position][j];Panax Notoginseng } - } the } + returnMincost; A } the + Public Static voidMain (string[] args) { -Scanner sc =NewScanner (system.in); $ intN, M; $n =sc.nextint (); -m =sc.nextint (); - int[] Adjmatrix =New int[n + 1] [n + 1]; the intresult = 0; - Wuyi for(inti = 1; I < n + 1; i++) { the for(intj = 1; J < n + 1; J + +) { -ADJMATRIX[I][J] =MAX; Wu } - } About for(inti = 0; I < m; i++) { $ intBegin =sc.nextint (); - intEnd =sc.nextint (); - intWeight =sc.nextint (); -Adjmatrix[begin][end] =weight; AAdjmatrix[end][begin] =weight; + } theresult = Primalgorithm (Adjmatrix, N, 4); - System.out.println (result); $ } the}View Code
Operation Result:
PS: The above code uses the Primm prim algorithm to generate a minimum spanning tree to solve the optimal path, the code on the CCF certification platform score and choose to join the minimum spanning tree vertex set of the initial fixed-point Initialvertex and the system operating environment, see figure, Sometimes you can get a full score of 100, sometimes because the run time-out can only be 90 points or even 80 points, you can see where can also be optimized, copy the code to the CCF certification platform for testing, pay attention to its submission requirements, my program does not use the package statement to define the packet information. (cannot be evaluated if defined), so the package statement for the above code needs to be removed
Optimal irrigation of the CCF series (201412-4)