### 11153 - Museums

Posted:

**Sun Dec 31, 2006 8:56 am**my dfs (backtracking) solution is getting TLE

what heuristics may i add to cut the run time?

what heuristics may i add to cut the run time?

The Online Judge board

https://uva.onlinejudge.org/board/

https://uva.onlinejudge.org/board/viewtopic.php?f=37&t=13634

Page **1** of **2**

Posted: **Sun Dec 31, 2006 8:56 am**

my dfs (backtracking) solution is getting TLE

what heuristics may i add to cut the run time?

what heuristics may i add to cut the run time?

Posted: **Sun Dec 31, 2006 10:20 am**

You should avoid to get into equivalent states more than once. Note that a state consists of visited museums, current location and time used.

But this may still be too slow, since there are up to 68927488 states.

Note that actually you are only interested in the minimum time to reach a certain position having visited some subset of the museums.

But this may still be too slow, since there are up to 68927488 states.

Note that actually you are only interested in the minimum time to reach a certain position having visited some subset of the museums.

Posted: **Mon Jan 01, 2007 10:55 am**

Can you give me some further hint on this problem?

I first thought to use the dynamic programming method that is similar to that in the Hamilton Path which runs in O(2^n*n^2) time but then found one can past a node several times...

my searching + optimization got TLE....

wondering what is the correct method for solving this problem ...

I first thought to use the dynamic programming method that is similar to that in the Hamilton Path which runs in O(2^n*n^2) time but then found one can past a node several times...

my searching + optimization got TLE....

wondering what is the correct method for solving this problem ...

Posted: **Mon Jan 01, 2007 3:56 pm**

Another hint:

Precalculate the fastest routes between any two nodes. You are not interested in how many times you passed a node, just how many times a route stops there, so you can imagine having direct connections corresponding to fastest routes.

Precalculate the fastest routes between any two nodes. You are not interested in how many times you passed a node, just how many times a route stops there, so you can imagine having direct connections corresponding to fastest routes.

Posted: **Mon Jan 01, 2007 4:40 pm**

Actually, just a bitmask of visited museums and current location can be enough. This would be just about 54000 states.Adrian Kuegel wrote:Note that a state consists of visited museums, current location and time used.

But this may still be too slow, since there are up to 68927488 states.

You can use this DP to find minimum time required to visit a subset of museums, and then iterate over all subsets to check their feasibility and pick the one with maximum sum of fun indexes.

Posted: **Mon Jan 01, 2007 4:48 pm**

Yes, that was also my method. I just wanted to show how to optimize his dfs function.

Posted: **Mon Jan 01, 2007 9:57 pm**

This trick is often being met on TC events...

Posted: **Tue Jan 02, 2007 2:38 am**

but what about the restriction on the money. how are you gonna handle it ??? please helpActually, just a bitmask of visited museums and current location can be enough. This would be just about 54000 states.

You can use this DP to find minimum time required to visit a subset of museums, and then iterate over all subsets to check their feasibility and pick the one with maximum sum of fun indexes.

BYE

Posted: **Tue Jan 02, 2007 3:14 am**

Dear **coolguy**:

Please think about that yourself! The point you mentioned is important in getting an acceptable solution for this task. Hint: think how money used is computed.

By the way, we have made several judge solutions for this task. The DP one takes 0.2x seconds. Another acceptable solution is Dijkstra. That solution is longer, has an asymptotically larger time complexity, but is easier to understand. That solution takes about 1 second. The two solutions I wrote above are both in PASCAL.

Please think about that yourself! The point you mentioned is important in getting an acceptable solution for this task. Hint: think how money used is computed.

By the way, we have made several judge solutions for this task. The DP one takes 0.2x seconds. Another acceptable solution is Dijkstra. That solution is longer, has an asymptotically larger time complexity, but is easier to understand. That solution takes about 1 second. The two solutions I wrote above are both in PASCAL.

Posted: **Tue Jan 02, 2007 3:15 am**

As I said, you can actually generate and check every possible subset of museums. Just ignore those subsets, which you can't afford due to time or money contraints.coolguy wrote:but what about the restriction on the money. how are you gonna handle it ??? please help

Posted: **Tue Jan 02, 2007 3:55 am**

hey thank you observer and mf.

just after posting in the board i came to understand what wf was trying to say. i got it accepted less than 1 sec. i wasnt understanding how you decreased your states. now i understand

thank you once again for letting me know about the idea:)

Bye

just after posting in the board i came to understand what wf was trying to say. i got it accepted less than 1 sec. i wasnt understanding how you decreased your states. now i understand

thank you once again for letting me know about the idea:)

Bye

Posted: **Tue Jan 02, 2007 7:29 am**

To mf:

how to code using bitmask dp? i googled but found

nothing. can you tell me how it is done or give me

any link from where i can learn it?

To observer:

can you give some hint on the Dijkstra method of

solving this problem?

Thanks.

how to code using bitmask dp? i googled but found

nothing. can you tell me how it is done or give me

any link from where i can learn it?

To observer:

can you give some hint on the Dijkstra method of

solving this problem?

Thanks.

Posted: **Tue Jan 02, 2007 7:41 am**

About the Dijkstra method:

Thinking about the order this Dijkstra goes (i.e. what cases are traversed first) will lead you to the DP solution.

This hint by Adrian basically describes how we can apply Dijkstra algorithm.Adrian Kuegel wrote:Note that actually you are only interested in the minimum time to reach a certain position having visited some subset of the museums.

Thinking about the order this Dijkstra goes (i.e. what cases are traversed first) will lead you to the DP solution.

Posted: **Thu Aug 16, 2007 9:30 am**

I got several WA for this problem,

please give me some IO

please give me some IO

Posted: **Wed Aug 29, 2007 8:21 am**

Ya, can anyone post some tricky test cases?