Question: Solve in C + + You need to redesign the road system of an imaginary country. The country is composed of N cities ( for
Solve in C
You need to redesign the road system of an imaginary country.
The country is composed of N cities for simplicity numbered from to N 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 C C CM such that C A and CM B and for each index i M there
is a road between cities Ci and Ci
The current state of the road network 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. You want to build some new roads and destroy some of the already existing roads in the
country 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, you want to minimize the total cost
spent on the reconstruction.
You are given three twodimensional arrays:
countryij or : there is an existing road between city i and j if and only if countryij
buildij: the cost for building a road between i and j The values of buildij are represented
using English letters. A B Z represent and a b z represent For
example, if buildb then that means the cost for building a road between city and city is
destroyij: the cost for destroying a road between i and j Again, the values are represented
using English letters like the above.
Your task is to find and print the minimal cost needed for the road network reconstruction.
You dont need to worry about invalid inputs.
Sample input : ABD,BAC,DCA ABD,BAC,DCA
Note: describes the twodimensional array country. ABD,BAC,DCA describes the two
dimensional array build. ABD,BAC,DCA describes the twodimensional array destroy. The input
format is: three strings separated by spaces; each string contains N parts separated by commas; each
part contains N characters.
Sample output :
Comment: There are three cities, totally disconnected.
Sample input : ABD,BAC,DCA ABD,BAC,DCA
Sample output :
Comment: 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 cost
Sample input : note: all inputs are on the same line. I just couldnt fit them in one line in this pdf
ABDFFF,BACFFF,DCAFFF,FFFABD,FFFBAC,FFFDCA
ABDFFF,BACFFF,DCAFFF,FFFABD,FFFBAC,FFFDCA
Sample output :
Comment: We have six cities forming two separate triangles. Destroy one road in each triangle costs
for each road and then join the triangles by a new road costs
Sample input : A A
Sample output :
Comment: One city is okay just as it is
Sample input : AfOj,fAcC,OcAP,jCPA AWFH,WAxU,FxAV,HUVA
Sample output :
Comment: 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
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
