-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.cpp
345 lines (306 loc) · 9.12 KB
/
graph.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
#include "graph.h"
// 从文件读取数据
bool Graph::readFileData(QString fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
return false;
QTextStream in(&file);
while (!in.atEnd()) {
Line line;
QString id, name, colour, totalStations, color;
bool ok;
int total;
int lvIndex, svIndex1, svIndex2;
in >> id >> line.id;
in >> name >> line.name;
in >> colour >> color;
line.color.setRgba(color.remove(0, 1).toUInt(&ok, 16));
in >> totalStations >> total;
if (linesHash.count(line.name)) {
lvIndex = linesHash[line.name];
} else {
lvIndex = linesHash[line.name] = lines.size();
lines.push_back(line);
}
Station station;
QString longlat;
QStringList strList;
for (int i = 0; !in.atEnd() && i < total; ++i) {
in >> station.id >> station.name >> longlat;
strList = longlat.split(',');
station.longitude = strList.first().toDouble();
station.latitude = strList.last().toDouble();
if (stationsHash.count(station.name)) {
svIndex2 = stationsHash[station.name];
} else {
svIndex2 = stationsHash[station.name] = stations.size();
stations.push_back(station);
}
stations[svIndex2].belongLines.insert(lvIndex);
lines[lvIndex].stationsSet.insert(svIndex2);
if (i) {
lines[lvIndex].edges.insert(Edge(svIndex1, svIndex2));
lines[lvIndex].edges.insert(Edge(svIndex2, svIndex1));
insertEdge(svIndex1, svIndex2);
}
svIndex1 = svIndex2;
}
bool flag = id == "id:" && name == "name:" && colour == "colour:"
&& totalStations == "totalStations:" && ok && !in.atEnd();
if (flag == false) {
file.close();
clearData();
return false;
}
in.readLine();
}
file.close();
updateMinMaxLongiLati();
return true;
}
// dijkstra获取最少时间的线路
bool Graph::queryTransferMinTime(int start,
int end,
QList<int> &stationsList,
QList<Edge> &edgesList)
{
const long long INF = 999999999;
stationsList.clear();
edgesList.clear();
if (start == end) {
stationsList.push_back(start);
stationsList.push_back(end);
return true;
}
createGraph();
std::vector<int> path(stations.size(), -1);
std::vector<double> dist(stations.size(), INF);
dist[start] = 0;
std::priority_queue<Node, std::vector<Node>, std::greater<Node>> pq;
pq.push(Node(start, 0));
while (!pq.empty()) {
Node top = pq.top();
pq.pop();
if (top.stationID == end) {
break;
}
for (int i = 0; i < graph[top.stationID].size(); ++i) {
Node &adjNode = graph[top.stationID][i];
if (top.distance + adjNode.distance < dist[adjNode.stationID]) {
path[adjNode.stationID] = top.stationID;
dist[adjNode.stationID] = top.distance + adjNode.distance;
pq.push(Node(adjNode.stationID, dist[adjNode.stationID]));
}
}
}
if (path[end] == -1) {
return false;
}
int p = end;
while (path[p] != -1) {
stationsList.push_front(p);
edgesList.push_front(Edge(path[p], p));
p = path[p];
}
stationsList.push_front(start);
return true;
}
// bfs获取最少换乘的线路
bool Graph::queryTransferMinTransfer(int start,
int end,
QList<int> &stationsList,
QList<Edge> &edgesList)
{
stationsList.clear();
edgesList.clear();
if (start == end) {
stationsList.push_back(start);
stationsList.push_back(end);
return true;
}
std::vector<bool> linesVisted(lines.size(), false);
std::vector<int> path(stations.size(), -1);
path[start] = -2;
std::queue<int> q;
q.push(start);
while (!q.empty()) {
int top = q.front();
q.pop();
for (auto l = stations[top].belongLines.begin(); l != stations[top].belongLines.end(); l++) {
if (!linesVisted[*l]) {
linesVisted[*l] = true;
for (auto s = lines[*l].stationsSet.begin(); s != lines[*l].stationsSet.end(); s++) {
if (path[*s] == -1) {
path[*s] = top;
q.push(*s);
}
}
}
}
}
if (path[end] == -1) {
return false;
} else {
int p = end;
while (path[p] != -2) {
stationsList.push_front(p);
edgesList.push_front(Edge(path[p], p));
p = path[p];
}
stationsList.push_front(start);
return true;
}
}
// 清空数据
void Graph::clearData()
{
stations.clear();
lines.clear();
stationsHash.clear();
linesHash.clear();
edges.clear();
graph.clear();
}
// 插入一条边
bool Graph::insertEdge(int n1, int n2)
{
if (edges.contains(Edge(n1, n2)) || edges.contains(Edge(n2, n1))) {
return false;
}
edges.insert(Edge(n1, n2));
return true;
}
// 生成图结构
void Graph::createGraph()
{
graph.clear();
graph = QVector<QVector<Node>>(stations.size(), QVector<Node>());
for (auto e = edges.begin(); e != edges.end(); e++) {
double dist = stations[e->first].distance(stations[e->second]);
graph[e->first].push_back(Node(e->second, dist));
graph[e->second].push_back(Node(e->first, dist));
}
}
// 获取线路颜色
QColor Graph::getLineColor(int l)
{
return lines[l].color;
}
// 获取线路名
QString Graph::getLineName(int l)
{
return lines[l].name;
}
// 获取线路hash值
int Graph::getLineHash(QString lineName)
{
return linesHash.contains(lineName) ? linesHash[lineName] : -1;
}
// 获取线路集合hash值
QList<int> Graph::getLinesHash(QList<QString> linesList)
{
QList<int> hashList;
for (auto &a : linesList) {
hashList.push_back(getLineHash(a));
}
return hashList;
}
// 获取线路名集合
QList<QString> Graph::getLinesNameList()
{
QList<QString> linesNameList;
for (auto l = lines.begin(); l != lines.end(); l++) {
linesNameList.push_back(l->name);
}
return linesNameList;
}
// 获取线路的所有包含站点
QList<QString> Graph::getLineStationsList(int l)
{
QList<QString> stationsList;
for (auto s = lines[l].stationsSet.begin(); s != lines[l].stationsSet.end(); s++) {
stationsList.push_back(stations[*s].name);
}
return stationsList;
}
// 更新边界经纬度
void Graph::updateMinMaxLongiLati()
{
double minLongitude = 200, minLatitude = 200;
double maxLongitude = 0, maxLatitude = 0;
for (auto &s : stations) {
minLongitude = std::min(minLongitude, s.longitude);
minLatitude = std::min(minLatitude, s.latitude);
maxLongitude = std::max(maxLongitude, s.longitude);
maxLatitude = std::max(maxLatitude, s.latitude);
}
Station::minLongitude = minLongitude;
Station::minLatitude = minLatitude;
Station::maxLongitude = maxLongitude;
Station::maxLatitude = maxLatitude;
}
// 获取站点最小坐标
QPointF Graph::getMinCoord()
{
return QPointF(Station::minLongitude, Station::minLatitude);
}
// 获取站点最大坐标
QPointF Graph::getMaxCoord()
{
return QPointF(Station::maxLongitude, Station::maxLatitude);
}
// 获取两个站点的公共所属线路
QList<int> Graph::getCommonLines(int s1, int s2)
{
QList<int> linesList;
for (auto s = stations[s1].belongLines.begin(); s != stations[s1].belongLines.end(); ++s) {
if (stations[s2].belongLines.contains(*s))
linesList.push_back(*s);
}
return linesList;
}
// 获取站点名
QString Graph::getStationName(int s)
{
return stations[s].name;
}
// 获取站点地理坐标
QPointF Graph::getStationCoord(int s)
{
return QPointF(stations[s].longitude, stations[s].latitude);
}
// 获取站点所属线路信息
QList<int> Graph::getStationLinesInfo(int s)
{
QList<int> returnList;
for (auto i = stations[s].belongLines.begin(); i != stations[s].belongLines.end(); ++i) {
returnList.push_back(*i);
}
return returnList;
}
// 获取站点id
int Graph::getStationHash(QString stationName)
{
return stationsHash.contains(stationName) ? stationsHash[stationName] : -1;
}
// 获取站点集合id
QList<QString> Graph::getStationsNameList()
{
QList<QString> list;
for (auto &s : stations) {
list.push_back(s.name);
}
return list;
}
// 获取网络结构,用于显示
void Graph::getGraph(QList<int> &stationsList, QList<Edge> &edgesList)
{
stationsList.clear();
for (int i = 0; i < stations.size(); ++i) {
stationsList.push_back(i);
}
for (auto e = edges.begin(); e != edges.end(); e++) {
edgesList.push_back(*e);
}
}