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
思路:题目比较常规,使用DFS最后一个节点超时,最后使用Dijkstra来AC,注释部分为DFS代码~
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n, m;
vector<vector<int> > vec;
// vector<int> temp_path;
// vector<int> dis_path;
// vector<int> time_path;
// bool visit[501] = {false};
int graph_length[501][501] = {0};
int graph_time[501][501] = {0};
// int min_dis = 0x7fffffff;
// int min_dis_time = 0x7fffffff;
// int min_time = 0x7fffffff;
// int min_time_section = 0x7fffffff;
// void dfs(int start, int end, int dis, int time){ // 使用dfs最后一个节点超时,very sorrow~
// if(start == end){
// if(dis < min_dis || (dis == min_dis && time < min_dis_time)){
// min_dis = dis;
// dis_path = temp_path;
// min_dis_time = time;
// }
// if(time < min_time || (time == min_time && temp_path.size() < min_time_section)){
// min_time = time;
// time_path = temp_path;
// min_time_section = temp_path.size();
// }
// return;
// }
// for(int i = 0; i < vec[start].size(); i++){
// if(!visit[vec[start][i]]){
// temp_path.push_back(vec[start][i]);
// visit[vec[start][i]] = true;
// dfs(vec[start][i], end, dis + graph_length[start][vec[start][i]], time + graph_time[start][vec[start][i]]);
// visit[vec[start][i]] = false;
// temp_path.pop_back();
// }
// }
// }
int dij_dis[501];
int dij_time[501];
int dij_path[501];
int dij_visit[501];
const int max_init = 99999999;
void dis_dijkstra(int start){
fill(dij_dis, dij_dis + 501, max_init);
fill(dij_visit, dij_visit + 501, false);
dij_dis[start] = 0;
for(int i = 0; i < n; i++){
dij_path[i] = i;
}
for(int i = 0; i < n; i++){
int u = -1;
int temp_min = max_init;
for(int j = 0; j < n; j++){
if(!dij_visit[j] && dij_dis[j] < temp_min){
u = j;
temp_min = dij_dis[j];
}
}
if(u == -1){
return;
}
dij_visit[u] = true;
for(int j = 0; j < n; j++){ //launch优化
if(!dij_visit[j] && graph_length[u][j] != max_init){
if(dij_dis[u] + graph_length[u][j] < dij_dis[j]){
dij_dis[j] = dij_dis[u] + graph_length[u][j];
dij_time[j] = dij_time[u] + graph_time[u][j];
dij_path[j] = u;
}else if(dij_dis[u] + graph_length[u][j] == dij_dis[j] && dij_time[u] + graph_time[u][j] < dij_time[j])
{
dij_time[j] = dij_time[u] + graph_time[u][j];
dij_path[j] = u;
}
}
}
}
}
int section_cnt[501];
void time_dijkstra(int start){
for(int i = 0; i < n; i++){
dij_path[i] = i;
}
fill(dij_time, dij_time + 501, max_init);
fill(section_cnt, section_cnt + 501, 0);
fill(dij_visit, dij_visit + 501, false);
dij_time[start] = 0;
for(int i = 0; i < n; i++){
int u = -1;
int min = max_init;
for(int j = 0; j < n; j++){
if(!dij_visit[j] && dij_time[j] < min){
u = j;
min = dij_time[j];
}
}
if(u == -1){
return;
}
dij_visit[u] = true;
for(int j = 0; j < n; j++){ //launch优化
if(!dij_visit[j] && graph_time[u][j] != max_init){
if(dij_time[u] + graph_time[u][j] < dij_time[j]){
dij_time[j] = dij_time[u] + graph_time[u][j];
dij_path[j] = u;
section_cnt[j] = section_cnt[u] + 1;
}else if(dij_time[u] + graph_time[u][j] == dij_time[j] && section_cnt[u] + 1 < section_cnt[j])
{
section_cnt[j] = section_cnt[u] + 1;
dij_path[j] = u;
}
}
}
}
}
int main(){
cin >> n >> m;
vec.resize(n);
fill(graph_length[0], graph_length[0] + 501 * 501, max_init);
fill(graph_time[0], graph_time[0] + 501 * 501, max_init);
for(int i = 0; i < m; i++){
int v1, v2, one_way, length, time;
scanf("%d %d %d %d %d", &v1, &v2, &one_way, &length, &time);
graph_length[v1][v2] = length;
graph_time[v1][v2] = time;
vec[v1].push_back(v2);
if(!one_way){
vec[v2].push_back(v1);
graph_time[v2][v1] = time;
graph_length[v2][v1] = length;
}
}
int in_start, in_end;
scanf("%d %d", &in_start, &in_end);
dis_dijkstra(in_start);
vector<int> vec_time_out;
vector<int> vec_dis_out;
int x = in_end;
while (x != dij_path[x])
{
vec_dis_out.push_back(dij_path[x]);
x = dij_path[x];
}
time_dijkstra(in_start);
x = in_end;
while (x != dij_path[x])
{
vec_time_out.push_back(dij_path[x]);
x = dij_path[x];
}
// dfs(in_start, in_end, 0, 0);
// Distance = 6: 3 -> 4 -> 8 -> 5
// Time = 3: 3 -> 1 -> 5
// Distance = 3; Time = 4: 3 -> 2 -> 5
if(vec_dis_out == vec_time_out){
printf("Distance = %d; Time = %d: ", dij_dis[in_end], dij_time[in_end]);
for(int i = vec_dis_out.size() - 1; i >= 0 ; i--){
printf("%d -> ", vec_dis_out[i]);
}
printf("%d\n", in_end);
}else
{
printf("Distance = %d: ", dij_dis[in_end]);
for(int i = vec_dis_out.size() - 1; i >= 0 ; i--){
printf("%d -> ", vec_dis_out[i]);
}
printf("%d\n", in_end);
printf("Time = %d: ", dij_time[in_end]);
for(int i = vec_time_out.size() - 1; i >= 0 ; i--){
printf("%d -> ", vec_time_out[i]);
}
printf("%d\n", in_end);
}
// if(time_path == dis_path){
// printf("Distance = %d; Time = %d: ", min_dis, min_time);
// printf("%d", in_start);
// for(int i = 0; i < time_path.size(); i++){
// printf(" -> %d", time_path[i]);
// }
// printf("\n");
// }else
// {
// printf("Distance = %d: ", min_dis);
// printf("%d", in_start);
// for(int i = 0; i < dis_path.size(); i++){
// printf(" -> %d", dis_path[i]);
// }
// printf("\n");
// printf("Time = %d: ", min_time);
// printf("%d", in_start);
// for(int i = 0; i < time_path.size(); i++){
// printf(" -> %d", time_path[i]);
// }
// printf("\n");
// }
return 0;
}
本文探讨了在给定地图上寻找从起点到终点的最短路径和最快路径的问题,通过对比深度优先搜索(DFS)和迪杰斯特拉算法(Dijkstra),详细解释了如何在考虑距离和时间两个因素的情况下找到最优解。





