1 条题解

  • 0
    @ 2025-5-26 21:32:18
    #include <iostream>
    #include <map>
    #include <string>
    #include <vector>
    #include <queue>
    #include <set>
    #include <limits>
    #include <sstream>
    using namespace std;
    
    struct Edge {
        string to, road;
        double dist, time;
    };
    
    struct State {
        string city;
        string road;
        double cost;
        int turns;
        vector<pair<string, string> > path;
    
        bool operator<(const State& other) const {
            if (cost != other.cost) return cost > other.cost;
            return turns > other.turns;
        }
    };
    
    map<string, vector<Edge> > graph;
    
    void add_road(vector<string>& road_data) {
        string road = road_data[0];
        for (size_t i = 1; i + 3 < road_data.size(); i += 3) {
            string cityA = road_data[i];
            double dist, t;
            stringstream ss1(road_data[i+1]);
            stringstream ss2(road_data[i+2]);
            ss1 >> dist; ss2 >> t;
            string cityB = road_data[i+3];
    
            graph[cityA].push_back((Edge){cityB, road, dist, t});
            graph[cityB].push_back((Edge){cityA, road, dist, t});
        }
    }
    
    void solve(string type, string start, string end) {
        priority_queue<State> pq;
        map<pair<string, string>, double> bestMap;
    
        State startState;
        startState.city = start;
        startState.road = "";
        startState.cost = 0;
        startState.turns = 0;
        startState.path = vector<pair<string, string> >();
        pq.push(startState);
        bestMap[make_pair(start, "")] = 0;
    
        while (!pq.empty()) {
            State cur = pq.top(); pq.pop();
            pair<string, string> node = make_pair(cur.city, cur.road);
            if (bestMap.find(node) != bestMap.end() && bestMap[node] < cur.cost) 
                continue;
    
            if (cur.city == end) {
                cout << "from " << start << endl;
                for (size_t i = 0; i < cur.path.size(); ++i) {
                    cout << cur.path[i].first << " to " << cur.path[i].second << endl;
                }
                return;
            }
    
            for (size_t i = 0; i < graph[cur.city].size(); ++i) {
                Edge e = graph[cur.city][i];
                double newCost;
                int newTurns = cur.turns;
                vector<pair<string, string> > newPath = cur.path;
    
                if (type == "distance") {
                    newCost = cur.cost + e.dist;
                } else if (type == "time") {
                    newCost = cur.cost + e.time;
                } else if (type == "turns") {
                    if (cur.road == e.road) {
                        newCost = cur.cost;
                    } else {
                        newCost = cur.cost + 1;
                        newTurns = cur.turns + 1;
                    }
                } else {
                    continue;
                }
    
                if (cur.road != e.road || cur.path.empty()) {
                    newPath.push_back(make_pair(e.road, e.to));
                } else {
                    newPath.back().second = e.to;
                }
    
                pair<string, string> newNode = make_pair(e.to, e.road);
                if (bestMap.find(newNode) == bestMap.end() || newCost < bestMap[newNode]) {
                    bestMap[newNode] = newCost;
                    State nextState;
                    nextState.city = e.to;
                    nextState.road = e.road;
                    nextState.cost = newCost;
                    nextState.turns = newTurns;
                    nextState.path = newPath;
                    pq.push(nextState);
                }
            }
        }
    }
    
    int main() {
        int n;
        cin >> n;
        string name;
        for (int i = 0; i < n; ++i) {
            cin >> name;
        }
    
        int r;
        cin >> r;
        string line;
        getline(cin, line); // consume newline
    
        for (int i = 0; i < r; ++i) {
            getline(cin, line);
            stringstream ss(line);
            vector<string> road_data;
            string token;
            while (ss >> token) road_data.push_back(token);
            add_road(road_data);
        }
    
        while (getline(cin, line)) {
            if (line == "") continue;
            stringstream ss(line);
            string type, src, dst;
            ss >> type >> src >> dst;
            solve(type, src, dst);
        }
        return 0;
    }
    • 1

    信息

    ID
    583
    时间
    1000ms
    内存
    10MiB
    难度
    9
    标签
    递交数
    4
    已通过
    1
    上传者