PAT-A1111. 圖-Dijkstra輸出最短路徑

題目鏈接:https://www.patest.cn/contests/pat-a-practise/1111

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2 <= N <= 500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

V1 V2 one-way length time

where V1 and V2 are the indices (from 0 to N-1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance D in the format:

Distance = D: source -> v1 -> … -> destination

Then in the next line print the fastest path with total time T:

Time = T: source -> w1 -> … -> destination

In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

Distance = D; Time = T: source -> u1 -> … -> destination

Sample Input 1:

10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5

Sample Output 1:

Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5

Sample Input 2:

7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5

Sample Output 2:
Distance = 3; Time = 4: 3 -> 2 -> 5

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int INF = 1000000000;
const int NMAX = 500;
int num;
int source, destination;
int dis[NMAX][NMAX];
int tim[NMAX][NMAX];
int d[NMAX];
int t[NMAX];
bool visit[NMAX];
vector<int> pre[NMAX];
vector<int> pret[NMAX];

vector<int> printDis;
vector<int> printTim;
vector<int> temp;

int mintim = INF, minpass = INF;

void DijkstraDis() {
    for(int i = 0; i <num; ++i){
        d[i] = INF;
        visit[i] = false;
        pre[i].push_back(i);
    }
    d[source] = 0;
    for(int i = 0; i < num; ++i){
        int mindis = INF, node = -1;
        for(int j = 0; j < num; ++j){
            if(!visit[j] && d[j]<mindis){
                node = j;
                mindis = d[j];
            }
        }
        if(node == -1) return;
        visit[node] = true;

        for(int k = 0; k < num; ++k){
            if(!visit[k] && dis[node][k] != INF){
                if(d[node]+dis[node][k] < d[k]){
                    d[k] = d[node] + dis[node][k];
                    pre[k].clear();
                    pre[k].push_back(node);
                }else if(d[node]+dis[node][k] == d[k])
                {   pre[k].push_back(node);
                }
            }
        }

    }

}

void DFSDis(int ed){

    if(ed == source){
        temp.push_back(ed);
        int thistim = 0;
        for(int i = (int)temp.size() - 1; i > 0; --i){
            thistim += tim[temp[i]][temp[i-1]];
        }
        if(thistim < mintim){
            mintim = thistim;
            printDis = temp;
        }

        temp.pop_back();
        return;
    }

    temp.push_back(ed);
    for(int j = 0; j < pre[ed].size(); ++j)
        DFSDis(pre[ed][j]);
    temp.pop_back();
}

void DijkstraTim() {
    for(int i = 0; i <num; ++i){
        t[i] = INF;
        visit[i] = false;
        pret[i].push_back(i);
    }
    t[source] = 0;
    for(int i = 0; i < num; ++i){
        int mintim = INF, node = -1;
        for(int j = 0; j < num; ++j){
            if(!visit[j] && t[j]<mintim){
                node = j;
                mintim = t[j];
            }
        }
        if(node == -1) return;
        visit[node] = true;
        for(int k = 0; k < num; ++k){
            if(!visit[k] && tim[node][k] != INF){
                if(t[node]+tim[node][k] < t[k]){
                    t[k] = t[node] + tim[node][k];
                    pret[k].clear();
                    pret[k].push_back(node);
                }else if(t[node]+tim[node][k] == t[k]){
                    pret[k].push_back(node);
                }
            }
        }

    }

}

void DFSTim(int ed){
    if(ed == source){
        temp.push_back(ed);
        int passby = (int)temp.size();
        if(passby < minpass){
            minpass = passby;
            printTim = temp;
        }

        temp.pop_back();
        return;
    }

    temp.push_back(ed);
    for(int j = 0; j < pret[ed].size(); ++j)
        DFSTim(pret[ed][j]);
    temp.pop_back();
}

int main() {
    int M = 0;
    cin >> num >> M;
    fill(dis[0], dis[0] + NMAX * NMAX, INF);
    fill(tim[0], tim[0] + NMAX * NMAX, INF);
    for(int i = 0; i < M; ++i){
        int v1,v2,oneway,d,t;
        cin >> v1 >> v2 >> oneway >> d >> t;
        dis[v1][v2] = d;
        tim[v1][v2] = t;
        if(oneway == 0){
            dis[v2][v1] = d;
            tim[v2][v1] = t;
        }
    }
    cin >> source >> destination;

    DijkstraDis();
    DFSDis(destination);

    DijkstraTim();
    DFSTim(destination);


    if(printTim == printDis) {
        cout << "Distance = " << d[destination] << "; ";
        cout << "Time = " << t[destination] << ": ";
        for(int i = (int)printDis.size() - 1; i >= 0; --i){
            cout << printDis[i];
            if(i > 0) cout << " -> ";
            else cout << endl;
        }
    }
    else {
        cout << "Distance = " << d[destination] << ": ";
        for(int i = (int)printDis.size() - 1; i >= 0; --i){
            cout << printDis[i];
            if(i > 0) cout << " -> ";
            else cout << endl;
        }
        cout << "Time = " << t[destination] << ": ";
        for(int i = (int)printTim.size() - 1; i >= 0; --i){
            cout << printTim[i];
            if(i > 0) cout << " -> ";
            else cout << endl;
        }
    }


    return 0;
}
發佈了23 篇原創文章 · 獲贊 5 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章