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. php 生成订单号201807205598981

    php版 /** * 生成唯一订单号 */ public function build_order_no() { $no = date('Ymd').substr(implode(NULL, arra ...

  2. composer 下载包慢的解决方法

    方法一: 修改 composer 的全局配置文件(推荐方式) 打开命令行窗口(windows用户)或控制台(Linux.Mac 用户)并执行如下命令: composer config -g repo. ...

  3. app自动化测试Appium+python

    一.node.js安装 https://nodejs.org/en/download/ ##一直下一步 ###cmd查看 二.  .NET Framework安装 https://www.micros ...

  4. How to hosts

    sudo cp /etc/hosts /etc/hosts.bak sudo cp ~/Desktop/hosts /etc/hosts sudo systemctl restart NetworkM ...

  5. 3.docker基础架构

    docker是一个典型的c/s架构产品. dockerd :为客户端提供  RESTFUL API,响应来自客户端的请求, 采用模块化的架构, 通过专门的 Engine 模块来分发管理各 个来自客户端 ...

  6. c++ string替换指定字符串

    string fnd = "dataset"; string rep = "labels"; string buf = "d:/data/datase ...

  7. 因为网络安全的重要性打算学习linux

    互联网的普及,在给人们带来巨大便利的同时,也让人们感受到网络安全隐患带给人们的不安与威胁.尤其是随着计算机技术和网络技术应用范围的不断扩充,网络安全方面存在的漏洞也越来越多,在这种情况下,如何提高网络 ...

  8. shiro注解和标签

    Controller中注解: @RequiresAuthentication @RequiresGuest @RequiresPermissions("account:create" ...

  9. kubernetes 基本命令

    查询命令: kubectl get pods -n kube-system kubectl get ClusterRole -n kube-system kubectl get ClusterRole ...

  10. AIM Tech Round 4 Div. 1

    A:显然最优方案是对所形成的置换的每个循环排个序. #include<iostream> #include<cstdio> #include<cmath> #inc ...