【PAT甲级】1111 Online Map

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1111 Online Map (pintia.cn)
🔑中文翻译:在线地图
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

1111 Online Map

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
题意

给定一张图,要求我们找到最短路径和最快路径。

第一行输入分别为路口数量 N N N ,街道数量 M M M ,其中编号从 0 ∽ N − 1 0 \backsim N-1 0N1

接下来 M M M 行,输入格式如下:

V1 V2 one-way length time

其中 V1V2 分别表示起点和终点,one-way1 时说明该边为单向边,为 0 时说明该边为双向边,后面两个参数表示该边的长度以及所需要花费的时间。

最后一行输入源点和终点。

输出格式为:

Distance = D: source -> v1 -> ... -> destination
Time = T: source -> w1 -> ... -> destination

分别表示最短路径和最快路径的输出情况,如果两者路径相同,则输出格式如下:

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

如果最短路径不唯一,则选择最短路径中最快的那条路径(保证唯一)。

如果最快路径不唯一,则选择经过结点最少的哪条路径(保证唯一)。

思路

这道题同样可以利用迪杰斯特拉算法去找最优解,可以通过一个 type 变量来区分是求最短路径还是最快路径,这样就可以合在一起写。具体思路如下:

  1. 输入数据,用链式前向星来存储邻接表。
  2. 寻找最短路径和最快路径,当 type = 0 时,计算的是最短路径,且将路径距离作为第一权值,将花费时间最为第二权值;当 type = 1 时,计算的是最快路径,且将花费时间作为第一权值,将路径中结点数量违第二权值。然后就可以正常的用迪杰斯特拉算法进行计算,以第一权值为依据对数据进行更新。并且用一个 pair 返回,pairfirst 存储最短距离或者最快时间,second 则存储对应的路径。
  3. 输出最终结果,注意如果最短路径和最快路径相同,则需要合在一起输出。
代码
#include<bits/stdc++.h>
using namespace std;

const int N = 510, M = N * N;
int e[M], w1[M], w2[M], ne[M], h[N], idx;
bool st[N];
int dist1[N], dist2[N], pre[N];
int n, m, S, T;

//链式前向星存储邻接表
void add(int a, int b, int c, int d)
{
    e[idx] = b, w1[idx] = c, w2[idx] = d, ne[idx] = h[a], h[a] = idx++;
}

//迪杰斯特拉算法
pair<int, string> dijkstra(int w1[], int w2[], int type)
{
    //初始化
    memset(dist1, 0x3f, sizeof dist1);
    memset(dist2, 0x3f, sizeof dist2);
    memset(st, false, sizeof st);
    dist1[S] = dist2[S] = 0;

    for (int i = 0; i < n; i++)
    {
        int t = -1;
        for (int j = 0; j < n; j++)    //找到一个权值最小的点
            if (!st[j] && (t == -1 || dist1[t] > dist1[j]))
                t = j;
        st[t] = true;

        for (int k = h[t]; k != -1; k = ne[k])    //更新信息
        {
            int j = e[k];

            int cost;   //分别表示计算最短和最快路径的情况
            if (!type)   cost = w2[k]; //最短路径
            else    cost = 1; //最快路径

            if (dist1[j] > dist1[t] + w1[k])
            {
                //第一权值可以更新的情况
                dist1[j] = dist1[t] + w1[k];
                dist2[j] = dist2[t] + cost;
                pre[j] = t;
            }
            else if (dist1[j] == dist1[t] + w1[k])
            {
                //第一权值相等,但第二权值可以更新的情况
                if (dist2[j] > dist2[t] + cost)
                {
                    dist2[j] == dist2[t] + cost;
                    pre[j] = t;
                }
            }
        }
    }

    //将结果用pair返回
    vector<int> path;
    for (int i = T; i != S; i = pre[i])  path.push_back(i);
    pair<int, string> res;
    res.first = dist1[T]; //第一权值的最小值
    res.second = to_string(S);    //对应的路径
    for (int i = path.size() - 1; i >= 0; i--)
        res.second += " -> " + to_string(path[i]);
    return res;
}

int main()
{
    cin >> n >> m;

    //输入街道信息
    memset(h, -1, sizeof h);
    for (int i = 0; i < m; i++)
    {
        int a, b, t, c, d;
        cin >> a >> b >> t >> c >> d;
        add(a, b, c, d);
        if (!t)  add(b, a, c, d);
    }

    cin >> S >> T;  //输入源点和终点

    auto A = dijkstra(w1, w2, 0);   //找到最短路径
    auto B = dijkstra(w2, w1, 1);   //找到最快路径

    if (A.second != B.second)  //如果最短路径和最快路径不同,则分开输出
    {
        printf("Distance = %d: %s\n", A.first, A.second.c_str());
        printf("Time = %d: %s\n", B.first, B.second.c_str());
    }
    else    //如果相同,则路径一起输出
    {
        printf("Distance = %d; Time = %d: %s\n", A.first, B.first, A.second.c_str());
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值