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
 #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = , inf = 0x7fffffff;
struct Arc{
int v;
int tim, len;
};
vector<Arc> arc[maxn];
int N, M, dis[maxn], tim[maxn], pre[maxn], pathLen[maxn], S, T;
vector<int> disPath, timPath, tempPath; void scan(){
scanf("%d%d", &N, &M);
for(int i = ; i < M; ++ i){
int v1, v2, oneWay;
Arc t;
scanf("%d%d%d%d%d", &v1, &v2, &oneWay, &t.len, &t.tim);
t.v = v2; arc[v1].push_back(t);
if(!oneWay) t.v = v1, arc[v2].push_back(t);
}
scanf("%d%d", &S, &T);
} void dijkstra_dis(int s){
bool vis[maxn];
fill(vis, vis+maxn, false);
fill(dis, dis+maxn, inf);
fill(tim, tim+maxn, inf);
dis[s] = tim[s] = ;
for(int i = ; i < N; ++ i){
int u = -, minDis = inf;
for(int j = ; j < N; ++ j){
if(!vis[j] && dis[j] < minDis){
minDis = dis[j];
u = j;
}
}
if(u == -) return;
vis[u] = true;
for(int k = ; k < arc[u].size(); ++ k){
int v = arc[u][k].v;
if(!vis[v]){
if(dis[u] + arc[u][k].len < dis[v]){
dis[v] = dis[u] + arc[u][k].len;
tim[v] = tim[u] + arc[u][k].tim;
pre[v] = u;
}else if(dis[u] + arc[u][k].len == dis[v] && tim[u] + arc[u][k].tim < tim[v]){
tim[v] = tim[u] + arc[u][k].tim;
pre[v] = u;
}
}
}
}
} void dijkstra_tim(int s){
bool vis[maxn];
fill(vis, vis+maxn, false);
fill(tim, tim+maxn, inf);
fill(pathLen, pathLen+maxn, inf);
tim[s] = , pathLen[s] = ;
for(int i = ; i < N; i ++){
int u = -, minTim = inf;
for(int j = ; j < N; j ++){
if(!vis[j] && tim[j] < minTim){
minTim = tim[j];
u = j;
}
}
if(u == -) return;
vis[u] = true;
for(int k = ; k < arc[u].size(); k ++){
int v = arc[u][k].v;
if(!vis[v]){
if(tim[u] + arc[u][k].tim < tim[v]){
tim[v] = tim[u] + arc[u][k].tim;
pre[v] = u;
pathLen[v] = pathLen[u] + ;
}else if(tim[u] + arc[u][k].tim == tim[v] && pathLen[u]+ < pathLen[v]){
pre[v] = u;
pathLen[v] = pathLen[u] + ;
}
}
}
}
} void dfs(int t){
tempPath.push_back(t);
if(pre[t] != -){
dfs(pre[t]);
}
} void printPath(vector<int> &path){
for(int i = path.size()-; i >= ; -- i){
printf("%d", path[i]);
if(i != ) printf(" -> ");
}
printf("\n");
} int main()
{
fill(pre, pre+maxn, -);
scan();
dijkstra_dis(S);
dfs(T);
disPath = tempPath;
fill(pre, pre+maxn, -);
dijkstra_tim(S);
tempPath.clear();//clear data
dfs(T);
timPath = tempPath;
if(timPath == disPath){
printf("Distance = %d; Time = %d: ", dis[T], tim[T]);
printPath(disPath);
}else{
printf("Distance = %d: ", dis[T]); printPath(disPath);
printf("Time = %d: ", tim[T]); printPath(timPath);
}
return ;
}

1111. Online Map (30)的更多相关文章

  1. PAT (Advanced Level) 1111. Online Map (30)

    预处理出最短路再进行暴力dfs求答案会比较好.直接dfs效率太低. #include<cstdio> #include<cstring> #include<cmath&g ...

  2. 1111 Online Map (30)(30 分)

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...

  3. PAT Advanced 1111 Online Map (30) [Dijkstra算法 + DFS]

    题目 Input our current position and a destination, an online map can recommend several paths. Now your ...

  4. PAT甲题题解-1111. Online Map (30)-PAT甲级真题(模板题,两次Dijkstra,同时记下最短路径)

    题意:给了图,以及s和t,让你求s到t花费的最短路程.最短时间,以及输出对应的路径.   对于最短路程,如果路程一样,输出时间最少的. 对于最短时间,如果时间一样,输出节点数最少的.   如果最短路程 ...

  5. 【PAT甲级】1111 Online Map (30分)(dijkstra+路径记录)

    题意: 输入两个正整数N和M(N<=500,M<=N^2),分别代表点数和边数.接着输入M行每行包括一条边的两个结点(0~N-1),这条路的长度和通过这条路所需要的时间.接着输入两个整数表 ...

  6. 1111 Online Map (30 分)

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

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

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

  8. 1111 Online Map (30 分)

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

  9. PAT 1111 Online Map[Dijkstra][dfs]

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

随机推荐

  1. Python学习随笔(1)--可视化工具plotly使用

    把数据库某列数据取出来,然后再在本地生成html文件形成可视化视图显示 #!/usr/bin/python# coding=utf-8 import pymysqlimport plotly.plot ...

  2. STS 安装SVN插件

    1:STS中 Help->Eclipse MarketPlace 搜索svn点击go安装svn插件,然后重启STS. 2:如果Team中出现SVN说明安装好了一半. 3: 手动安装SVN Con ...

  3. ESLint具体规则设置

    "no-alert": 0,//禁止使用alert confirm prompt "no-array-constructor": 2,//禁止使用数组构造器 & ...

  4. docker镜像无法下载或者下载缓慢

    解决docker镜像无法下载的问题 2015年10月02日 16:01:05 阅读数:20776 克服跨洋网络延迟,使用Docker Hub Mirror加速Docker官方镜像下载 http://c ...

  5. Object备忘录

    1.Object.assign(target,...source) 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象. 2.Object.create()方法创建一个新对 ...

  6. Idea书签管理器的使用

    1. 添加书签 以光标所在的行,为落点, 方式一: F11 , 添加一个默认的书签 方式二:Crtl + Shift + 数字 , 添加一个带编号 的书签 2. 查看书签 方式一:Shift + F1 ...

  7. 写给自己的小CASE

    一时的完美不代表什么,重要的是不断地学习和成长:直面挑战,不断进取.   摘自<看见成长的自己>.

  8. [ArcGIS]ArcGIS Server环境搭建,发布服务,以及使用ArcGIS API for JavaScript

    环境搭建 安装Web服务器 IIS 控制面板-程序-程序和功能-启用或关闭Windows功能,勾选以下 安装VisualStudio,选择包括ASP.NET模块 安装ArcGIS服务器 ArcGIS ...

  9. java实验环境搭建,eclise下载与使用

    一.1.官方下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载后安装,默认安装即可. 2.在 wind ...

  10. unity 动态更新模型透明度

    RaycastHit[] hits; Vector3 normal = transform.position - target.position; hits = Physics.RaycastAll( ...