题目

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

题意

在线地图中标注了街口和街道

求出发地到目的地距离最短路径,若最短距离路径有多条,取耗时最少的最短距离路径

求出发地到目的地耗时最少路径,若耗时最少路径有多条,取经过顶点最少的耗时最少路径

题目分析

已知图,顶点,边,边权-距离,边权-耗时

求距离最短路径,若最短距离路径有多条,取耗时最少的最短距离路径

求耗时最少路径,若耗时最少路径有多条,取经过顶点最少的耗时最少路径

解题思路

  • Dijkstra求最短路径,若最短距离路径有多条,选择其中耗时最少的路径
  • dfs回溯路径,并保存到vector dispath
  • Dijkstra求耗时最少路径,若耗时最少路径有多条,取其中经过顶点最少
  • dfs回溯路径,并保存到vector Timepath

知识点

比较两个路径是否相同,可以将路径保存于两个vector

vector<int> path1,path2;
if(path1==path2){
//路径相同
}

Code

#include <iostream>
#include <vector>
using namespace std;
const int inf=0x7fffffff;
const int maxn=510;
int n,m,e[maxn][maxn],w[maxn][maxn],st,fin;
int dis[maxn],dispre[maxn],Timepre[maxn],visit[maxn],weight[maxn],Time[maxn],NodeNum[maxn];
vector<int> dispath,Timepath,temppath;
void dfsdispath(int x) {
if(x==-1)return;
dispath.push_back(x);
dfsdispath(dispre[x]);
}
void dfsTimepath(int x) {
if(x==-1)return;
Timepath.push_back(x);
dfsTimepath(Timepre[x]);
}
int main(int argc,char * argv[]) {
scanf("%d %d",&n,&m);
int v1,v2,flag,len,time;
for(int i=0; i<m; i++) {
scanf("%d %d %d %d %d",&v1,&v2,&flag,&len,&time);
e[v1][v2]=len;
w[v1][v2]=time;
if(flag!=1) {
e[v2][v1]=len;
w[v2][v1]=time;
}
}
scanf("%d %d",&st,&fin);
// 寻找最短距离,耗时最短的路径
fill(dis,dis+n,inf);
fill(dispre,dispre+n,-1);
fill(weight,weight+n,inf);
dis[st]=0;
weight[st]=0;
for(int i=0; i<n; i++) {
int u=-1,minn=inf;
for(int j=0; j<n; j++) {
if(visit[j]==false&&dis[j]<minn) {
u=j;
minn=dis[j];
}
}
if(u==-1||u==fin)break;
visit[u]=true;
for(int v=0; v<n; v++) {
if(e[u][v]==0||visit[v]==true)continue;
if(e[u][v]+dis[u]<dis[v]) {
dis[v]=e[u][v]+dis[u];
dispre[v]=u;
weight[v]=w[u][v]+weight[u];
} else if(e[u][v]+dis[u]==dis[v]&&weight[v]>w[u][v]+weight[u]) {
// 取所有距离最短中的时间最短路径
dispre[v]=u;
weight[v]=w[u][v]+weight[u];
}
}
}
dfsdispath(fin);
fill(Time,Time+n,inf);
fill(Timepre,Timepre+n,-1);
fill(visit,visit+n,0);
fill(NodeNum,NodeNum+n,inf);
Time[st]=0;
NodeNum[st]=0;
for(int i=0; i<n; i++) {
int u=-1,minn=inf;
for(int j=0; j<n; j++) {
if(visit[j]==false&&Time[j]<minn) {
u=j;
minn=Time[j];
}
}
if(u==-1||u==fin)break;
visit[u]=true;
for(int v=0; v<n; v++) {
if(w[u][v]==0||visit[v]==true)continue;
if(w[u][v]+Time[u]<Time[v]) {
Time[v]=w[u][v]+Time[u];
Timepre[v]=u;
NodeNum[v]=1+NodeNum[u];
} else if(w[u][v]+Time[u]==Time[v]&&NodeNum[v]>1+NodeNum[u]) {
// 取所有距离最短中的时间最短路径
Timepre[v]=u;
NodeNum[v]=1+NodeNum[u];
}
}
}
dfsTimepath(fin);
printf("Distance = %d",dis[fin]);
if(dispath==Timepath) {
printf("; Time = %d: ",Time[fin]);
} else {
printf(": ");
for(int i=dispath.size()-1;i>=0;i--){
printf("%d",dispath[i]);
if(i!=0)printf(" -> ");
}
printf("\nTime = %d: ",Time[fin]);
}
for(int i=Timepath.size()-1;i>=0;i--){
printf("%d",Timepath[i]);
if(i!=0)printf(" -> ");
}
return 0;
}

PAT Advanced 1111 Online Map (30) [Dijkstra算法 + DFS]的更多相关文章

  1. PAT Advanced 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]

    题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...

  2. PAT Advanced 1072 Gas Station (30) [Dijkstra算法]

    题目 A gas station has to be built at such a location that the minimum distance between the station an ...

  3. PAT Advanced 1018 Public Bike Management (30) [Dijkstra算法 + DFS]

    题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...

  4. 1018 Public Bike Management (30) Dijkstra算法 + DFS

    题目及题解 https://blog.csdn.net/CV_Jason/article/details/81385228 迪杰斯特拉重新认识 两个核心的存储结构: int dis[n]: //记录每 ...

  5. PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]

    题目 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family mem ...

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

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

  7. PAT甲级1111. Online Map

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

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

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

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

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

随机推荐

  1. 外网如何访问 Service?【转】

    除了 Cluster 内部可以访问 Service,很多情况我们也希望应用的 Service 能够暴露给 Cluster 外部.Kubernetes 提供了多种类型的 Service,默认是 Clus ...

  2. 在GNOME开发人员的努力下,Pango 1.44即将问世

    早在5月份,Red Hat的Matthias Clasen共同制定了计划,在近年来相当陈旧的情况下,对Pango布局引擎库进行了一些改进. 这项工作将随着Pango1.44版本的发布而实现,看起来它很 ...

  3. tools.quartz.about

    官方网站,中文文档,demo,  参考零, 参考一, 参考二, 参考三, 参考四 , 参考五 ,文档下载 .

  4. spoj694--Distinct Substrings

    个人第一道后缀数组题目.对于每一个后缀suffix(i),都有len-sa[i]个前缀(也即有len-sa[i]个不同的字符串),其中与排名前一位的后缀有height[i]个共同的前缀,最后所得到的新 ...

  5. Java 定时循环运行程序

    Timer 和 ScheduledExecutorSeruvce 都能执行定时的循环任务,有函数 scheduleAtFixedRate.但是,如果任务运行时间较长,超过了一个周期时长,下一个任务就会 ...

  6. ZOJ 3802 Easy 2048 Again 状态DP

    zoj 上次的月赛题,相当牛的题目啊,根本想不到是状态压缩好吧 有个预先要知道的,即500个16相加那也是不会超过8192,即,合并最多合并到4096,只有2的12次方 所以用状态压缩表示前面有的序列 ...

  7. (转)linux shell 的here document 用法 (cat << EOF)

    什么是Here Documen: Here Document 是在Linux Shell 中的一种特殊的重定向方式,它的基本的形式如下 cmd << delimiter Here Docu ...

  8. 云时代架构阅读笔记一——Java性能优化(一)

    Java语言学习了这么长时间之后,自己对于Java编程的一些细节还是稍微有点总结,正好根据云时代架构中<Java高级开发必会的50个性能优化的细节(珍藏版)>来叙述一些我和里面的点比较相符 ...

  9. Mysql 存储过程造测试数据

    1.Mysql 存储过程造测试数据 -- 创建一个用户表 CREATE TABLE `sys_user` ( -- `id` CHAR (32) NOT NULL DEFAULT '' COMMENT ...

  10. 051-PHP求余运算

    <?php $x=10%5; //进行求余运算 $y=10%3; //进行求余运算 $z=10%6; //进行求余运算 echo $x; //输出变量x的值 echo $y; //输出变量y的值 ...