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 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 <vector>
using namespace std;
#define inf 999999999
int N, M, start, des, shortest[];//shortest存储最短路径的长度(下标为0)和最快路径的时间(下标为1)
int graph[][][];//图的邻接矩阵
int dis[], past[], num[];////各点最短距离、父节点、num求最短路径时存储时间,求最快路径时存储当前路径上结点个数
bool visit[] = { false };
vector<int>path[];//下标为0存储最短路径,下标为1存储最快路径
void Dijkstra(int k)//k==0求最短路径,k==1求最快路径
{
while (visit[des] == false)
{
int v = -, min = inf;
for (int i = ; i < N; ++i)
{
if (visit[i] == false && min > dis[i])
{
v = i;
min = dis[i];
}
}
if (v == -)break;
visit[v] = true;
for (int i = ; i < N; ++i)
{
if (visit[i] == false && graph[v][i][k] != && dis[i] > dis[v] + graph[v][i][k])
{
dis[i] = dis[v] + graph[v][i][k];//更新距离
past[i] = v;//更新父节点
if (k == )//是求最短路径
num[i] = num[v] + graph[v][i][];//叠加时间
else//求最快路径
num[i] = num[v] + ;//叠加经过的路径节点
}
else if (graph[v][i][k] != && dis[i] == dis[v] + graph[v][i][k])//路径距离相等
{
if (k == && num[i] > num[v] + graph[v][i][])//时间更短
{
past[i] = v;//要经过这个点
num[i] = num[v] + graph[v][i][];//叠加经过的时间
}
else if (k == && num[i] > num[v] + )//经过更少的节点
{
past[i] = v;//则经过该点
num[i] = num[v] + ;//叠加经过的节点
}
}
}
}
shortest[k] = dis[des];//存储答案信息
}
void DFS(int v, int k)//使用DFS寻找出答案
{
if (v == start)//到起点
{
path[k].push_back(v);
return;
}
DFS(past[v], k);//从后向前寻根,故DFS后进行记录路径,则路径则是顺序的
path[k].push_back(v);
}
bool cmp()
{
if (path[].size() != path[].size())
return false;
for (int i = ; i < path[].size(); ++i)
if (path[][i] != path[][i])
return false;
return true;
}
void printPath(int k)//路径打印
{
for (int i = ; i < path[k].size(); ++i)
cout << path[k][i] << (i == path[k].size() - ? "" : " -> ");
cout << endl;
}
int main()
{
cin >> N >> M;
while (M--)
{
int a, b, c, d, e;
cin >> a >> b >> c >> d >> e;
graph[a][b][] = d;
graph[a][b][] = e;
if (c == )//有双向道
{
graph[b][a][] = d;
graph[b][a][] = e;
}
}
cin >> start >> des;
for (int i = ; i < ; ++i)//使用两次Dij+DFS
{
fill(visit, visit + N, false);
fill(dis, dis + N, inf);
fill(num, num + N, inf);
dis[start] = ;
num[start] = ;
Dijkstra(i);
DFS(des,i);//从终点开始寻根
}
if (cmp())//比较路径是否相同
{
printf("Distance = %d; Time = %d: ", shortest[], shortest[]);
printPath();
}
else//不相等
{
printf("Distance = %d: ", shortest[]);
printPath();
printf("Time = %d: ", shortest[]);
printPath();
}
return ;
}

PAT甲级——A1111 Online Map【30】的更多相关文章

  1. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  2. PAT甲级1111. Online Map

    PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...

  3. PAT甲级——1111 Online Map (单源最短路经的Dijkstra算法、priority_queue的使用)

    本文章同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90041078   1111 Online Map (30 分) ...

  4. PAT甲级——1131 Subway Map (30 分)

    可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30  ...

  5. PAT甲级——A1131 Subway Map【30】

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  6. 【刷题-PAT】A1111 Online Map (30 分)

    1111 Online Map (30 分) Input our current position and a destination, an online map can recommend sev ...

  7. pat 甲级 1022. Digital Library (30)

    1022. Digital Library (30) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Di ...

  8. pat 甲级 1049. Counting Ones (30)

    1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ...

  9. pat 甲级 1072. Gas Station (30)

    1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...

随机推荐

  1. table 表头不动,tbody滚动对齐

    http://www.imaputz.com/cssStuff/bigFourVersion.html# https://blog.csdn.net/yiifaa/article/details/52 ...

  2. photoshop钢笔工具简单记录

    1. 移动锚点 Ctrl + 左键 2. 增加.删除锚点 左键(显示+.-) 3. 直线曲线相互转换 Alt + 左键(注意提示) 默认情况下为直线,按住Alt鼠标左键点击目标锚点,目标锚点两边的直线 ...

  3. HttpUrlConnection类基本使用

    这个类用来模拟浏览器向服务器发送请求和接收响应 注意: HttpUrlConnection对象简称huc对象 1)获取huc对象向url构造中传递url字符串,并调用openconnection方法即 ...

  4. leetcood学习笔记-107-二叉树的层次遍历二

    题目描述: 方法一: class Solution(object): def levelOrderBottom(self, root): """ :type root: ...

  5. 6361. 【NOIP2019模拟2019.9.18】鲳数

    题目 题目大意 给你一个区间\([l,r]\),求这个区间内每个整数的十进制上从高位到低位的逆序对个数之和. 思考历程 一开始就知道这是个数位DP-- 结果一直都没有调出来,心态崩了-- 正解 先讲讲 ...

  6. Change myself to be better

    发现和改变自己不好的习惯 遇到问题的反应 自己遇到问题的时候,特别是不熟悉的问题的时候就会有点焦虑,这个应该是每个人都会有的,遇到自己不熟悉的或者是没有经历过的东西都会 感觉不舒服,或者是遇到难题或者 ...

  7. Android NDK Downloads

    https://developer.android.google.cn/ndk/downloads/index.html

  8. SQL优化之慢查询和explain以及性能分析

    性能优化的思路 首先需要使用慢查询功能,去获取所有查询时间比较长的SQL语句 使用explain去查看该sql的执行计划 使用show profile去查看该sql执行时的性能问题 MySQL性能优化 ...

  9. composer(作曲家)安装php-ml

    刚开始我用的是up5.6版本php命令安装composer 后来使用composer时发现命令行会提示php版本太低 于是我下载了wamp,使用7.1版本的php重新安装了composer,因为php ...

  10. bzoj1051题解

    [题意分析] 给你一张有向图,求有多少个点,满足以其他任意一点为起点都能到达该点. [解题思路] 如果这张有向图不连通,则一定没有点能被其他所有点到达,答案为0. 然后先用tarjan缩一波强连通分量 ...