Problem Summary
Given a graph with vertices 1,2,…,N and M undirected edges, find the maximal length of a round trip.
The tour should have two parts. Let the first part be A[1]->A[2]->…->A[K], the second be B[1]->B[2]->…->B[P]. They should satisfy:
- A[1]=B[P]=1, A[K]=B[1]=N.
- The two parts have no common vertices except 1 and N, i.e. A[i]!=B[j], for any 1<i<K, 1<j<P.
- A[i]<A[j], for any 1<=i<j<=K
- B[i]>B[j], for any 1<=i<j<=P
Solution
Since array A is strictly increasing and array B is strictly decreasing, we can use dynamic programming.
We can split the round tour into 2 seperate tours for 2 people.
Let f[i][j] be the maximal number of vertices visited when one person is at vertex i, and the other is at j. The DP equtions are:
|
|
Code
|
|