There is a problem asked in contest. I already solved this problem with dynamic programming and its complexity O(n^2). But i am looking for solution with less efficient way. What will be the complexity of this less efficient way. Thanks for the helps.

Question:

Once upon a time, there were N medieval towns in the beautiful Moldavian territory, uniquely numbered from 1 through N. The town numbered with 1 was the capital city. The towns were connected by N-1 bidirectional roads, each road having a length expressed in kilometers. There was a unique way to travel between any pair of towns without going through a town twice (i.e. the graph of roads was a tree).

When a town was attacked, the situation had to be reported as soon as possible to the capital. The message was carried by harbingers, one of which resided in each town. Each harbinger was characterized by the amount of time required to start the journey and by his constant speed (expressed in minutes per kilometer) after departure.

The message from a town was always carried on the unique shortest path to the capital. Initially, the harbinger from the attacked town carried the message. In each town that he traversed, a harbinger had two options: either go to the next town towards the capital, or leave the message to the harbinger from this town. The new harbinger applied the same algorithm as above. Overall, a message could be carried by any number of harbingers before arriving in the capital.

Your task is to find, for each town, the minimum time required to send a message from that town to the capital.

Input:

The first line of the input file harbingers.in contains one integer N, the number of towns in Moldavia. Each of the following N-1 lines contains three integers u v d, separated by one space, describing a road of length d kilometers between towns numbered with u and v. Subsequently, N-1 pairs of integers follow, one per line. The ith pair, Si Vi, describes the characteristics of the harbinger in the i+1th town: Si is the number of minutes to prepare for the journey, and Vi is the number of minutes needed to travel one kilometer. There is no harbinger in the capital.

5
1 2 20
2 3 12
2 4 1
4 5 3
26 9
1 10
500 2
2 30

Output:

The output file harbingers.out should consist of exactly one line containing N-1 integers. The ith number represents the minimum time, in minutes, required to send a message from the i+1th town to the capital.

share|improve this question
5  
I guess you mean "more efficient way"? Otherwise, its easy to make the algorithm arbitrarily inefficient. – Henry yesterday
i mean less efficent way. I would like to compare their complexity.For instance i am looking for a O(n^3) solution – mustad yesterday
5  
@mustad: O(n^2) is a subset of O(n^3), so an O(n^2) solution is also an O(n^3) solution. – amit yesterday
@amit assume Theta(n^3); it's still trivial to make an algorithm arbitrarily slow. – Jan Dvorak yesterday
By the way, did you know you can sort arrays in Th((3/2)^n)? – Jan Dvorak yesterday
show 3 more comments
feedback

2 Answers

There is a general way to make any dynamic programming solution less efficient. The essence of dynamic programming is to store solutions to sub-problems for reuse.

To make it less efficient in a somewhat reasonable way, get rid of the sub-problem result storage. Instead, recalculate each sub-problem solution whenever you need it.

share|improve this answer
feedback

Using inefficient data structure with same algorithm can help to have O(n^3). Storing towns in a linked list instead of an array will make algorithm one order less efficient.

To make it even less efficient, it is easier to change algorithm. For example checking of all harbinger changing combinations and using minimal, which is exponential in time.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.