-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAstar3.cpp
346 lines (335 loc) · 8.52 KB
/
Astar3.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <queue>
#include <cmath>
#include <unordered_set>
#include <cstring>
#include <map>
#include <windows.h>
#define x first
#define y second
using namespace std;
const int N = 5010;
int copy_g[N][N];
int g[N][N]; // adjacency matrix, store the edge information
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<int, PIII> PIIII;
typedef pair<int, char> PIC;
unordered_set<int> queue_mapping;
unordered_map<int, PIIII> del; // SPT, store vertices poped from the queue
unordered_map<int, PII> coordinates; // vertices coordinates
unordered_map<int, vector<PIII>> change; // changes
/* the first dimension is time, the second is starting vertex,
the third is end vertex, the fourth is the updated distance.*/
priority_queue<PIIII, vector<PIIII>, greater<PIIII>> q; // priority queue
/*the first dimension is the estimated distance, the second is the real distance from source to current,
the third is the vertex ID, and the fourth is the predecessor ID.*/
int dist[N];
int change_matrix[15][80];
int pre[N];
const int M = 150;
const int UB = 738;
map<PII, int> streets_mapping;
int Euclidean[N][N];
double longitude[N], latitude[N];
// Angles to radians
double rad(double d)
{
const double PI = 3.1415926535898;
return d * PI / 180.0;
}
// Euclidean Distance: Calculate the straight-line distance between two vertices.
int CalcDistance(float fLati1, float fLong1, float fLati2, float fLong2)
{
const float EARTH_RADIUS = 6378.137;
double radLat1 = rad(fLati1);
double radLat2 = rad(fLati2);
double a = radLat1 - radLat2;
double b = rad(fLong1) - rad(fLong2);
double s = 2 * asin(sqrt(pow(sin(a / 2), 2) + cos(radLat1) * cos(radLat2) * pow(sin(b / 2), 2)));
s = s * EARTH_RADIUS;
s = (int)(s * 10000000) / 10000;
return s;
}
// Absolute value of Euclidean distance
int cal(int st, int ed)
{
return abs(Euclidean[st][ed]);
}
// main function
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
int ID;
scanf("%d", &ID);
double tempx, tempy;
scanf("%lf %lf", &tempx, &tempy);
longitude[i] = tempx;
latitude[i] = tempy;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
Euclidean[i][j] = (int)CalcDistance(longitude[i], latitude[i], longitude[j], latitude[j]);
}
}
// Initialize the state table
cout << "---------------initialize the state table---------------(Input(0,0,0)to end)\n";
for (int i = 1; i <= 12; i++)
{
for (int j = 1; j <= 60; j++)
{
scanf("%d", &change_matrix[i][j]);
}
}
// Initialize the street graph
cout << "---------------initialize the street graph---------------(Input(0,0,0)to end)\n";
setbuf(stdin, NULL); // Make stdin input stream to unbuffered
while (true)
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
streets_mapping[{a, b}] = c;
streets_mapping[{b, a}] = c;
if (a + b + c == 0)
{
break;
}
}
// Initialize the edge information
cout << "---------------initialize the edge information---------------(Input(0,0,0)to end)\n";
for (int i = 1; i <= M; i++)
{
for (int j = 1; j <= M; j++)
{
g[i][j] = 50000;
}
}
memset(dist, 0x3f, sizeof dist);
while (true)
{
int a, b;
double dis;
scanf("%d %d %lf", &a, &b, &dis);
if (a + b + dis == 0)
{
break;
}
g[a][b] = (int)dis;
g[b][a] = (int)dis;
}
for (int i = 1; i <= N; i++)
{
g[i][i] = 0;
}
memcpy(copy_g, g, sizeof copy_g);
// Initialize the change information
cout << "-----------initialize the change information---------------(Input(0,0,0)to end)\n";
while (true)
{
int t, a, b, dis;
for (auto temp : streets_mapping)
{
int a = temp.first.first;
int b = temp.first.second;
int id = temp.second;
int pre = change_matrix[id][1];
for (int j = 1; j <= 60; j++)
{
if (change_matrix[id][j] != pre)
{
if (change_matrix[id][j] == 0)
change[j].push_back({a, {b, 50000}});
else
change[j].push_back({a, {b, copy_g[a][b]}});
}
pre = change_matrix[id][j];
}
}
break;
}
// Initialize the source vertex and the destination vertex
cout << "-----------initialize the source vertex and the destination vertex---------\n";
int st, ed;
scanf("%d %d", &st, &ed);
dist[st] = 0;
q.push({dist[st] + cal(st, ed), {dist[st], {st, -1}}});
queue_mapping.insert(st);
int t = 1;
while (true)
{
t++;
// check for changes
if (change.find(t) != change.end())
{
vector<PIII> temp_change = change[t];
for (auto cur_node : temp_change)
{
int update_st = cur_node.first; // source vertex
int update_ed = cur_node.second.first; // destination vertex
int update_dis = cur_node.second.second;
if (del.find(update_ed) == del.end() && queue_mapping.find(update_ed) == queue_mapping.end())
{
// not in queue or SPT
//continue;
}
else if (queue_mapping.find(update_ed) != queue_mapping.end())
{
//in queque
vector<PIIII> temp_v;
while (q.top().second.second.first != update_ed)
{
temp_v.push_back(q.top());
q.pop();
}
auto temp_item = q.top();
q.pop();
if (update_dis == 50000)
{
if (temp_item.second.second.second == update_st)
{
temp_item.first = 50000;
temp_item.second.first = 50000;
}
}
else
{
if (temp_item.second.second.second == update_st)
{
temp_item.first = dist[update_st] + (update_dis) + cal(update_ed, ed);
temp_item.second.first = dist[update_ed] + (update_dis);
}
else
{
if (dist[update_st] + (update_dis) + cal(update_ed, ed) < temp_item.first)
{
temp_item.first = dist[update_st] + (update_dis) + cal(update_ed, ed);
temp_item.second.first = dist[update_ed] + (update_dis);
temp_item.second.second.second = update_st;
pre[temp_item.second.second.first] = update_st;
}
}
}
if (temp_item.first > 0)
{
q.push(temp_item);
for (auto x : temp_v)
{
q.push(x);
}
}
}
else
{
//in SPT
auto temp_item = del[update_ed];
if (update_dis == 50000)
{
continue;
}
if (update_dis != 50000)
{
if (dist[update_st] + (update_dis) + cal(update_ed, ed) < temp_item.first)
{
vector<int> temp_v;
int cur_id = temp_item.second.second.first;
for (auto x : del)
{
int cur_id = x.second.second.first;
if (cur_id != update_ed && g[update_ed][cur_id] != 50000)
{
temp_v.push_back(cur_id);
}
}
temp_item.first = dist[update_st] + (update_dis) + cal(update_ed, ed);
if (temp_item.first > 0)
{
for (auto x : temp_v)
{
del.erase(x);
}
del.erase(update_ed);
temp_item.first = dist[update_st] + (update_dis) + cal(update_ed, ed);
temp_item.second.first = dist[update_st] + (update_dis);
temp_item.second.second.second = update_st;
q.push(temp_item);
}
}
}
}
g[update_st][update_ed] = g[update_ed][update_st] = update_dis;
}
}
else
{
auto cur_node = q.top();
q.pop();
int cur_id = cur_node.second.second.first;
queue_mapping.erase(cur_id);
int cur_dist = cur_node.second.first;
del[cur_id] = cur_node;
for (int i = 1; i <= M; i++)
{
if (g[cur_id][i] != 50000 && i != cur_id)
{
if (dist[i] > cur_dist + g[cur_id][i] && i != cur_id)
{
if (i == 63)
{
// cout << "current vertex ID = %d; the dist value = %d; the edge length = %d; the target vertex = %d\n",cur_id , cur_dist , g[cur_id][i] , i;
;
}
dist[i] = cur_dist + g[cur_id][i];
pre[i] = cur_id;
q.push({dist[i] + cal(i, ed), {dist[i], {i, cur_id}}});
queue_mapping.insert(i);
}
}
}
cout << cur_dist << "fesfegerhr" << endl;
cout << cur_id << "fdsfassd" << endl;
if (cur_id == ed && dist[ed] != 50000)
{
break;
}
}
}
vector<int> temp_v;
for (int i = 1; i <= 150; i++)
{
cout << dist[i] << endl;
}
for (int i = ed; i != st; i = pre[i])
{
temp_v.push_back(i);
cout << i << "<-";
}
temp_v.push_back(st);
int temp_t = 0;
double item = 0;
double sum = 0;
reverse(temp_v.begin(), temp_v.end());
for (int i = 0; i < temp_v.size(); i++)
{
if (i != 0)
{
int temp_ID = streets_mapping[{temp_v[i - 1], temp_v[i]}];
if (change_matrix[temp_t][temp_ID] >= 1)
{
item += (double)g[temp_v[i - 1]][temp_v[i]];
}
sum += (double)g[temp_v[i - 1]][temp_v[i]];
}
temp_t++;
}
double per = item / sum * 100.0;
printf("percentage = %.2f\%\n", per);
return 0;
}