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<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int INF = ;
int GL[][], GT[][];
int dst[], visit[];
vector<int> pre1[], pre2[];
int N, M, sour, destn;
void dijkstra(int G[][], vector<int> pre[], int s){
fill(dst, dst + , INF);
fill(visit, visit + , );
dst[s] = ;
for(int i = ; i < N; i++){
int u = -, minLen = INF;
for(int j = ; j < N; j++){
if(dst[j] < minLen && visit[j] == ){
minLen = dst[j];
u = j;
}
}
if(u == -){
return;
}
visit[u] = ;
for(int j = ; j < N; j++){
if(visit[j] == && G[u][j] != INF){
if(G[u][j] + dst[u] < dst[j]){
dst[j] = G[u][j] + dst[u];
pre[j].clear();
pre[j].push_back(u);
}else if(G[u][j] + dst[u] == dst[j]){
pre[j].push_back(u);
}
}
}
}
}
int minTime = INF, minLen = INF;
vector<int> ans1, ans2, temp1, temp2;
void DFS1(int d){ //函数前初始化minTime、minLen
temp1.push_back(d);
if(d == sour){
int tempL = , tempT = ;
for(int i = temp1.size() - ; i > ; i--){
tempL += GL[temp1[i]][temp1[i - ]];
tempT += GT[temp1[i]][temp1[i - ]];
}
if(tempL < minLen){
minLen = tempL;
minTime = tempT;
ans1 = temp1;
}else if(tempL == minLen && tempT < minTime){
minLen = tempL;
minTime = tempT;
ans1 = temp1;
}
} for(int i = ; i < pre1[d].size(); i++){
DFS1(pre1[d][i]);
}
temp1.pop_back();
}
void DFS2(int d){
temp2.push_back(d);
if(d == sour){
int tempL = , tempT = ;
for(int i = temp2.size() - ; i > ; i--){
tempL += GL[temp2[i]][temp2[i - ]];
tempT += GT[temp2[i]][temp2[i - ]];
}
if(tempT < minTime){
ans2 = temp2;
minTime = tempT;
minLen = tempL;
}else if(tempT == minTime && temp2.size() < ans2.size()){
ans2 = temp2;
minTime = tempT;
minLen = tempL;
}
}
for(int i = ; i < pre2[d].size(); i++){
DFS2(pre2[d][i]);
}
temp2.pop_back();
}
int main(){
scanf("%d%d", &N, &M);
fill(GL[], GL[] + *, INF);
fill(GT[], GT[] + *, INF);
for(int i = ; i < M; i++){
int tag, v1, v2, L, T;
scanf("%d%d%d%d%d", &v1, &v2, &tag, &L, &T);
if(tag == ){
GL[v1][v2] = L;
GT[v1][v2] = T;
}else{
GL[v1][v2] = GL[v2][v1] = L;
GT[v1][v2] = GT[v2][v1] = T;
}
}
scanf("%d%d", &sour, &destn);
dijkstra(GL, pre1, sour);
dijkstra(GT, pre2, sour);
minTime = INF; minLen = INF;
DFS1(destn);
int prtLen = minLen;
minTime = INF; minLen = INF;
DFS2(destn);
int prtTim = minTime;
if(ans1 == ans2){
int LL = ans1.size();
printf("Distance = %d; Time = %d: %d", prtLen, prtTim, ans1[LL - ]);
for(int i = LL - ; i >= ; i--){
printf(" -> %d", ans1[i]);
}
}else{
int LL1 = ans1.size(), LL2 = ans2.size();
printf("Distance = %d: %d", prtLen, ans1[LL1 - ]);
for(int i = LL1 - ; i >= ; i--){
printf(" -> %d", ans1[i]);
}
printf("\n");
printf("Time = %d: %d", prtTim, ans2[LL2 - ]);
for(int i = LL2 - ; i >= ; i--){
printf(" -> %d", ans2[i]);
}
}
cin >> N;
return ;
}

总结:

1、题意:用路程作为边权,求最短路径,如果有多条则输出耗时最短的。 再用时间作为边权求最短路,如果有多条则输出经过节点个数最少的。使用两次迪杰斯特拉和DFS即可。最开始没读清题,以为以时间为边权求最短路,如果有多条则选择路程最短的,结果测试点2过不去。

2、注意one-way是单行道标志,为1表示只有v1到v2的路,为0表示v1到v2和v2到v1都是通的。注意有些是单向路径,有些是双向。另外,由于DFS回溯时得到的路径是倒着的、有些路是单向的,所以在累加边权时也是倒序的,是 GL[temp1[i]][temp1[i - 1]]  而非 GL[temp1[i - 1]][temp1[ i ]]。

A1111. Online Map的更多相关文章

  1. PAT A1111 Online Map (30 分)——最短路径,dijkstra

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...

  2. PAT甲级——A1111 Online Map【30】

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...

  3. 【刷题-PAT】A1111 Online Map (30 分)

    1111 Online Map (30 分) Input our current position and a destination, an online map can recommend sev ...

  4. PAT_A1111#Online Map

    Source: PAT A1111 Online Map (30 分) Description: Input our current position and a destination, an on ...

  5. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  6. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  7. 1111 Online Map (30 分)

    1111 Online Map (30 分) Input our current position and a destination, an online map can recommend sev ...

  8. mapreduce中一个map多个输入路径

    package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...

  9. .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法

    .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...

随机推荐

  1. java中级——二叉树比较冒泡和选择排序

    上次我们说到二叉树排序比较,给出如下的题目 题目:创建五万个随机数,然后用分别用冒泡法,选择法,二叉树3种排序算法进行排序,比较哪种更快 废话不说直接上源码,可以看控制台结果 注意的是 需要我们需要上 ...

  2. python(Django之Logging、API认证)

    一.Loging模块 用于方便的记录日志的模块 import logging logging.basicConfig(filename='log.log', format='%(asctime)s - ...

  3. python之路--while, 格式化输出, 编码

    一 . while循环 while 条件: 循环体(break, continue) while True: content = input('你想对我说什么:,输入你最帅退出') if conten ...

  4. MySQL中and和or的优先级的问题

    参考资料: 关于MySQL中AND和OR的优先级的问题 SQL语句中的AND和OR执行顺序问题 MySQL带OR关键字的多条件查询

  5. Javascript 实现复制(Copy)动作方法大全

    一.实现点击按钮,复制文本框中的的内容 <script type="text/javascript"> function copyUrl2() { var Url2=d ...

  6. Razor Pages with ASP.NET Core 2

    With ASP.NET Core 2 we get another way of building web applications. It’s one of those new things th ...

  7. How the Microsoft Bot Framework Changed Where My Friends and I Eat: Part 1

    Bots are everywhere nowadays, and we interact with them all of the time. From interactions on our ph ...

  8. Jquery中val方法使用的坑

    Jquery中val方法使用 val()// 取得第一个匹配元素的当前值 val(val)// 设置所有匹配元素的值 val([val1, val2])// 设置多选的checkbox.多选selec ...

  9. python史上最全学习路线图

    ps:盘它 python入门教程 关注微信公众号,回复"python入门"获取视频下载地址

  10. Git——快速安装Git及初始化配置【二】

    文档 https://git-scm.com/book/zh/v2 下载 mac https://git-scm.com/download/mac Linux https://git-scm.com/ ...