PAT Advanced 1111 Online Map

Overview

PAT Advanced 1111 Online Map: use Dijkstra to find shortest path for time and distance, then DFS search for the unique path among all the shortest paths.

PAT Advenced 1111 Online Map Problem

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

PAT Advenced 1111 Online Map Algorithm

The algorithm is:

  1. Prepare two graphs, use Dijkstra to find shortest path for time and distance
  2. During Dijkstra, for all the shortest paths that have equal distances or times, keep all possible parents of the nodes
  3. DFS the parents tree, for each path on the parents tree, compare the measurement about either the total time or the total intersections, store the unique one somewere
  4. compare the time path and distance path, if they are the same, print out one line, if not, print out distance and time path separately.

Note the last test case of this problem is large data testing as follows

测试点	结果	用时(ms)	内存(kB)	得分/满分
0	答案正确	2	384	9/9
1	答案正确	6	384	9/9
2	答案正确	4	368	7/7
3	答案正确	6	376	2/2
4	答案正确	56	2432	3/3

Be careful about your implementation, do not use heap or priority queue, it could be TLE.

The following is the C++ source code that could be accepted by PAT OJ to pass this PAT Advenced 1111 Online Map Problem:

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include<vector>
#include<queue>
#include<cstdio>
#include<climits>

const int MAXN = 505;

using std::vector;

bool flag[MAXN];
int dd[MAXN];
int t[MAXN];
vector<int> pd[MAXN];
vector<int> pt[MAXN];

int gd[MAXN][MAXN];
int gt[MAXN][MAXN]; 

int n, m;

int f(int cs[]) {
	int c = INT_MAX;
	int r = -1;
	for (int i = 0; i < n; ++i) {
		if (!flag[i] && cs[i] < c) {
			c = cs[i];
			r = i;
		}
	}
	return r;
}

void gao(int s, int d, int g[MAXN][MAXN], int c[MAXN], vector<int> p[MAXN]) {
	for (int i = 0; i < n; ++i) {
		flag[i] = false;
		c[i] = INT_MAX;
	}

	c[s] = 0;
	while (!flag[d]) {
		int curr = f(c);
		flag[curr] = true;
		for (int i = 0; i < n; ++i) {
			if (g[curr][i] != -1 && !flag[i]) {
				int cl = g[curr][i];

				if (c[i] > c[curr] + cl) {
					c[i] = c[curr] + cl;
					p[i].clear();
					p[i].push_back(curr);
				} else if (c[i] == c[curr] + cl) {
					p[i].push_back(curr);
				}
			}
		}
	}
}


int mt = INT_MAX;
vector<int> pDist;
void gao1(int s,
		int curr,
		vector<int> *p,
		int tt) {
	if (s == curr) {
		if (tt < mt) {
			pDist = *p;
			mt = tt;
		} 
		return ;
	}

	for (int pa : pd[curr]) {
		p->push_back(pa);
		gao1(s, pa, p, tt + gt[pa][curr]);
		p->pop_back();
	}
}


int mh = INT_MAX;
vector<int> pTime;
void gao2(int s,
		int curr,
		vector<int> *p,
		int h) {
	if (s == curr) {
		if (h < mh) {
			pTime = *p;
			mh = h;
		} 
		return ;
	}

	for (int pa : pt[curr]) {
		p->push_back(pa);
		gao2(s, pa, p, h + 1);
		p->pop_back();
	}
}


void printPath(const vector<int> &p) {
	int f = true;
	for (int i = p.size() - 1; i >=0; --i) {
		if (f) {
			printf("%d", p[i]);
			f = false;
		} else {
			printf(" -> %d", p[i]);
		}
	}
}

int main() {
	scanf("%d %d", &n, &m);

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			gd[i][j] = -1;
			gt[i][j] = -1;
		}
	}

	int v1, v2;
	int f;
	int l, tt; 

	for (int i = 0; i < m; ++i) {
		scanf("%d %d %d %d %d", &v1, &v2, &f, &l, &tt);
		gd[v1][v2] = l;
		gt[v1][v2] = tt;
		if (0 == f) {
			gd[v2][v1] = l;
			gt[v2][v1] = tt;
		}
	}

	int s, d; 
	scanf("%d %d", &s, &d);

	gao(s, d, gd, dd, pd);
	gao(s, d, gt, t, pt);

	vector<int> tmp;
	tmp.push_back(d);
	gao1(s, d, &tmp, 0);
	tmp.clear();
	tmp.push_back(d);
	gao2(s, d, &tmp, 0);

	if (pDist == pTime) {
		printf("Distance = %d; Time = %d: ", dd[d], t[d]);
		printPath(pDist);
	} else {
		printf("Distance = %d: ", dd[d]);
		printPath(pDist);
		printf("\n");
		printf("Time = %d: ", t[d]);
		printPath(pTime);
	}
	return 0;
}

Summary

PAT Advanced 1111 Online Map could be solved by using Dijkstra to find shortest path for time and distance, then DFS search for the unique path among all the shortest paths.

Written on September 16, 2016