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 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】的更多相关文章
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1111. Online Map
PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...
- PAT甲级——1111 Online Map (单源最短路经的Dijkstra算法、priority_queue的使用)
本文章同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90041078 1111 Online Map (30 分) ...
- PAT甲级——1131 Subway Map (30 分)
可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30 ...
- PAT甲级——A1131 Subway Map【30】
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- 【刷题-PAT】A1111 Online Map (30 分)
1111 Online Map (30 分) Input our current position and a destination, an online map can recommend sev ...
- pat 甲级 1022. Digital Library (30)
1022. Digital Library (30) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Di ...
- pat 甲级 1049. Counting Ones (30)
1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ...
- pat 甲级 1072. Gas Station (30)
1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...
随机推荐
- Batch - 重定向符号Redirection >
总结 Don't use a piping operator, which is what ">" Redirection is. 不要使用管道运算符,即“>”. Di ...
- SQL SERVER中[dbo]的解释
1.作用: (1)DBO是每个数据库的默认用户,具有所有者权限,即DbOwner:通过用DBO作为所有者来定义对象,能够使数据库中的任何用户引用而不必提供所有者名称.(2)至于为什么要使用所有者进行限 ...
- LeetCode 707. Design Linked List (设计链表)
题目标签:Linked List 题目让我们自己设计一个 linked list,可以是单向和双向的.这里选的是单向,题目并不是很难,但要考虑到所有的情况,具体看code. Java Solution ...
- hexo中加入点击出现小红心的特效会导致无法双击选中和连续点击三次选中一整行的操作
文章目录 问题描述 解决 个人博客:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io 问题描述 如题,我们 ...
- 《Java语言程序设计》编程练习6.18(检测密码)
6.18 (检测密码)一些网站对于密码具有一些规则.编写一个方法,检测字符串是否是一个有效密码. 假定密码规则如下: • 密码必须至少8位字符. • 密码仅能包含字母和数字. ...
- JAVA利用JXL导出 EXCEL (在原有的excel模板上把数据导到excel上)
添加依赖 <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>j ...
- 3.4_springboot2.x整合spring Data Elasticsearch
Spring Data Elasticsearch 是spring data对elasticsearch进行的封装. 这里有两种方式操作elasticsearch: 1.使用Elasticsearch ...
- 【CF516D】Drazil and Morning Exercise
题目 首先我们知道,在树上距离一个点最远的点一定是直径的两个端点之一 首先两遍\(\rm dfs\)把直径求出来,定义\(d(u)\)表示点\(u\)距离其最远点的距离,有了直径我们就能求出\(d\) ...
- USACO 2008 January Silver Telephone Lines /// 二分最短路 邻接表dijkstra oj22924
题目大意: 一共有N (1 ≤ N ≤ 1,000)个电线杆,有P P (1 ≤ P ≤ 10,000)对电线杆是可以连接的, 用几条线连接在一起的电线杆之间都可相互通信,现在想要使得电线杆1和电线杆 ...
- java 冒泡排序法、选择排序
1.冒泡排序 /* * 冒泡排序 * 外层控制循环多少趟,内层控制每一趟的循环次数 */ public class Test08 { public static void main(String[] ...