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)的更多相关文章

  1. PAT (Advanced Level) 1111. Online Map (30)

    预处理出最短路再进行暴力dfs求答案会比较好.直接dfs效率太低. #include<cstdio> #include<cstring> #include<cmath&g ...

  2. 1111 Online Map (30)(30 分)

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...

  3. PAT Advanced 1111 Online Map (30) [Dijkstra算法 + DFS]

    题目 Input our current position and a destination, an online map can recommend several paths. Now your ...

  4. PAT甲题题解-1111. Online Map (30)-PAT甲级真题(模板题,两次Dijkstra,同时记下最短路径)

    题意:给了图,以及s和t,让你求s到t花费的最短路程.最短时间,以及输出对应的路径.   对于最短路程,如果路程一样,输出时间最少的. 对于最短时间,如果时间一样,输出节点数最少的.   如果最短路程 ...

  5. 【PAT甲级】1111 Online Map (30分)(dijkstra+路径记录)

    题意: 输入两个正整数N和M(N<=500,M<=N^2),分别代表点数和边数.接着输入M行每行包括一条边的两个结点(0~N-1),这条路的长度和通过这条路所需要的时间.接着输入两个整数表 ...

  6. 1111 Online Map (30 分)

    1111. Online Map (30)Input our current position and a destination, an online map can recommend sever ...

  7. PAT甲级——1111 Online Map (单源最短路经的Dijkstra算法、priority_queue的使用)

    本文章同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90041078   1111 Online Map (30 分) ...

  8. 1111 Online Map (30 分)

    1111 Online Map (30 分) Input our current position and a destination, an online map can recommend sev ...

  9. PAT 1111 Online Map[Dijkstra][dfs]

    1111 Online Map(30 分) Input our current position and a destination, an online map can recommend seve ...

随机推荐

  1. sql存储过程调用示例

    1.配置文件: <connectionStrings> <add name="constr" connectionString="data source ...

  2. Unable to docker login through CLI - unauthorized: incorrect username or password

    Unable to docker login through CLI - unauthorized: incorrect username or password To solve it proper ...

  3. k3生成解决方案时错误处理

    F6一键生成,会出现进程使用的错误,关掉了游览器,bos设计器,以及重启了本机iis站点,都没解决,打开任务管理器发现,bos.ide没有关掉

  4. SQL Server 与MySQL中排序规则与字符集相关知识的一点总结

    字符集&&排序规则 字符集是针对不同语言的字符编码的集合,比如UTF-8字符集,GBK字符集,GB2312字符集等等,不同的字符集使用不同的规则给字符进行编码排序规则则是在特定字符集的 ...

  5. dump命令详解

    1.简介: dump命令用于备份文件系统. dump为备份工具程序,可将目录或整个文件系统备份至指定的设备,或备份成一个大文件. 2.语法: dump [-cnu][-0123456789][-b & ...

  6. mybatis sql in not in的使用

    xml配置 <select id="SelectAllByNotsampleNo" resultMap="BaseResultMap" parameter ...

  7. Game Engine Architecture 7

    [Game Engine Architecture 7] 1.SRT Transformations When a quaternion is combined with a translation ...

  8. 解决SpringMVC拦截器中Request数据只能读取一次的问题

    解决SpringMVC拦截器中Request数据只能读取一次的问题 开发项目中,经常会直接在request中取数据,如Json数据,也经常用到@RequestBody注解,也可以直接通过request ...

  9. Linux - 远程管理常用命令

    远程管理常用命令 目标 关机/重启 shutdown 查看或配置网卡信息 ifconfig ping 远程登录和复制文件 ssh scp 01. 关机/重启 序号 命令 对应英文 作用 01 shut ...

  10. Python 的xlutils模块

    python模块: xlrd:读取excel xlwt:写入excel 缺点:excel格式无法复用 推荐xlutils模块 可复制原excel格式 from xlutils.copy import ...