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. 第一个SpringBoot程序

    第一个SpringBoot程序 例子来自慕课网廖师兄的免费课程 2小时学会SpringBoot Spring Boot进阶之Web进阶 使用IDEA新建工程,选择Spring Initializr,勾 ...

  2. 【Java深入研究】6、CGLib动态代理机制详解

    一.首先说一下JDK中的动态代理: JDK中的动态代理是通过反射类Proxy以及InvocationHandler回调接口实现的 但是,JDK中所要进行动态代理的类必须要实现一个接口,也就是说只能对该 ...

  3. Matlab diag的用法

    X = diag(v,k) 以向量v的元素作为矩阵X的第k条对角线元素,当k=0时,v为X的主对角线:当k>0时,v为上方第k条对角线 几个例子: 当k> v=[1 2 3]; >& ...

  4. linux shell脚本之-变量极速入门与进阶(1)

    1,如果创建shell脚本? 使用任意文本编辑软件,一般为vim,创建.sh结尾的文件,在文件的最开头用 #!/bin/bash 注明shell的类型 如: ghostwu@dev:~/linux/s ...

  5. JavaScript的值传递和引用传递

    译者按: 程序员应该知道递归,但是你真的知道是怎么回事么? 原文: All About Recursion, PTC, TCO and STC in JavaScript 译者: Fundebug 为 ...

  6. 简单实用的jQuery分页插件

    在做商城和订单管理的时候,常常会用到分页功能,所以我封装了一个jQuery的分页插件,该插件主要实现上下翻页,输入数字跳转等功能. 具体实现如下: 输入参数需要当前页码pageNo,总页码totalP ...

  7. VUE CLI 3.0 安装及创建项目

    一.安装 VUE CLI 3.0 官网: https://cli.vuejs.org/   详细资料可以自己先把官网过一遍. 1. 安装(默认你的电脑上已安装node及npm) npm install ...

  8. ASP.NET MVC WebAPI 资源整理

    注:这是收集给公司同事学习的资料,入门级别的. 使用ASP.Net WebAPI构建REST服务(一)——简单的示例 http://blog.csdn.net/mengzhengjie/article ...

  9. Chrome调试本地文件无法使用window.opener对象进行窗口间值传递

    今天在百度BAE上建了个应用,svn上传后发现页面间互调有些问题,几经查询发现: (1)IE下正常的window.opener.object1.object2页面间对象访问方法在Chrome下不能使用 ...

  10. 数据库开源框架GreenDao的使用解析

    数据库开源框架GreenDao的使用解析 1,GreenDao概述 1),greenDao是一个当下十分火热的数据库开源框架,或者说是一个帮助Android开发者将数据存到SQLite中的一个开源项目 ...