1111. Online Map (30)
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 <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = , inf = 0x7fffffff;
struct Arc{
int v;
int tim, len;
};
vector<Arc> arc[maxn];
int N, M, dis[maxn], tim[maxn], pre[maxn], pathLen[maxn], S, T;
vector<int> disPath, timPath, tempPath; void scan(){
scanf("%d%d", &N, &M);
for(int i = ; i < M; ++ i){
int v1, v2, oneWay;
Arc t;
scanf("%d%d%d%d%d", &v1, &v2, &oneWay, &t.len, &t.tim);
t.v = v2; arc[v1].push_back(t);
if(!oneWay) t.v = v1, arc[v2].push_back(t);
}
scanf("%d%d", &S, &T);
} void dijkstra_dis(int s){
bool vis[maxn];
fill(vis, vis+maxn, false);
fill(dis, dis+maxn, inf);
fill(tim, tim+maxn, inf);
dis[s] = tim[s] = ;
for(int i = ; i < N; ++ i){
int u = -, minDis = inf;
for(int j = ; j < N; ++ j){
if(!vis[j] && dis[j] < minDis){
minDis = dis[j];
u = j;
}
}
if(u == -) return;
vis[u] = true;
for(int k = ; k < arc[u].size(); ++ k){
int v = arc[u][k].v;
if(!vis[v]){
if(dis[u] + arc[u][k].len < dis[v]){
dis[v] = dis[u] + arc[u][k].len;
tim[v] = tim[u] + arc[u][k].tim;
pre[v] = u;
}else if(dis[u] + arc[u][k].len == dis[v] && tim[u] + arc[u][k].tim < tim[v]){
tim[v] = tim[u] + arc[u][k].tim;
pre[v] = u;
}
}
}
}
} void dijkstra_tim(int s){
bool vis[maxn];
fill(vis, vis+maxn, false);
fill(tim, tim+maxn, inf);
fill(pathLen, pathLen+maxn, inf);
tim[s] = , pathLen[s] = ;
for(int i = ; i < N; i ++){
int u = -, minTim = inf;
for(int j = ; j < N; j ++){
if(!vis[j] && tim[j] < minTim){
minTim = tim[j];
u = j;
}
}
if(u == -) return;
vis[u] = true;
for(int k = ; k < arc[u].size(); k ++){
int v = arc[u][k].v;
if(!vis[v]){
if(tim[u] + arc[u][k].tim < tim[v]){
tim[v] = tim[u] + arc[u][k].tim;
pre[v] = u;
pathLen[v] = pathLen[u] + ;
}else if(tim[u] + arc[u][k].tim == tim[v] && pathLen[u]+ < pathLen[v]){
pre[v] = u;
pathLen[v] = pathLen[u] + ;
}
}
}
}
} void dfs(int t){
tempPath.push_back(t);
if(pre[t] != -){
dfs(pre[t]);
}
} void printPath(vector<int> &path){
for(int i = path.size()-; i >= ; -- i){
printf("%d", path[i]);
if(i != ) printf(" -> ");
}
printf("\n");
} int main()
{
fill(pre, pre+maxn, -);
scan();
dijkstra_dis(S);
dfs(T);
disPath = tempPath;
fill(pre, pre+maxn, -);
dijkstra_tim(S);
tempPath.clear();//clear data
dfs(T);
timPath = tempPath;
if(timPath == disPath){
printf("Distance = %d; Time = %d: ", dis[T], tim[T]);
printPath(disPath);
}else{
printf("Distance = %d: ", dis[T]); printPath(disPath);
printf("Time = %d: ", tim[T]); printPath(timPath);
}
return ;
}
1111. Online Map (30)的更多相关文章
- PAT (Advanced Level) 1111. Online Map (30)
预处理出最短路再进行暴力dfs求答案会比较好.直接dfs效率太低. #include<cstdio> #include<cstring> #include<cmath&g ...
- 1111 Online Map (30)(30 分)
Input our current position and a destination, an online map can recommend several paths. Now your jo ...
- PAT Advanced 1111 Online Map (30) [Dijkstra算法 + DFS]
题目 Input our current position and a destination, an online map can recommend several paths. Now your ...
- PAT甲题题解-1111. Online Map (30)-PAT甲级真题(模板题,两次Dijkstra,同时记下最短路径)
题意:给了图,以及s和t,让你求s到t花费的最短路程.最短时间,以及输出对应的路径. 对于最短路程,如果路程一样,输出时间最少的. 对于最短时间,如果时间一样,输出节点数最少的. 如果最短路程 ...
- 【PAT甲级】1111 Online Map (30分)(dijkstra+路径记录)
题意: 输入两个正整数N和M(N<=500,M<=N^2),分别代表点数和边数.接着输入M行每行包括一条边的两个结点(0~N-1),这条路的长度和通过这条路所需要的时间.接着输入两个整数表 ...
- 1111 Online Map (30 分)
1111. Online Map (30)Input our current position and a destination, an online map can recommend sever ...
- PAT甲级——1111 Online Map (单源最短路经的Dijkstra算法、priority_queue的使用)
本文章同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90041078 1111 Online Map (30 分) ...
- 1111 Online Map (30 分)
1111 Online Map (30 分) Input our current position and a destination, an online map can recommend sev ...
- PAT 1111 Online Map[Dijkstra][dfs]
1111 Online Map(30 分) Input our current position and a destination, an online map can recommend seve ...
随机推荐
- Python学习随笔(1)--可视化工具plotly使用
把数据库某列数据取出来,然后再在本地生成html文件形成可视化视图显示 #!/usr/bin/python# coding=utf-8 import pymysqlimport plotly.plot ...
- Golang源码探索(三) GC的实现原理(转)
Golang从1.5开始引入了三色GC, 经过多次改进, 当前的1.9版本的GC停顿时间已经可以做到极短.停顿时间的减少意味着"最大响应时间"的缩短, 这也让go更适合编写网络服务 ...
- 剑指offer——包含min函数的栈
题目:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度为O(1)) 该题是自己第一次采用编程的方式来实现Java中栈的功能,故直接借鉴了大牛的代码 import ...
- MPC学习笔记1:基于状态空间模型的预测控制(2)
基于估计的无约束预测控制 1.引言 基本上这两个部分都是在线性理论的框架下,利用状态空间法来建模.求解控制律.状态空间模型在理论分析上具有很强的优越性,但实际应用中能直接准确且经济地获取系统状态并不容 ...
- Django系列之web应用与http协议
第一节:最简单的web应用程序 web应用程序指供浏览器访问的程序,通常也简称为web应用.应用程序有两种模式C/S,B/S.C/S是客户端/服务器程序.也就是说这类程序一般独立运行.而B/S就是浏览 ...
- CSRF总结
一.CSRF概述 CSRF跨站请求伪造,2007年被列为互联网20大安全隐患之一. 什么是跨站请求伪造?CSRF或XSRF 挟制用户在当前已经登录的web应用程序上执行非本意的操作的攻击方法.攻击者盗 ...
- jquery的$(selector).each(function(index,element))和$.each(dataresource,function(index,element))的区别
$(selector).each(function(index,element)) 定义和用法 each() 方法规定为每个匹配元素规定运行的函数. $(selector).each(function ...
- 用eclipse创建动态web项目手动生成web.xml方法
建一个web项目,后来在用到web.xml文件时,才发现项目创建时没有自动创建web.xml文件. 在创建的项目上单击右键,然后单击java EE Tools下的用红线圈住的地方,然后查看你的WEB- ...
- nginx屏蔽ip配置
屏蔽单个IP的命令是 deny 192.168.201.1 封ip段192 deny 192.0.0.0/8 封ip段192.168 deny 192.168.0.0/16 封ip段192.168.2 ...
- 动态在线扩容root根分区大小的方法详解
前言 本文主要介绍了关于动态在线扩容root根分区大小的相关内容,分享出来供大家参考学习,下面话不都说了,来一起看看详细的介绍吧. ? 1 qemu-img resize yourname.img + ...