A1111 Online Map (30 point(s))

求最短路径,Dijskstra

1. 原文

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 Mlines 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

2. 解析

Dijskstra应用 固定套路

3. AC代码

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
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=510;
const int Inf=312312312;
int graph[maxn][maxn];
int timecost[maxn][maxn];
int dis[maxn];
bool visit[maxn]={false};
int co[maxn];
int pre[maxn]={};
int num[maxn]={};
int s,t;
void dfs(vector<int> &v,int a)
{
if (a==s)
{
return;
}else{
dfs(v,pre[a]);
}
v.push_back(a);
}
int main()
{
fill(graph[0],graph[0]+maxn*maxn,Inf);
fill(timecost[0],timecost[0]+maxn*maxn,Inf);
int n,m;
scanf("%d%d",&n,&m);
int v1,v2,oneway;
for (int i = 0; i < m; ++i)
{
scanf("%d%d%d",&v1,&v2,&oneway);
scanf("%d%d",&graph[v1][v2],&timecost[v1][v2]);
if (oneway==0)
{
graph[v2][v1]=graph[v1][v2];
timecost[v2][v1]=timecost[v1][v2];
}
}
scanf("%d%d",&s,&t);
fill(dis,dis+maxn,Inf);
fill(co,co+maxn,Inf);
dis[s]=0;
co[s]=0;
for (int i = 0; i < n; ++i)
{
int u=-1;int min=Inf;
for (int j = 0; j < n; ++j)
{
if (visit[j]==false&&dis[j]<min)
{
u=j;
min=dis[j];
}
}
if (u==-1)
{
break;
}
visit[u]=true;
for (int v = 0; v < n; ++v)
{
if (visit[v]==false&&graph[u][v]!=Inf)
{
if (dis[u]+graph[u][v]<dis[v])
{
dis[v]=dis[u]+graph[u][v];
co[v]=timecost[u][v]+co[u];
pre[v]=u;
}else if(dis[u]+graph[u][v]==dis[v]){
if (co[u]+timecost[u][v]<co[v])
{
co[v]=co[u]+timecost[u][v];
pre[v]=u;
}
}
}
}
}
vector<int> ansD;
dfs(ansD,t);
fill(visit,visit+maxn,false);
fill(pre,pre+maxn,0);
fill(co,co+maxn,Inf);
fill(num,num+maxn,Inf);
co[s]=0;
num[s]=1;
for (int i = 0; i < n; ++i)
{
int u=-1;
int min=Inf;
for (int j = 0; j < n; ++j)
{
if (visit[j]==false&&co[j]<min)
{
u=j;
min=co[j];
}
}
if (u==-1)
{
break;
}
visit[u]=true;
for (int v = 0; v < n; ++v)
{
if (visit[v]==false&&graph[u][v]!=Inf)
{
if (co[u]+timecost[u][v]<co[v])
{
co[v]=co[u]+timecost[u][v];
num[v]=num[u]+1;
pre[v]=u;
}else if(co[u]+timecost[u][v]==co[v]){
if (num[u]+1<num[v])
{
num[v]=num[u]+1;
pre[v]=u;
}

}
}
}
}
vector<int> ansT;
dfs(ansT,t);

if (ansD==ansT)
{
printf("Distance = %d; Time = %d: %d", dis[t],co[t],s);
for (int i = 0; i < ansD.size(); ++i)
{
printf(" -> %d",ansD[i]);
}
printf("\n");
}else{
printf("Distance = %d: %d", dis[t],s);
for (int i = 0; i < ansD.size(); ++i)
{
printf(" -> %d",ansD[i]);
}
printf("\n");

printf("Time = %d: %d", co[t],s);
for (int i = 0; i < ansT.size(); ++i)
{
printf(" -> %d",ansT[i]);
}
printf("\n");
}
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1111/
  • 发布时间: 2019-09-03 12:30
  • 更新时间: 2021-10-29 14:08
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!