Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8769787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:25:43+00:00 2026-06-13T17:25:43+00:00

I found a problem on Algorithms on usaco , I cannot link to real

  • 0

I found a problem on Algorithms on usaco, I cannot link to real problem as it requires authentication so pasting it.

You have just won a contest where the prize is a free vacation in Canada. You must travel via air, and the cities are ordered from east to west. In addition, according to the rules, you must start at the further city west, travel only east until you reach the furthest city east, and then fly only west until you reach your starting location. In addition, you may visit no city more than once (except the starting city, of course).

Given the order of the cities, with the flights that can be done (you can only fly between certain cities, and just because you can fly from city A to city B does not mean you can fly the other direction), calculate the maximum number of cities you can visit.

This is part of tutorial text on dynamic programming. There is suggestion for solution also given in the tutorial.

Imagine having two travelers who start in the western most city. The travelers take turns traveling east, where the next traveler to move is always the western-most, but the travelers may never be at the same city, unless it is either the first or the last city. However, one of the traveler is only allowed to make “reverse flights,” where he can travel from city A to city B if and only if there is a flight from city B to city A.

It’s not too difficult to see that the paths of the two travelers can be combined to create a round-trip, by taking the normal traveler’s path to the eastern-most city, and then taking the reverse of the other traveler’s path back to the western-most city. Also, when traveler x is moved, you know that the traveler y has not yet visited any city east of traveler x except the city traveler y is current at, as otherwise traveler y must have moved once while x was west of y.

As far as I understood the solution, I think that the solution can be done by keeping a one dimensional table for outgoing city in forward direction and a table for outgoing city in reverse direction.
eg. If I have 5 cities, A,B…E orientated in required direction and directed graph between cities is A is starting city and E is the destination.

   A | B | C | D | E
A  0 | 1 | 0 | 0 | 0
B  0 | 0 | 1 | 1 | 0
C  1 | 0 | 0 | 0 | 1
D  0 | 0 | 0 | 0 | 1
E  0 | 0 | 1 | 0 | 0

I am keeping two tables one for outgoing city I will fill it by initializing first city as 1 ie. number of maximum cities visited and then for each next entry scan all the previous cities from which a path exists to the current city and then choose maximum, doing the same for reverse traveler.

table[i] = Max(j=i to 0) (table[j])

I will have maximum at the destination city, but this doesn’t guarantee that any city is not repeated. If I keep one more entry in the array field along with maximum number I won’t be able to switch it from forward to reverse. I am learning dynamic programming so may be I did not get correct idea.
This is the table which I constructed for the graph

A | B | C | D | E
1 | 2 | 3 | 3 | 4
1 | 1 | 2 | 1 | 3

so will take maximum from both. Please provide directions/hints so that I can proceed in right direction.

PS. this is not a homework question

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:25:45+00:00Added an answer on June 13, 2026 at 5:25 pm

    Your approach with the two tables models the state for each traveler independently, as the number of cities already visited, combined with the location where that traveler is currently staying. As you found out yourself, that won’t prevent visiting a city twice.

    Instead, I’d use three elements to model state: the city where one traveler is currently located, the city where the other is located, and the number of cities they have visited in total, i.e. the sum of their individual counts. So you’d have a single 2d table, instead of your two 1d tables. Cell (f, r) would contain the best known number of total cities visited when the forward traveler is in city f and the reverse traveler is in city r.

    You’d probably iterate over these states in order of their minimal element. That means you’ll next expand a state which you have not expanded yet, and where the smaller of these two numbers is minimal among all unexpanded states. If in that state, f < r, then you’d use it to update states (f’, r) with f’ > r, if there is a flight from f to f’. On the other hand, if r < f you update (f, r’) with r’ > f and a flight from r’ to r.

    In pseudocode:

    first = (f: 0, r: 0)  # tuple with two members, called f and r
    todo = set { first }  # a set with a tuple as its only element
    visited = a map from tuples to integers, unset values defaulting to 0
    visited[first] = 1
    while todo is not empty:
      best = ∞
      cur = null
      for t in todo:
        if min(t.f, t.r) < best:
          best = min(t.f, t.r)
          cur = t
      todo.remove(cur)
      if (cur.f < cur.r):
        for f' in cities where flights from f arrive:
          next = (f: f', r: cur.r)  # keep r but use f' as the new f
          todo.add(next)            # will do nothing if it already is in the set
          visited[next] = max(visited[next], visited[cur] + 1)
      else:
        for r' in cities where flights to r depart:
          next = (f: cur.f, r: r')
          todo.add(next)
          visited[next] = max(visited[next], visited[cur] + 1)
    best = 0
    for cur in keys of visited:
      if best < visited[cur]:
        if there is a flight from cur.f to cur.r: # can close the tour
          best = visited[cur]
    return best
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have encountered a very strange class not found problem in our web app
I'am currently working on GWTs Activity-Place implementation. Now I have found problem with the
I have two branches: trunk, production. I have found a problem in trunk, made
I have given run-time functions for two algorithms solving the same problem. Let's say
Seem to run into a service endpoint not found problem when trying to get
I found a problem with host - client float standard in OpenCL. The problem
I found some problem. When i running apc_store and more times update a page
I found this problem in a book. Problem: What is the output of the
I've made a very simple aspect, and found a problem when debugging it (see
Possible Duplicate: Can main function call itself in C++? I found this problem very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.