L3-007 天梯地图

#include <bits/stdc++.h>

using namespace std;

const int N = 550;

using pii = pair<int, int>;

struct Node{
    int v, length, time;
};

vector<Node> g[N];
int n, m;
int st, ed;
int distTime[N];
int dist[N];
int Timedistpre[N], distpre[N];
int node[N];
bool state[N];
vector<int> minTimepath, minDistpath;

void Dijkstra1() {
    memset(state, 0, sizeof state);
    memset(distTime, 0x3f, sizeof(distTime));
    memset(dist, 0x3f, sizeof dist);
    priority_queue<pii, vector<pii>, greater<pii>> heap;
    distTime[st] = 0;
    heap.push({distTime[st], st});
    dist[st] = 0;

    while (heap.size()) {
        auto t = heap.top();
        heap.pop();

        int u = t.second;
        if (state[u]) continue;
        state[u] = true;

        for (auto itr : g[u]) {
            int v = itr.v, time = itr.time, w = itr.length;
            if (distTime[v] > distTime[u] + time) { //时间最快
                distTime[v] = distTime[u] + time;
                dist[v] = dist[u] + w;
                heap.push({distTime[v], v});
                Timedistpre[v] = u;
            } else if (distTime[v] == distTime[u] + time) {
                if (dist[v] > dist[u] + w) {
                    dist[v] = dist[u] + w;
                    Timedistpre[v] = u;
                } 
            }
        }
    }

    int eed = ed;
    while (eed != st) {
        minTimepath.push_back(eed);
        //cout << eed << " ";
        eed = Timedistpre[eed];
    }
    minTimepath.push_back(st);
    reverse(minTimepath.begin(), minTimepath.end());
    //cout << "\n";
}

void Dijkstra2() {
    memset(state, 0, sizeof state);
    memset(dist, 0x3f, sizeof dist);
    memset(node, 0x3f, sizeof node);
    priority_queue<pii, vector<pii>, greater<pii>> heap;
    dist[st] = 0;
    heap.push({ dist[st], st});
    node[st] = 1;
    while (heap.size()) {
        auto t = heap.top();
        heap.pop();

        int u = t.second;
        if (state[u]) continue;
        state[u] = true;

        for (auto itr : g[u]) {
            int v = itr.v, w = itr.length;
            if (dist[v] > dist[u] + w) {
                dist[v] = dist[u] + w;
                distpre[v] = u;
                node[v] = node[u] + 1;
                heap.push({dist[v], v});
            } else if (dist[v] == dist[u] + w) {
                if (node[v] > node[u] + 1) {
                    node[v] = node[u] + 1;
                    distpre[v] = u;
                }
            }
        }
    }

    int eed = ed;
    while (eed != st) {
        minDistpath.push_back(eed);
        //cout << eed << " ";
        eed = distpre[eed];
    } 
    minDistpath.push_back(st);
    reverse(minDistpath.begin(), minDistpath.end());
}

int main() {
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int u, v, type, length, time;
        cin >> u >> v >> type >> length >> time;
        g[u].push_back({v, length, time});
        if (type == 0) g[v].push_back({u, length, time});
    }
    cin >> st >> ed;
    Dijkstra1();
    Dijkstra2();
    //minTimepath, minDistpath;
    if (minTimepath == minDistpath) {
        cout << "Time = " << distTime[ed] << "; Distance = " << dist[ed] << ": ";
        for (int i = 0; i < minTimepath.size(); i++) {
            cout << minTimepath[i];
            if (i != (int)minTimepath.size() - 1) {
                cout << " => ";
            }
        }
    } else {
        cout << "Time = " << distTime[ed] << ": ";
        for (int i = 0; i < minTimepath.size(); i++) {
            cout << minTimepath[i];
            if (i != (int)minTimepath.size() - 1) {
                cout << " => ";
            }
        }
        cout << "\n";
        cout << "Distance = " << dist[ed] << ": ";
        for (int i = 0; i < minDistpath.size(); i++) {
            cout << minDistpath[i];
            if (i != (int)minDistpath.size() - 1) {
                cout << " => ";
            }
        }
    }

    return 0;
}
posted @ 2022-04-20 15:22  Xxaj5  阅读(26)  评论(0编辑  收藏  举报