Huawei OD computer test - 5G network construction (Java & JS & Python & C)

Question 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, each base station needs to be connected using optical fiber to ensure that the base stations can be interconnected. It is assumed that optical fiber is used 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. For example, if base station A and base station B have optical fibers, and base station B and base station C also have optical fibers, then base station A and base station C are considered to be connected to each other.

Enter description

The first line of input represents 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 row, the format is:

X Y Z P

in:

X, Y represents the number of the base station

  • 0 < X ≤ N
  • 0 < Y ≤ N
  • X ≠ Y

Z represents the cost of setting up optical fiber between X and Y

  • 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, the minimum construction cost will be output.

If conditions are given,

Guess you like

Origin blog.csdn.net/qfc_128220/article/details/134844501