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. Codeigniter设置时区

    Codeigniter 3.x,在application/config/config.php 末尾加上 date_default_timezone_set("Asia/Shanghai&qu ...

  2. Laravel 开发环境搭建

    本人使用的是Laravel5.5版本,需要PHP7支持,所以安装的环境是Apache2.php7.0.mysql5.7,系统为ubuntu14.04LTS(14以下的版本对php7支持不够),主要参考 ...

  3. CSS盒模型及应用

    其实,CSS就三个大模块: 盒子模型 . 浮动 . 定位,其余的都是细节.要求这三部分,无论如何也要学的非常精通. 所谓盒子模型就是把HTML页面中的元素看作是一个矩形的盒子,也就是一个盛装内容的容器 ...

  4. 分布式项目controller项目中web.xml配置文件的编写

    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " ...

  5. javascript追加节点

    追加节点 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  6. Nginx被动健康检查和主动健康检查

    1.被动健康检查 Nginx自带有健康检查模块:ngx_http_upstream_module,可以做到基本的健康检查,配置如下: upstream cluster{ server max_fail ...

  7. Prometheus监控node-exporter常用指标含义

    一.说明 最近使用Prometheus新搭建监控系统时候发现内存采集时centos6和centos7下内存监控指标采集计算公式不相同,最后采用统一计算方法并整理计算公式如下: 1 100-(node_ ...

  8. NOIp2018集训test-9-7(pm) (联考一day1)

    又被辉神吊打了.今天不仅被辉神李巨吊打,还给基本上给全班垫底了. 看到T3就知道是十进制快速幂,全机房考试的当时应该就我会,结果我tm没找到递推. Orz lyc BM直接水过,Orz wys六个fo ...

  9. NX二次开发-UFUN设置视图边界线显示隐藏UF_DRAW_set_border_display

    #include <uf.h> #include <uf_draw.h> #include <uf_drf.h> #include <uf_obj.h> ...

  10. [转]gulp打包工具总结

    与grunt类似,gulp也是构建工具,但相比于grunt的频繁IO操作,gulp的流操作能更快更便捷地完成构建工作.gulp借鉴了Unix操作系统的管道(pipe)思想,前一级的输出,直接变成后一级 ...