PAT 1111

题目 : Online Map

分值 : 30
难度 : 中等题
思路 : 两次不同的Dijkstra,细节把握好就行,属于基本功题
坑点 : 代码略有冗余,写的太丑了,但是一次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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
using namespace std;
#define Max_ 9999999999
int length[501][501] ;
int Time[501][501] ;
int pre[501];
int Flag = 0 ;
int Q1[501] ; int Q1_cur = 0 ;
int Q2[501] ; int Q2_cur = 0 ;
void print(int s ,int b )
{
if(s!=-1)
{
print(pre[s] , b);
if(b == 0)
Q1[Q1_cur++] = s;
else
Q2[Q2_cur++] = s ;
//if(Flag++)
// cout << " -> ";
//cout << s ;
}
}
int main() {
int N , M ,S,D;
cin >> N >> M ;
int v1 ,v2 ,one_way ,leng,Tim ;
for(int i = 0 ; i< N ; i++)
for(int j = 0 ; j< N ; j++)
{
length[i][j] =Max_ ;
Time[i][j] = Max_ ;
}
for(int i = 0 ; i< M ; i++)
{
cin >> v1 >> v2 >> one_way >>leng>>Tim ;
length[v1][v2] =leng ;
Time[v1][v2] = Tim ;
if(one_way ==0)
{
length[v2][v1]= leng ;
Time[v2][v1] =Tim;
}
}
cin >> S>>D ;
int dict[N] ;
int time_cost[N];
int flag[N] ;
/*
* deal with the shortest path
*/
for(int i = 0 ; i <N ; i++)
{
dict[i] =Max_ ;
time_cost[i]= Max_ ;
pre[i]= -1 ;
flag[i] = 0 ;
}
dict[S] = 0 ;time_cost[S] = 0 ;
int count = 0 ;
while(count < N)
{
int min=Max_ , index;
for(int i = 0 ; i < N ; i++) //find low dict
{
if(flag[i]) continue ;
if(dict[i] < min )
{
min = dict[i];
index = i ;
}
}
flag[index] = 1 ;
for(int j = 0 ; j<N; j++)
{
if(flag[j]) continue;
if(dict[j] > dict[index] + length[index][j] )
{
dict[j] = dict[index] + length[index][j] ;
time_cost[j] = time_cost[index] + Time[index][j] ;
pre[j] = index ;
}
if(dict[j] == dict[index] + length[index][j])
{
if(time_cost[j] > time_cost[index] + Time[index][j] )
{
time_cost[j] = time_cost[index] + Time[index][j] ;
pre[j] = index ;
}
}
}
count ++ ;
}
// for(int i = 0 ;i < N ; i++)
// cout << dict[i] <<" ";
print(D,0) ;
//cout <<endl ;
/*
* deal with the fastest path
*/
int total[N ] ;
Flag = 0 ;
for(int i = 0 ; i <N ; i++)
{
total[i] = 0 ;
time_cost[i]= Max_ ;
pre[i]= -1 ;
flag[i] = 0 ;
}
time_cost[S] = 0 ;
count = 0 ;
while(count < N)
{
int min=Max_ , index;
for(int i = 0 ; i < N ; i++) //find low dict
{
if(flag[i]) continue ;
if(time_cost[i] < min )
{
min = time_cost[i];
index = i ;
}
}
flag[index] = 1 ;
for(int j = 0 ; j<N; j++)
{
if(flag[j]) continue;
if(time_cost[j] > time_cost[index] + Time[index][j] )
{
time_cost[j] = time_cost[index] + Time[index][j] ;
total[j] =total[index] +1 ;
pre[j] = index ;
}
if(time_cost[j] == time_cost[index] + Time[index][j])
{
if(total[j] > total[index]+1 )
{
total[j] = total[index] +1 ;
pre[j] = index ;
}
}
}
count ++ ;
}
// for(int i = 0 ;i < N ; i++)
// cout << dict[i] <<" ";
print(D,2) ;
bool T = true ;
for(int i = 0 ; i< N; i++)
{
if(Q1[i] != Q2[i])
{
T=false ;
break ;
}
else
{
if(Q1[i] == D)
break ;
}
}
if(T)
{
cout <<"Distance = "<<dict[D]<<"; Time = "<<time_cost[D]<<": ";
for(int i = 0 ; i< Q1_cur ; i++)
{
if(Flag++)
cout <<" -> ";
cout <<Q1[i] ;
}
}
else {
cout <<"Distance = "<<dict[D]<<": ";
for(int i = 0 ; i< Q1_cur ; i++)
{
if(Flag++)
cout <<" -> ";
cout <<Q1[i] ;
}
cout <<endl ;
Flag = 0 ;
cout <<"Time = "<<time_cost[D]<<": ";
for(int i = 0 ; i< Q2_cur ; i++)
{
if(Flag++)
cout <<" -> ";
cout <<Q2[i] ;
}
cout <<endl ;
}

}