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. laravel 守护进程Supervisor的配置

    安装Supervisor Supervisor是Linux系统中常用的进程守护程序.如果队列进程queue:work意外关闭,它会自动重启启动队列进程.在Ubuntu安装Supervisor 非常简单 ...

  2. python之路--FTP 上传视频示例

    # 服务端 import json import socket import struct server = socket.socket() server.bind(('127.0.0.1',8001 ...

  3. 使用Guava cache构建本地缓存

    前言 最近在一个项目中需要用到本地缓存,在网上调研后,发现谷歌的Guva提供的cache模块非常的不错.简单易上手的api:灵活强大的功能,再加上谷歌这块金字招牌,让我毫不犹豫的选择了它.仅以此博客记 ...

  4. npm安裝、卸載、刪除、撤銷發佈包、更新版本信息

    利用npm安裝包: 全局安裝:npm install -g 模塊安裝 局部安裝(可以使用repuire(‘模塊名’)引用):npm install 模塊名稱 如果權限不夠,就是用管理員方式安裝. 本地 ...

  5. Tomcat配置Https环境

    windows环境下:http://blog.csdn.net/supersky07/article/details/7407523 linux环境下:http://blog.csdn.net/cuk ...

  6. 搭建Hexo博客(三)—换电脑继续写Hexo博客

    Hexo和GitHub搭建博客的原理是:Hexo将source下的md文件生成静态的html页面,存放到public目录中,这一步是由命令:hexo -g完成.接下来执行hexo -d命令,就将pub ...

  7. css背景色 透明字体不透明

    .demo{ padding: 25px; background-color: rgba(,,,0.5);/* IE9.标准浏览器.IE6和部分IE7内核的浏览器(如QQ浏览器)会读懂 */ }

  8. fpm 打包工具安装调试

    https://github.com/jordansissel/fpm  官方git yum install ruby-devel gcc make rpm-build rubygems gem so ...

  9. .net mvc 基类属性覆盖问题

    一,问题是这样的 我使用.net mvc设计架构时, 为了方便大家的获取UserInfo信息, 把UserInfo对象,放在了自定义的基类BaseController中, 二,问题出现了 我发觉多个人 ...

  10. Android ProgressDialog 简单实用

    ProgressDialog progressDialog; @SuppressLint("HandlerLeak") Handler handler1 = new Handler ...