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. Java学习笔记之——变量与数据类型、运算符

    一.变量 1.变量:变化的值 变量在代码运行期间,开辟了一块空间 .这块空间是有地址的,给这块取了个名字, 这个名字就叫做变量名,这块空间放的东西叫做变量值 2.变量的初始化: (1)先声明再赋值: ...

  2. php面向对象的接口和APP接口区别

    以下是php面向对象的接口:

  3. vue自制switch滑块

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Spring Boot MyBatis配置多种数据库

    mybatis-config.xml是支持配置多种数据库的,本文将介绍在Spring Boot中使用配置类来配置. 1. 配置application.yml # mybatis配置 mybatis: ...

  5. Handler消息处理机制详解

    之前一直只知道handler如何使用,不知道其中的工作原理,趁着新版本提测阶段比较空闲,及时做一个总结. 先看一下Google官方文档关于handler的解释: A Handler allows yo ...

  6. 国网SGCC_UAP 反编译.class文件源代码

    SGCC_UAP和eclipse操作方式差不多,对于用惯了IDEA和Android Studio的人来说非常不方便,按住Ctrl点击类名不能查看源码. 因为jar包下都是.class文件,所以需要安装 ...

  7. 安卓界面之Viewpager和Tablayout实现滑动界面

    摘要:六部实现选项卡界面 一. 在gradle文件添加以下代码: implementation 'com.android.support:design:28.0.0' 在gradle文件添加以上代码后 ...

  8. 《R数据挖掘入门》彩色插图(第9章)

    图9.5  图9.9

  9. 山西WebGIS项目总结

    有一段时间没写blog了,说实话,最近的心态一直在变化,看了一部日剧,回想了这一年所学所见,感觉生活目标变了. 做国土项目这段时间不是很忙,由于数据一直给不到位,时间拖得很久,所以在这期间也在继续学习 ...

  10. scrapy之spider模块

    scrapy中的spider的用法 : 1.scrapy命令行可以传参数给构造器 scrapy crawl myspider -a category=electronics 构造器接收传入的参数 im ...