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. 【Spring】18、springMVC对异常处理的支持

    无论做什么项目,进行异常处理都是非常有必要的,而且你不能把一些只有程序员才能看懂的错误代码抛给用户去看,所以这时候进行统一的异常处理,展现一个比较友好的错误页面就显得很有必要了.跟其他MVC框架一样, ...

  2. One Person Game(zoj3593+扩展欧几里德)

    One Person Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status ...

  3. 前端面试(原生js篇) - 精确运算

    一.面试题 问:开发的时候有用到过 Math 吗? 答:很多啊.比如生成 GUID 的时候,就会用到 Math.random() 来生成随机数. 问:别的呢?比如向下取整.向上取整? 答:向下取整是  ...

  4. 【读书笔记】iOS-OCUnit-单元测试

    一,新建立一个hello工程--->在左侧会看到helloTests---->helloTests.m.如下图所示. 二,打开查看会看到如下代码. #import <UIKit/UI ...

  5. 《Inside C#》笔记(五) 方法

    方法用来体现类的行为. 一 相关概念 a) ref和out 通常一个方法只能返回一个值,但如果确实需要返回多个值时,可以使用ref或out.如果方法的参数被标注为ref或out,在调用该方法时,会传递 ...

  6. 后台返回xml格式转json

    之前后台做了一个xml格式的数据返回给前端,这个可愁坏了我,不过现在还是解决了,虽然方法有点笨,但没有找到其他的方法,先将就着用吧. 后台返回的是这样的: 那么我们就要这样处理:commonMetho ...

  7. 腾讯云部署golang flow流程,vue.js+nginx+mysql+node.js

    这次总算把js-ojus/flow的ui部署到腾讯云上,比较吐槽的就是,为啥这么复杂,vue.js前后端分离,比golang编写的部署方面复杂几万倍.真是浪费人生啊. golang+sqlite写的东 ...

  8. (网页)angularjs中的验证input输入框只能输入数字和小数点

    百度的资料:自己记录看下 把js的验证方法改成angular可使用的方法 AngularJS文件的写法: $scope.clearNoNum = function(obj,attr){ //先把非数字 ...

  9. 将你的 Virtual dom 渲染成 Canvas

    项目概述 一个基于Vue的virtual dom插件库,按照Vue render 函数的写法,直接将Vue生成的Vnode渲染到canvas中.支持常规的滚动操作和一些基础的元素事件绑定. githu ...

  10. 一文学redis操作(记录向)

    相关内容: 虽然有参考文档,而且记忆太多也是耗脑,但学习的时候还是想要有个系统划分开知识点的文档,即使不要求去细致记忆,但划分开知识块后脑子里的印象才会更清晰,所以就有了这个博文. 主要是将各种命令进 ...