KingdomReorganization
SRM 531 · 2011-11-22 · by Mimino
Problem Statement
This problem statement contains subscripts and may not be shown properly outside of the applet.
The greatest king of all times, Michael IV, is going to make big changes in his kingdom. The kingdom is composed of N cities (for simplicity numbered from 0 to N-1). Some pairs of cities are connected by bidirectional roads. We say that there is a path between different cities A and B if there exists a sequence of unique cities {C1, C2, ..., CM}, such that C1 = A and CM = B and for each index i < M, there is a road between cities Ci and Ci+1.
The current state of the kingdom is miserable. Some pairs of cities are not connected by any path. On the other hand, other pairs of cities are connected by multiple different paths, and that leads to complicated traffic routing. Michael wants to build some new roads and destroy some of the already existing roads in the kingdom so that after the reconstruction there will exist exactly one path between every pair of distinct cities. As building new roads and destroying old ones costs a lot of money, Michael wants to minimize the total cost spent on the reconstruction.
You are given a
Notes
- N, the number of cities in the kingdom can be determined as the number of elements of kingdom.
- Two paths {A1, A2, ..., AR} and {B1, B2, ..., BS} are considered different if either R is not equal to S or there exists i <= R, such that Ai is not equal to Bi.
- There can never exist a road leading from some city to itself.
- The values in build that correspond to existing roads can simply be ignored. Similarly, ignore the values in destroy that correspond to nonexistent roads.
Constraints
- N will be between 1 and 50, inclusive.
- kingdom, build and destroy will each contain N elements.
- Each of the elements of kingdom, build and destroy will contain N characters.
- Each character in each element of kingdom will be either '0' or '1'.
- For each i, kingdom[i][i] will be '0'.
- For each i and j, kingdom[i][j] will be equal to kingdom[j][i].
- Each character in each element of build and destroy will be either an uppercase letter ('A'-'Z') or a lowercase letter ('a'-'z').
- For each i, build[i][i] and destroy[i][i] will be 'A'.
- For each i and j, build[i][j] will be equal to build[j][i].
- For each i and j, destroy[i][j] will be equal to destroy[j][i].
{"000","000","000"}
{"ABD","BAC","DCA"}
{"ABD","BAC","DCA"}
Returns: 3
There are three cities in the kingdom, totally disconnected. The only optimal solution is to build the roads between cities 0-1 (cost 1) and 1-2 (cost 2).
{"011","101","110"}
{"ABD","BAC","DCA"}
{"ABD","BAC","DCA"}
Returns: 1
Now the three cities form a connected triangle and we need to destroy one road. Optimal solution is to destroy the road between the cities 0-1 (cost 1).
{"011000","101000","110000","000011","000101","000110"}
{"ABDFFF","BACFFF","DCAFFF","FFFABD","FFFBAC","FFFDCA"}
{"ABDFFF","BACFFF","DCAFFF","FFFABD","FFFBAC","FFFDCA"}
Returns: 7
We have six cities forming two separate triangles. Like in the Example 1, destroy one road in each triangle (costs 1 for each road) and then join the triangles by a new road (costs 5).
{"0"}
{"A"}
{"A"}
Returns: 0
One city is okay just as it is.
{"0001","0001","0001","1110"}
{"AfOj","fAcC","OcAP","jCPA"}
{"AWFH","WAxU","FxAV","HUVA"}
Returns: 0
We have four cities, which are connected in such a way that there is exactly one path between each two cities. Thus there is nothing to reconstruct.
Submissions are judged against all 36 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class KingdomReorganization with a public method int getCost(vector<string> kingdom, vector<string> build, vector<string> destroy) · 36 test cases · 2 s / 256 MB per case