徐子烊

  • 主页
  • 语雀
所有文章 关于我

徐子烊

  • 主页
  • 语雀

1111 Online Map (30 分)

2019-04-25

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:

1
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:

1
Distance = D: source -> v1 -> ... -> destination

Then in the next line print the fastest path with total time T:

1
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:

1
Distance = D; Time = T: source -> u1 -> ... -> destination

Sample Input 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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:

1
2
Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5

Sample Input 2:

1
2
3
4
5
6
7
8
9
10
11
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:

1
Distance = 3; Time = 4: 3 -> 2 -> 5

作者: CHEN, Yue

单位: 浙江大学

时间限制: 400 ms

内存限制: 64 MB

代码长度限制: 16 KB

题目大意

给出一张图,结点之间既有长度也有时间。求起点至终点最短路径、时间最短路径。其中最短路径有多条时,输出时间最短的。时间最短路径有多条时,输出路上结点数最少的。

分析

使用两次dijkstra。因为要输出路径,用pre数组记录前驱。tatn(timeatnode)记录走至第i个结点的时间,natn(numberatnode)记录走至第i个结点时路径上结点数。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <deque>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cmath>
#include <sstream>

using namespace std;
int n, m, start, des, inf = 999999999;
int e[505][505];
int tim[505][505];
int dis[505], pre[505], vis[505],tatn[505],natn[505];

int main() {
cin >> n >> m;
fill(e[0], e[0] + 505 * 505, inf);
fill(tim[0], tim[0] + 505 * 505, inf);
fill(dis, dis + 505, inf);
int v1, v2, oneway, len, t;
for (int i = 0; i < m; i++) {
cin >> v1 >> v2 >> oneway >> len >> t;
e[v1][v2] = len;
tim[v1][v2] = t;
if (!oneway) {
e[v2][v1] = len;
tim[v2][v1] = t;
}
}
cin >> start >> des;
dis[start] = 0;
//计算最短路
for (int i = 0; i < n; i++) {
int v = -1, mindis = inf;
for (int j = 0; j < n; j++) {
if (!vis[j] && dis[j] != inf) {
if (dis[j] < mindis) {
mindis=dis[j];
v = j;
}
}
}
if (v == -1 ||v==des)
break;
vis[v] = 1;
for (int i = 0; i < n; i++) {
if (!vis[i] && e[v][i] != inf) {
if (dis[i] > dis[v] + e[v][i]) {
dis[i] = dis[v] + e[v][i];
pre[i] = v;
tatn[i]=tatn[v]+tim[v][i];
}else if(dis[i] == dis[v] + e[v][i]){//路径相同时,比较时间
if(tatn[i]>tatn[v]+tim[v][i]) {
pre[i] = v;
tatn[i]=tatn[v]+tim[v][i];
}
}
}
}
}
vector<int> dpath;
for(int i=des;i!=start;){
dpath.push_back(i);
i=pre[i];
}
cout<<"Distance = "<<dis[des];
fill(dis, dis + 505, inf);
fill(vis,vis+505,0);
dis[start]=0;
//计算最短时间
for (int i = 0; i < n; i++) {
int v = -1, mint = inf;
for (int j = 0; j < n; j++) {
if (!vis[j] && dis[j] != inf) {
if (dis[j] < mint) {
mint=dis[j];
v = j;
}
}
}
if (v == -1 ||v==des)
break;
vis[v] = 1;
for (int i = 0; i < n; i++) {
if (!vis[i] && tim[v][i] != inf) {
if (dis[i] > dis[v] + tim[v][i]) {
dis[i] = dis[v] + tim[v][i];
pre[i] = v;
natn[i]=natn[v]+1;
}else if(dis[i]==dis[v]+tim[v][i]){//时间相同时,比较路径上结点数
if(natn[i]>natn[v]) {
pre[i] = v;
natn[i]=natn[v]+1;
}
}
}
}
}
vector<int> tpath;
for(int i=des;i!=start;){
tpath.push_back(i);
i=pre[i];
}
if(tpath==dpath)
cout<<"; ";
else {
cout << ": " << start;
for (int i = dpath.size() - 1; i >= 0; i--)
cout << " -> " << dpath[i];
cout<<endl;
}
cout<<"Time = "<<dis[des]<<": "<<start;
for (int i = tpath.size() - 1; i >= 0; i--)
cout << " -> " << tpath[i];
return 0;
}

其他

测试点4是时间相同情况,测试点2是路径长度相同情况。

  • PAT
  • dijkstra
  • 图
  • PAT题解

扫一扫,分享到微信

微信分享二维码
1112 Stucked Keyboard (20 分)
1110 Complete Binary Tree (25 分)
  1. 1. 1111 Online Map (30 分)
    1. 1.1. Input Specification:
    2. 1.2. Output Specification:
    3. 1.3. Sample Input 1:
    4. 1.4. Sample Output 1:
    5. 1.5. Sample Input 2:
    6. 1.6. Sample Output 2:
  2. 2. 题目大意
  3. 3. 分析
  4. 4. 代码
  5. 5. 其他
© 2020 徐子烊
Hexo Theme Yilia by Litten
  • 所有文章
  • 关于我

tag:

  • PAT
  • 多项式
  • dijkstra
  • 图
  • 树
  • 最大子序列
  • 进制转换
  • 二分
  • dfs
  • 连通分量
  • 模拟
  • STL
  • 大整数
  • 需复习
  • 链表
  • 贪心
  • 动态规划
  • 子序列
  • 二叉树
  • LCS
  • 栈
  • 排序
  • 质因数
  • 树状数组
  • 完全二叉树
  • 整数溢出
  • AVL树
  • 深搜
  • 哈希
  • bfs
  • 并查集
  • 红黑树
  • LCA
  • 拓扑排序
  • lca
  • 实习
  • bat
  • Leetcode
  • Java并发
  • 线程池
  • 6.824
  • 分布式
  • mapreduce
  • caffe
  • epoll
  • Raft
  • schnorr
  • 密码学
  • 设计模式
  • 单例模式
  • 原型模式
  • 工厂模式
  • 建造者模式
  • 抽象工厂
  • 桥接模式
  • 模版模式
  • 组合模式
  • 策略模式
  • 装饰器模式
  • 迭代器模式
  • 适配器模式

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

很惭愧

只做了一点微小的工作