PAT甲级真题 1111 Online Map (30分) C++实现(两次dijkstra+dfs,复用封装函数)

题目

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

思路

求最优权重的最短距离路径和最短时间路径,两次dijkstra+dfs即可,两个函数都仅需写一次并重复使用。

对于求节点数最少的最短时间路径,可以传参进去值全为1的权重数组,因为每经过一个节点数目都+1。

这样能够降低很大代码量(难得比柳神的代码简洁)。

代码

#include 
#include 
using namespace std;

#define MAXINT 0x3f3f3f3f

void printPath(vector<int> &path){
    int i = path.size() - 1;
    cout << path[i--];
    while (i >= 0){
        cout << " -> " << path[i--];
    }
    cout << endl;
}

void dfs(vector<vector<int> > &pre, vector<vector<int> > &w, int root, int sum, int &minSum, vector<int> path, vector<int> &minPath){
    path.push_back(root);
    if (pre[root].size()==0) {  //到达叶子
        if (sum < minSum){
            minSum = sum;
            minPath = path;
        }
        return;
    }
    for (int i=0; i<pre[root].size(); i++){
        dfs(pre, w, pre[root][i], sum + w[pre[root][i]][root], minSum, path, minPath);
    }
}

void Dijkstra(int src, vector<vector<int> > &w, vector<int> &dist, vector<vector<int> > &pre){
    vector<bool> visited(dist.size());
    dist[src] = 0;
    for (int i=0; i<dist.size(); i++){
        int minDist = MAXINT;
        int minI = -1;
        for (int j=0; j<dist.size(); j++){
            if (!visited[j] && dist[j]<minDist){
                minDist = dist[j];
                minI = j;
            }
        }
        visited[minI] = true;
        for (int j=0; j<dist.size(); j++){
            if (!visited[j]){
                if (minDist + w[minI][j] < dist[j]){
                    pre[j].clear();
                    pre[j].push_back(minI);
                    dist[j] = minDist + w[minI][j];
                }
                else if (minDist + w[minI][j] == dist[j]){
                    pre[j].push_back(minI);
                }
            }
        }
    }
}

int main() {
    int n, m, src, dst;
    cin >> n >> m;
    vector<vector<int> > len(n, vector<int>(n, MAXINT));
    vector<vector<int> > time(n, vector<int>(n, MAXINT));
    for (int i=0; i<m; i++){
        int u, v, flag, l, t;
        cin >> u >> v >> flag >> l >> t;
        len[u][v] = l;
        time[u][v] = t;
        if (!flag){
            len[v][u] = l;
            time[v][u] = t;
        }
    }
    cin >> src >> dst;
    
    //Dijkstra找最短距离路径
    vector<int> distL(n, MAXINT);
    vector<vector<int> > preL(n);
    Dijkstra(src, len, distL, preL);
    //dfs找最短路径中时间最小路径
    int minL = MAXINT;
    vector<int> minPathL;
    vector<int> path;
    dfs(preL, time, dst, 0, minL, path, minPathL);
    
    //Dijkstra找最短时间路径
    vector<int> distT(n, MAXINT);
    vector<vector<int> > preT(n);
    Dijkstra(src, time, distT, preT);
    //dfs找最短路径中节点数最小的
    int minT = MAXINT;
    vector<int> minPathT;
    path.clear();
    vector<vector<int> > nodes(n, vector<int>(n, 1));  //经过节点数量,统一为1
    dfs(preT, nodes, dst, 0, minT, path, minPathT);

    cout << "Distance = " << distL[dst];
    if (minPathL == minPathT)
        cout << "; ";
    else{
        cout << ": ";
        printPath(minPathL);
    }
    cout << "Time = " << distT[dst] << ": ";
    printPath(minPathT);
    return 0;
}

你可能感兴趣的:(PAT)