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 <stdio.h>
#include <algorithm>
#include <set>
#include <string.h>
#include <vector>
#include <math.h>
#include <queue>
#include <iostream>
#include <string>
using namespace std;
const int maxn = ;
const int inf = ;
int n,m;
int vis[maxn];
int d[maxn],c[maxn],pre[maxn],d2[maxn],c2[maxn];
int g[maxn][maxn],co[maxn][maxn];
void dijkstra(int st){
fill(d,d+maxn,inf);
fill(c,c+maxn,inf);
fill(vis,vis+maxn,false);
fill(pre,pre+maxn,-);
d[st]=;
c[st]=;
for(int i=;i<n;i++){
int u=-,min=inf;
for(int j=;j<n;j++){
if(vis[j]==false && d[j]<min){
min=d[j];
u=j;
}
}
if(u==-) return;
vis[u]=true;
for(int v=;v<n;v++){
if(vis[v]==false && g[u][v]!=inf){
if(d[v]>d[u]+g[u][v]){
d[v]=d[u]+g[u][v];
c[v]=c[u]+co[u][v];
pre[v]=u;
}
else if(d[v]==d[u]+g[u][v] && c[v]>c[u]+co[u][v]){
c[v]=c[u]+co[u][v];
pre[v]=u;
}
}
}
}
}
void dijkstra2(int st){
fill(d2,d2+maxn,inf);
fill(c2,c2+maxn,inf);
fill(vis,vis+maxn,false);
fill(pre,pre+maxn,-);
d2[st]=;
c2[st]=;
for(int i=;i<n;i++){
int u=-,min=inf;
for(int j=;j<n;j++){
if(vis[j]==false && c2[j]<min){
min=c2[j];
u=j;
}
}
if(u==-) return;
vis[u]=true;
for(int v=;v<n;v++){
if(vis[v]==false && co[u][v]!=inf){
if(c2[v]>c2[u]+co[u][v]){
c2[v]=c2[u]+co[u][v];
d2[v]=d2[u]+;
pre[v]=u;
}
else if(c2[v]==c2[u]+co[u][v] && d2[v]>d2[u]+){
d2[v]=d2[u]+;
pre[v]=u;
}
}
}
}
}
vector<int> v1,v2;
void dfs(vector<int> &v,int st,int ed){
v.push_back(ed);
if(st==ed)return;
dfs(v,st,pre[ed]);
}
bool issame(vector<int> v1,vector<int> v2){
if(v1.size()!=v2.size())return false;
for(int i=;i<v1.size()&&i<v2.size();i++){
if(v1[i]!=v2[i]) return false;
}
return true;
}
int main(){
scanf("%d %d",&n,&m);
fill(g[],g[]+maxn*maxn,inf);
fill(co[],co[]+maxn*maxn,inf);
for(int i=;i<m;i++){
int v1,v2,way,len,time;
scanf("%d %d %d %d %d",&v1,&v2,&way,&len,&time);
g[v1][v2]=len;
co[v1][v2]=time;
if(way==){
g[v2][v1]=len;
co[v2][v1]=time;
}
}
int st,ed;
scanf("%d %d",&st,&ed);
dijkstra(st);
dfs(v1,st,ed);
dijkstra2(st);
dfs(v2,st,ed);
if(issame(v1,v2)){
printf("Distance = %d; Time = %d: %d",d[ed],c2[ed],v1[v1.size()-]);
for(int i=v1.size()-;i>=;i--){
printf(" -> %d",v1[i]);
}
return ;
}
printf("Distance = %d: %d",d[ed],v1[v1.size()-]);
for(int i=v1.size()-;i>=;i--){
printf(" -> %d",v1[i]);
}
printf("\n");
printf("Time = %d: %d",c2[ed],v2[v2.size()-]);
for(int i=v2.size()-;i>=;i--){
printf(" -> %d",v2[i]);
}
printf("\n");
}

注意点:比较简单的最短路径题,就是算两个最小路径,都是有第二标尺的,再根据要求输出。又是题目没看清就开始做,导致一直答案错误,还有复制函数过来,总会落下一些地方没改,如果可以还是再敲一遍为好。

PAT A1111 Online Map (30 分)——最短路径,dijkstra的更多相关文章

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

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

  2. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  3. [PAT] 1147 Heaps(30 分)

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  4. PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)

    1147 Heaps (30 分)   In computer science, a heap is a specialized tree-based data structure that sati ...

  5. PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

    1057 Stack (30 分)   Stack is one of the most fundamental data structures, which is based on the prin ...

  6. 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)

    题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...

  7. 【PAT甲级】1072 Gas Station (30 分)(Dijkstra)

    题意: 输入四个正整数N,M,K,D(N<=1000,M<=10,K<=10000)分别表示房屋个数,加油站个数,路径条数和加油站最远服务距离,接着输入K行每行包括一条路的两条边和距 ...

  8. 1030 Travel Plan (30分)(dijkstra 具有多种决定因素)

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

  9. PAT 1131. Subway Map (30)

    最短路. 记录一下到某个点,最后是哪辆车乘到的最短距离.换乘次数以及从哪个位置推过来的,可以开$map$记录一下. #include<map> #include<set> #i ...

随机推荐

  1. 加快maven中jar包的下载速度,maven镜像收集

    maven下载jar包的默认仓库是http://my.repository.com/repo/path速度较慢,通过配置国内镜像提高下载速度 1.打开eclipse--->Window---&g ...

  2. Java开发中json使用,各对象与json相互转换

    Json:一种网络通信使用的数据格式,因为便于解析,比较流行,对象可以转为json,同样json也可以转对象. 下面介绍下Json工具的简单使用(fastjson && jackson ...

  3. matlab的解方程的例子

    syms x y z=exp(2*x+y)+cos(3*x*y)-exp(1)-1; zz=subs(z,x,1) solve(zz)

  4. Tests for Variances

    In each case, we'll illustrate how to perform the hypothesis tests of this lesson using summarized d ...

  5. Python全栈学习_day001作业

    Day1作业及默写 1.简述变量命名规范 1. 必须以字母.数字.下划线命名,且不能以数字开头 2. 不能是python的关键字 3. 不能以中文或者拼音作为变量名 4. 命名格式推荐以驼峰式或者下划 ...

  6. c语言学习笔记-switch

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 一.switch函数意义 选择性执行 二.switch函数结构 switch(表达式) { case: 常量表达式; 语句1; ...

  7. 前端学习 之 Bootstrap (一)

    中文文档 一.前言 1.简介 Bootsrtap作为一个前端框架,开箱即用,封装了前段开发的大量底层细节,开放灵活,对响应式设计网页很好支持,组件丰富,社区活跃. 特点: 可重用性 一致性 灵活的栅栏 ...

  8. 自定义控件详解(三):Canvas效果变换

    Canvas 画布 从前面我们已经知道了 Canvas 类可以绘出 各种形状. 这里学习一下Canvas 类的变换效果(平移,旋转等) 首先需要了解一下Canvas 画布, 我们用Canvas.Dra ...

  9. Android项目实战(三十二):圆角对话框Dialog

    前言: 项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框. 对话框包括:1.圆角 2.app图标 , 提示文本,关闭对话框的"确定"按钮 难点:1.对话框边框圆角 ...

  10. Kotlin入门(19)Android的基础布局

    线性布局线性布局LinearLayout是最常用的布局,顾名思义,它下面的子视图像是用一根线串了起来,所以其内部视图的排列是有顺序的,要么从上到下垂直排列,要么从左到右水平排列.排列顺序只能指定一维方 ...