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)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
随机推荐
- Laravel5.5+ 区分前后端用户登录
Laravel 的用户认证是通过 Auth Facade 门脸实现的,手动认证可是使用 Auth::login() 或 Auth::attempt() 这两个方法实现. 以下内容纯属个人实现,也许有 ...
- centOS7防火墙关闭失败问题
CentOS7命令: 查看防火墙状态:firewall-cmd --state 关闭防火墙:systemctl stop firewalld.service 禁止开机自启:systemctl disa ...
- SQL Server 2014备份维护计划
1. 数据库 -> [管理]-> [维护计划] -> [新建维护计划](如果没有操作过可以,选择“维护计划向导”): 2. 直接点击下一步,然后填写计划名称.说 ...
- 实验吧 WEB 猫抓老鼠
人生的第一道CTF题目哇,鸡冻 其实只是学了一下HTTP抓包得到的都是什么,就开始上手胡搞了 题目名字叫猫抓老鼠,还疯狂暗示catch!catch!catch!catch!,就想到要用抓包其实我是因为 ...
- Mysql(Mariadb)数据库主从复制
Mysql主从复制的实现原理图大致如下: MySQL之间数据复制的基础是以二进制日志文件(binary log file)来实现的,一台MySQL数据库一旦启用二进制日志后,其作为master,它数据 ...
- Uncaught SyntaxError: Unexpected token export
开发过程中遇到这个错误,虽然不影响使用,但是每次浏览器控制台都会有错误输出,看起来十分不舒服,故翻阅资料发现是因为浏览器虽然支持了es6,但是不支持es6的Module直接使用,需要在script标签 ...
- 修改iptables后重启返回错误
在防火墙添加规则后我是这样改的vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ...
- [WC2018]即时战略——动态点分治(替罪羊式点分树)
题目链接: [WC2018]即时战略 题目大意:给一棵结构未知的树,初始时除1号点其他点都是黑色,1号点是白色,每次你可以询问一条起点为白色终点任意的路径,交互库会自动返回给你这条路径上与起点相邻的节 ...
- 牛客寒假算法训练1 D 欧拉(容斥)
1 #include<bits/stdc++.h> using namespace std; ; typedef long long ll; int p[maxn],a[maxn]; ll ...
- Spring01-Ioc基本使用
一. Spring简介 1. Spring介绍 Spring框架主页: Spring官网 Spring资源地址:下载地址 Spring框架,由Rod Johnson开发 Spring是一个非常活跃的开 ...