Description a long time ago in a galaxy far far away the

Assignment Help Data Structure & Algorithms
Reference no: EM13359239

Description: A long time ago in a galaxy far, far away, the country Mafghanistan had n cities and m old roads, where each road connected a pair of cities. Due to the treacherous mountains, there was no road or even path between some cities (i.e., some cities were unreachable from others). During Operation Mafghanistan Freedom, which spread democracy across Mafghanistan like wildfire in Santa Barbara in the summer, the Mamerican forces evaporated all the old roads in Mafghanistan. As the Commanding General of the Mamerican forces, you are in charge of Operation Mafghan Reconstruction, which will build new roads in Mafghanistan subject to the following constraints:

    Due to the treacherous mountains, you may build new roads only between the cities where old roads existed before Operation Mafghanistan Freedom.

    You should build enough new roads such that if City A was reachable from City B via some old roads, City A must be reachable from City B via some new roads.

    You should minimize the total lengths of the new roads to be built.

Define a region to be a set of cities that will be reachable from one another via some new roads. Find all the regions using algorithms with good asymptotic running times.

You may NOT use STL classes except the string class.

Input

Read input from cin. The input contains the number of cities (n), the number of old roads (m), and a series of old roads where each road is represented by its two terminal cities and its length. Each city is represented by a unique integer from 0 to n-1. Each road length is represented by an integer. The following EBNF specifies the input. An example input is available in the hand out. Your program need not handle invalid input (such as invalid file format or invalid numbers representing cities).

<input> ::= <num-cities> <num-old-roads> <old-road>*

<num-cities> ::= <ws>* <number> <ws>* '\n'

<num-old-roads> ::= <ws>* <number> <ws>* '\n'

<old-road> ::= <ws>* <city> <ws>+ <city> <ws>+ <length> <ws>* '\n'

<city> ::= <number>

<length> ::= <number>

<number> ::= <digit>+

<digit> ::= '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'

<ws> ::= ' ' | '\t'

Output

    On the first line, print

    <?xml version="1.5"?>

    On the second line, print:

    <country>

    Print each region in the ascending order of the number of cities in the region. For each region:

        On the first line, print:

        <region>

        Then, print each new road per line in the ascending order of the lengths of the roads. For each new road, print its two terminal cities represented by their numbers (print the smaller number before the larger number), and then the length of the road, as in the following example:

        <road>0 5 10</road>

        On the last line, print:

        </region>

    On the last line, print:

    </country>

The output of your program must match the output of the reference program. In other words, if you run

diff my_output your_output

you should see no output from diff.

Your program must exit with the exit code 0, i.e., call return 0 from the function main() or call exit(0).

Determinism in comparison of equal values

To avoid non-determinism of your algorithm when two roads e1=(s1, t1) and e2=(s2, t2) have the same length, consider e1 to be shorter than e2 if and only if

 

    min(s1, t1) < min(s2, t2), or

    min(s1, t1) == min(s2, t2) and max(s1, t1) < max(s2, t2)

To avoid non-determinism of your algorithm when two regions r1={...} and r2={...} have the same number of cities, consider r1 to have fewer cities than r2 if and only if min(r1) < min(r2), wheremin(s) returns the smallest member of the set s. Note that regions are disjoint sets of cities.

Resource limit

The gradebot has to limit resources, such as CPU time and memory, that each program can consume. If a program exceeds its resource limit, the gradebot will kill it, but the gradebot cannot distinguish between this case and other problems in the program (such as segmentation fault).

To test if a program exceeds the resource limit imposed by the gradebot, run:

bash -c 'ulimit -c 0 -d 500000 -f 100000 -m 100000 -n 50 -s 50000 -t 90 -v 500000; spantree < input_file > output_file'

 

When the program exceeds the resource limit, you will see the message Killed.

To learn more about resource limit, run man bash and skip to the section on ulimit (by typing /ulimit followed by a RETURN).

Hints

You probably need to implement union-find, min heap, minimum cost spanning tree, and an O(n log n) sorting algorithm (e.g., Quick Sort, in Section 7.3 of the textbook).

To optimize your program, run g++ with the -O3 option.

Reference no: EM13359239

Questions Cloud

Is there any risk to the employee in a defined benefit : is there any risk to the employee in a defined benefit plan?one of the advantages of the defined benefit plan to
Technology use in the workplacethe actual case study : technology use in the workplacethe actual case study analysiswhen considering the impact of the information age on
What do you think hr can do to provide management with a : what do you think hr can do to provide management with a better understanding of why exit interviews can be
Domestic and international hrmwhat are the main : domestic and international hrmwhat are the main similarities of domestic and international hrm?discuss two hr
Description a long time ago in a galaxy far far away the : description a long time ago in a galaxy far far away the country mafghanistan had n cities and m old roads where each
360 degree feedback and human resource managementim having : 360 degree feedback and human resource managementim having a troubles trying to discuss the relation betwwen 360 degree
Importance of human resources1 developing a report on the : importance of human resources1. developing a report on the effectiveness of a staffing process for entry-level jobs
Factor evaluation system vs hays profile methodshow the : factor evaluation system vs. hays profile methodshow the differences and similarities between the factor evaluation
While companies are trying to optimize their global supply : while companies are trying to optimize their global supply chain governments are aiming for the protection of their

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  What is the difference between syntax and semantics

Explain the distinction between an ambiguity in a proposed algorithm and an ambiguity in the representation of an algorithm.

  Define file type

Define File Type and provide some of the important information to know about it and where do file types come from?

  Karatsuba''s divide-and-conquer algorithm

In class we discussed Karatsuba's divide-and-conquer algorithm for integer multiplication, which multiplies n-bit numbers by recursively multiplying n bit numbers. We take two numbers X and Y and split them each into their most significant half a..

  Use of sequential or relational file

Assume you wish to keep a record of everything you have in your house. You could use a sequential data document to keep track of common items such as 'cloths'.

  Write down the algorithm to insert an item

Write down the sample code to create a Linked List and allocate storage space for a node Write down the algorithm to insert an item At the beginning of a linked list

  Test the database management system functionality

In a report that less than half of all companies validate the in their databases and test database management system's functionality. Explain your answer.

  Computing entropy of plaintext message

Compute the entropy of the plaintext message?

  Communicationa significant distinction between online and

communicationa significant distinction between online and face-to-face classes lies in the area of

  Inventory tracking database

Construct a relational database of your choice. The DB should contain no more than six tables. Define three business requirements that this database will provide.

  What are the properties of an algorithm

What is a first-in-first-out data structure ? Write algorithms to perform the following operations on it - create, insertion, deletion, for testing overflow and empty conditions.

  Sorting algorithm to put whole array in order

Assume you have the array of 1000 records in which only few are out of order and they are not very far from their correct positions.

  Describe a dynamic programming algorithm

Let Fi(x) = i * (1+log x). Describe a dynamic programming algorithm to input 2 integers x and m and determine how to break x into m integers x1, x2, ..., Xm such that f1(x1) + f2(x2)+----+fm(Xm) is the largest among all possible ways of breaking x..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd