A1111. Online Map
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的更多相关文章
- PAT A1111 Online Map (30 分)——最短路径,dijkstra
Input our current position and a destination, an online map can recommend several paths. Now your jo ...
- PAT甲级——A1111 Online Map【30】
Input our current position and a destination, an online map can recommend several paths. Now your jo ...
- 【刷题-PAT】A1111 Online Map (30 分)
1111 Online Map (30 分) Input our current position and a destination, an online map can recommend sev ...
- PAT_A1111#Online Map
Source: PAT A1111 Online Map (30 分) Description: Input our current position and a destination, an on ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- 1111 Online Map (30 分)
1111 Online Map (30 分) Input our current position and a destination, an online map can recommend sev ...
- mapreduce中一个map多个输入路径
package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
随机推荐
- .Net批量插入数据
1. 一般我们普通数据插入是这样的: 现在我们写一个控制台程序用常规办法添加10000条数据. //以下是批量插入数据的办法 //连接字符串 string str = "Server=.;D ...
- python爬虫之scrapy安装(一)
简介: Scrapy,Python开发的一个快速.高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据挖掘.监测和自动化测试. Scrap ...
- python之路--进程内容补充
一. 进程的其他方法 进程id, 进程名字, 查看进程是否活着(is_alive()), terminate()发送结束进程的信号 import time import os from multipr ...
- JMeter 连接 sql server
1.安装驱动 http://www.microsoft.com/zh-CN/download/details.aspx?id=11774 下载后解压后复制sqljdbc.jar到 “jmeter的安装 ...
- Delphi之TComponent类
TComponent类 TComponent类直接由TPersistent派生.TComponent的独特特征是它的属性能够在设计期间通过ObjectInspector来控制,能够拥有其他组件.非可视 ...
- [转]Java 的强引用、弱引用、软引用、虚引用
1.强引用(StrongReference) 强引用是使用最普遍的引用.如果一个对象具有强引用,那垃圾回收器绝不会回收它.如下: Object o=new Object(); // 强引用 当内存空间 ...
- 规范化Normalization
一.批规范化 Batch Normalization 转自: http://blog.csdn.net/hjimce/article/details/50866313 https://zhuan ...
- The Xamarin Live Player Unpacked
It is 2017, and it is almost criminal to say that your app doesn't work on a given mobile platform. ...
- [离散时间信号处理学习笔记] 8. z逆变换
z逆变换的计算为下面的复数闭合曲线积分: $x[n] = \displaystyle{\frac{1}{2\pi j}}\oint_{C}X(z)z^{n-1}dz$ 式中$C$表示的是收敛域内的一条 ...
- 搭建Hexo博客(三)—换电脑继续写Hexo博客
Hexo和GitHub搭建博客的原理是:Hexo将source下的md文件生成静态的html页面,存放到public目录中,这一步是由命令:hexo -g完成.接下来执行hexo -d命令,就将pub ...