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. WCF输出JSON

    public class MyService : IService { public Message GetXml(string format) { WebOperationContext conte ...

  2. 讨论下茴香逗的茴字有几种写法,javascript字符串数组查找“indexOf"的替代方式。

  3. PYTHON基础-入门

    变量名可以是数字,字母,下划线,字母不能开头.

  4. 区块链入门(5)Truffle 项目实战,Solidity IDE, 智能合约部署

    在上一张我们学习了Truffle项目的创建,部署等相关内容,今天我们就来实战一下. 今天我们要做3件事: 1) 学习搭建一个Solidity IDE(Remix). 2) 使用这个Solidity I ...

  5. [转]使用screw plus来保护php代码安全

    原文链接 https://www.jianshu.com/p/f6425e2f8643 https://github.com/del-xiong/screw-plus http://git.oschi ...

  6. Linux-03

    目录处理命令 目录处理命令:ls 命令名称:ls 命令英文原意:list 命令所在路径:/bin/ls 执行权限:所有用户 功能描述:现实目录文件 语法:ls 选项[-ald] [文件或目录] -a ...

  7. CSS选择器命名及常用命名

    CSS选择器命名及常用命名 CSS选择器命名及常用命名 规范的命名也是Web标准中的重要一项,标准的命名可以使代码更加易读,而且利于搜索引擎搜索,比如定义了两个div,一个 id 命名为“div1”, ...

  8. 牛客小白月赛13 小A的回文串(Manacher)

    链接:https://ac.nowcoder.com/acm/contest/549/B来源:牛客网 题目描述 小A非常喜欢回文串,当然我们都知道回文串这种情况是非常特殊的.所以小A只想知道给定的一个 ...

  9. (1)Linux常用的运维平台和工具

    运维工程师使用的运维平台和工具包括: Web服务器:apache.tomcat.nginx.lighttpd 监控:nagios.ganglia.cacti.zabbix 自动部署:ansible.s ...

  10. 毕设之iframe跳转子页面问题

    我的Django项目中的index.html分为三个层次,head.body.footer.其中body细分为left和right两部分,right的地图是使用iframe嵌入的map.html页面, ...