• PAT甲级——A1111 Online Map【30】


    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), 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 N1) 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


      1 #include <iostream>
      2 #include <vector>
      3 using namespace std;
      4 #define inf 999999999
      5 int N, M, start, des, shortest[2];//shortest存储最短路径的长度(下标为0)和最快路径的时间(下标为1)
      6 int graph[505][505][2];//图的邻接矩阵
      7 int dis[505], past[505], num[505];////各点最短距离、父节点、num求最短路径时存储时间,求最快路径时存储当前路径上结点个数
      8 bool visit[505] = { false };
      9 vector<int>path[2];//下标为0存储最短路径,下标为1存储最快路径
     10 void Dijkstra(int k)//k==0求最短路径,k==1求最快路径
     11 {
     12     while (visit[des] == false)
     13     {
     14         int v = -1, min = inf;
     15         for (int i = 0; i < N; ++i)
     16         {
     17             if (visit[i] == false && min > dis[i])
     18             {
     19                 v = i;
     20                 min = dis[i];
     21             }            
     22         }
     23         if (v == -1)break;
     24         visit[v] = true;
     25         for (int i = 0; i < N; ++i)
     26         {
     27             if (visit[i] == false && graph[v][i][k] != 0 && dis[i] > dis[v] + graph[v][i][k])
     28             {
     29                 dis[i] = dis[v] + graph[v][i][k];//更新距离
     30                 past[i] = v;//更新父节点
     31                 if (k == 0)//是求最短路径
     32                     num[i] = num[v] + graph[v][i][1];//叠加时间
     33                 else//求最快路径
     34                     num[i] = num[v] + 1;//叠加经过的路径节点
     35             }
     36             else if (graph[v][i][k] != 0 && dis[i] == dis[v] + graph[v][i][k])//路径距离相等
     37             {
     38                 if (k == 0 && num[i] > num[v] + graph[v][i][1])//时间更短
     39                 {
     40                     past[i] = v;//要经过这个点
     41                     num[i] = num[v] + graph[v][i][1];//叠加经过的时间
     42                 }
     43                 else if (k == 1 && num[i] > num[v] + 1)//经过更少的节点
     44                 {
     45                     past[i] = v;//则经过该点
     46                     num[i] = num[v] + 1;//叠加经过的节点
     47                 }
     48             }
     49         }
     50     }
     51     shortest[k] = dis[des];//存储答案信息
     52 }
     53 void DFS(int v, int k)//使用DFS寻找出答案
     54 {
     55     if (v == start)//到起点
     56     {
     57         path[k].push_back(v);
     58         return;
     59     }
     60     DFS(past[v], k);//从后向前寻根,故DFS后进行记录路径,则路径则是顺序的
     61     path[k].push_back(v);
     62 }
     63 bool cmp()
     64 {
     65     if (path[0].size() != path[1].size())
     66         return false;
     67     for (int i = 0; i < path[0].size(); ++i)
     68         if (path[0][i] != path[1][i])
     69             return false;
     70     return true;
     71 }
     72 void printPath(int k)//路径打印
     73 {
     74     for (int i = 0; i < path[k].size(); ++i)
     75         cout << path[k][i] << (i == path[k].size() - 1 ? "" : " -> ");
     76     cout << endl;
     77 }
     78 int main()
     79 {
     80     cin >> N >> M;
     81     while (M--)
     82     {
     83         int a, b, c, d, e;
     84         cin >> a >> b >> c >> d >> e;
     85         graph[a][b][0] = d;
     86         graph[a][b][1] = e;
     87         if (c == 0)//有双向道
     88         {
     89             graph[b][a][0] = d;
     90             graph[b][a][1] = e;
     91         }
     92     }
     93     cin >> start >> des;
     94     for (int i = 0; i < 2; ++i)//使用两次Dij+DFS
     95     {
     96         fill(visit, visit + N, false);
     97         fill(dis, dis + N, inf);
     98         fill(num, num + N, inf);
     99         dis[start] = 0;
    100         num[start] = 0;
    101         Dijkstra(i);
    102         DFS(des,i);//从终点开始寻根
    103     }
    104     if (cmp())//比较路径是否相同
    105     {
    106         printf("Distance = %d; Time = %d: ", shortest[0], shortest[1]);
    107         printPath(0);
    108     }
    109     else//不相等
    110     {
    111         printf("Distance = %d: ", shortest[0]);
    112         printPath(0);
    113         printf("Time = %d: ", shortest[1]);
    114         printPath(1);
    115     }
    116     return 0;
    117 }
  • 相关阅读:
    初识JSP知识
    Session每次访问都回新建对象问题的处理
    C++ 重载中括号
    C++ 重载输出符号
    C++ 单目运算 and 双目运算符重载complex
    C++运算符重载范例
    c++ 运算符重载之成员函数重载
    c++ 友元函数
    c++ static 类
    c++ const 类
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11451291.html
Copyright © 2020-2023  润新知