PAT 1111 Online Map (30)
1111 Online Map (30)(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
思路:
兩遍dijkstra,第一遍求最短路徑同時維護最小距離和最少時間,第二遍求最短時間同時維護最少時間和最少節點個數,並且兩次都要儲存路徑用於輸出時比較兩次路徑是否一樣。
程式碼:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
int d[500][500], t[500][500], dis[500], cost[500], tim[500], num[500], vis[500], pre1[500], pre2[500];
void dijkstra1(int a, int b, int n)
{
for (int i = 0; i < n; i++)
{
vis[i] = 0;
dis[i] = INF;
cost[i] = INF;
}
dis[a] = 0;
cost[a] = 0;
for (int i = 0; i < n; i++)
{
int min = INT_MAX, k = -1;
for (int j = 0; j < n; j++)
{
if (!vis[j] && dis[j] < min)
{
min = dis[j];
k = j;
}
}
if (k == -1)
return;
vis[k] = 1;
if (k == b)
return;
for (int j = 0; j < n; j++)
{
if (!vis[j] && d[k][j] != INF)
{
if (dis[k] + d[k][j] < dis[j])
{
dis[j] = dis[k] + d[k][j];
cost[j] = cost[k] + t[k][j];
pre1[j] = k;
}
else if (dis[k] + d[k][j] == dis[j] && cost[k] + t[k][j] < cost[j])
{
cost[j] = cost[k] + t[k][j];
pre1[j] = k;
}
}
}
}
}
void dijkstra2(int a, int b, int n)
{
for (int i = 0; i < n; i++)
{
vis[i] = 0;
tim[i] = INF;
num[i] = 0;
}
tim[a] = 0;
num[a] = 1;
for (int i = 0; i < n; i++)
{
int min = INT_MAX, k = -1;
for (int j = 0; j < n; j++)
{
if (!vis[j] && tim[j] < min)
{
min = tim[j];
k = j;
}
}
if (k == -1)
return;
vis[k] = 1;
if (k == b)
return;
for (int j = 0; j < n; j++)
{
if (!vis[j] && t[k][j] != INF)
{
if (tim[k] + t[k][j] < tim[j])
{
tim[j] = tim[k] + t[k][j];
num[j] = num[k] + 1;
pre2[j] = k;
}
else if (tim[k] + t[k][j] == tim[j] && num[k] + 1 < num[j])
{
num[j] = num[k] + 1;
pre2[j] = k;
}
}
}
}
}
bool is_identical(int a, int b)
{
int i = pre1[b], j = pre2[b];
while (i != a || j != a)
{
if (i != j)
return false;
i = pre1[i];
j = pre2[j];
}
if (i != j)
return false;
return true;
}
void print_path(int pre[], int a, int b)
{
if (b == a)
return;
print_path(pre, a, pre[b]);
cout << " -> " << b;
}
int main()
{
int n, m;
cin >> n >> m;
memset(d, INF, sizeof(d));
memset(t, INF, sizeof(t));
for (int i = 0; i < m; i++)
{
int v1, v2, way, len, time;
cin >> v1 >> v2 >> way >> len >> time;
d[v1][v2] = len;
t[v1][v2] = time;
if (!way)
{
d[v2][v1] = len;
t[v2][v1] = time;
}
}
int sou, des;
cin >> sou >> des;
dijkstra1(sou, des, n);
dijkstra2(sou, des, n);
if (is_identical(sou, des))
{
cout << "Distance = " << dis[des] << "; Time = " << tim[des] << ": " << sou;
print_path(pre1, sou, des);
}
else
{
cout << "Distance = " << dis[des] << ": " << sou;
print_path(pre1, sou, des);
cout << endl;
cout<<"Time = "<< tim[des] << ": " << sou;
print_path(pre2, sou, des);
}
return 0;
}