One-way flight trip problem

后端 未结 18 1506

You are going on a one-way indirect flight trip that includes billions an unknown very large number of transfers.

  • You are not stoppi
18条回答
  •  无人及你
    2020-12-12 17:30

    I provide here a more general solution to the problem:

    You can stop several times in the same airport, but you have to use every ticket exactly 1 time

    You can have more than 1 ticket for each part of your trip.

    Each ticket contains src and dst airport.

    All the tickets you have are randomly sorted.

    You forgot the original departure airport (very first src) and your destination (last dst).

    My method returns list of cities (vector) that contain all specified cities, if such chain exists, and empty list otherwise. When there are several ways to travel the cities, the method returns lexicographically smallest list.

    #include
    #include
    #include
    #include
    #include
    #include
    
    using namespace std;
    
    struct StringPairHash
    {
        size_t operator()(const pair &p) const {
            return hash()(p.first) ^ hash()(p.second);
        }
    };
    
    void calcItineraryRec(const multimap &cities, string start,
        vector &itinerary, vector &res,
        unordered_set, StringPairHash> &visited, bool &found)
    {
        if (visited.size() == cities.size()) {
            found = true;
            res = itinerary;
            return;
        }
        if (!found) {
            auto pos = cities.equal_range(start);
            for (auto p = pos.first; p != pos.second; ++p) {
                if (visited.find({ *p }) == visited.end()) {
                    visited.insert({ *p });
                    itinerary.push_back(p->second);
                    calcItineraryRec(cities, p->second, itinerary, res, visited, found);
                    itinerary.pop_back();
                    visited.erase({ *p });
                }
            }
        }
    }
    
    vector calcItinerary(vector> &citiesPairs)
    {
        if (citiesPairs.size() < 1)
            return {};
    
        multimap cities;
        set uniqueCities;
        for (auto entry : citiesPairs) {
            cities.insert({ entry });
            uniqueCities.insert(entry.first);
            uniqueCities.insert(entry.second);
        }
    
        for (const auto &startCity : uniqueCities) {
            vector itinerary;
            itinerary.push_back(startCity);
            unordered_set, StringPairHash> visited;
            bool found = false;
            vector res;
            calcItineraryRec(cities, startCity, itinerary, res, visited, found);
            if (res.size() - 1 == cities.size())
                return res;
        }
        return {};
    }
    

    Here is an example of usage:

        int main()
        {
            vector> cities = { {"Y", "Z"}, {"W", "X"}, {"X", "Y"}, {"Y", "W"}, {"W", "Y"}};
            vector itinerary = calcItinerary(cities); // { "W", "X", "Y", "W", "Y", "Z" }
            // another route is possible {W Y W X Y Z}, but the route above is lexicographically smaller.
    
            cities = { {"Y", "Z"}, {"W", "X"}, {"X", "Y"}, {"W", "Y"} };
            itinerary = calcItinerary(cities);  // empty, no way to travel all cities using each ticket exactly one time
        }
    

提交回复
热议问题