Huawei OD Computer Test Real Questions-5G Network Construction-2023 OD Unified Examination (Paper C)

Topic description:

It is now necessary to build a 5G network in a certain city. N locations have been selected to set up 5G base stations. The numbers are fixed from 1 to N. Next, optical fibers need to be used to connect the base stations to ensure that the base stations can be interconnected. Optical fibers are set up between different base stations. The costs vary, and some nodes already have optical fiber connections between them. Please design an algorithm to calculate the minimum cost to connect these base stations.

Note: The communication between base stations is transitive. If base station A and base station B have optical fibers set up, and base station B and base station C also have optical fibers, then base station A and base station C are considered to be able to communicate with each other.

Input description: The first line of input indicates the number N of base stations, where 0<N<=20

The second line of input represents the number M of base station pairs that have direct fiber connection conditions, where 0<M<N*(N-1)/2

Enter M rows of data continuously starting from the third line, in the format of X Y Z P, where X Y represents the number of the base station, 0< Cost, where 0<Z<100, P indicates whether there is a fiber connection, 0 indicates not connected, 1 indicates connected

Output description: If a successfully interconnected 5G network can be built under given conditions, then the minimum construction cost will be output;

If a successfully interconnected 5G network cannot be built under the given conditions, -1 will be output.

Example 

Example 1

Input: 3

           3

           1 2 3 0

           1 3 1 0

           2 3 5 0

Output: 4

Note: Only optical fiber needs to be laid between base stations 1, 2 and 2, 3, and the cost is 3+1&

Guess you like

Origin blog.csdn.net/2301_76848549/article/details/134906906