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
|
void dijkstra(std::pair<LONG, LONG> start, std::pair<LONG, LONG> end){
struct Vertex vStart;
struct Vertex vEnd;
if(vertices.find(start) == vertices.end()){
try{
Platforms* startPlat = *getPlatform(start.first, start.second);
vStart = Vertex(start.first, start.second);
vStart.id = 0;
add_log("Start %d X: %d Y: %d", vStart.id, vStart.x, vStart.y);
vertices.insert(std::make_pair(start, vStart));
struct vEdge eStartLeft = vEdge(startPlat->x1, startPlat->y1, vStart.x, vStart.y, false, NONE, pDist(startPlat->x1, startPlat->y1, vStart.x, vStart.y), 1);
struct vEdge eStartRight = vEdge(startPlat->x2, startPlat->y2, vStart.x, vStart.y, false, NONE, pDist(startPlat->x2, startPlat->y2, vStart.x, vStart.y), 1);
vedges.insert(std::make_pair(std::make_pair(eStartLeft.x1, eStartLeft.y1), eStartLeft));
vedges.insert(std::make_pair(std::make_pair(eStartRight.x1, eStartRight.y1), eStartRight));
vedges.insert(std::make_pair(std::make_pair(eStartLeft.x2, eStartLeft.y2), eStartLeft));
vedges.insert(std::make_pair(std::make_pair(eStartRight.x2, eStartRight.y2), eStartRight));
}catch(...){
MessageBoxA(NULL, "Couldn't find starting platform", "Error", 0);
}
}
if(vertices.find(end) == vertices.end()){
try{
Platforms* endPlat = *getPlatform(end.first, end.second);
vEnd = Vertex(end.first, end.second);
vEnd.id = vertices.size();
add_log("End %d X: %d Y: %d", vEnd.id, vEnd.x, vEnd.y);
vertices.insert(std::make_pair(end, vEnd));
struct vEdge eEndLeft = vEdge(endPlat->x1, endPlat->y1, vEnd.x, vEnd.y, false, NONE, pDist(endPlat->x1, endPlat->y1, vEnd.x, vEnd.y), 1);
struct vEdge eEndRight = vEdge(endPlat->x2, endPlat->y2, vEnd.x, vEnd.y, false, NONE, pDist(endPlat->x2, endPlat->y2, vEnd.x, vEnd.y), 1);
vedges.insert(std::make_pair(std::make_pair(eEndLeft.x1, eEndLeft.y1), eEndLeft));
vedges.insert(std::make_pair(std::make_pair(eEndRight.x1, eEndRight.y1), eEndRight));
vedges.insert(std::make_pair(std::make_pair(eEndLeft.x2, eEndLeft.y2), eEndLeft));
vedges.insert(std::make_pair(std::make_pair(eEndRight.x2, eEndRight.y2), eEndRight));
}catch(...){
MessageBoxA(NULL, "Couldn't find ending platform", "Error", 0);
}
}
if(vertices.find(start) != vertices.end() && vertices.find(end) != vertices.end()){
std::vector<Vertex>vVec;
vVec.resize(vertices.size());
add_log("vVec size: %d", vertices.size());
for(auto i:vertices){
vVec[i.second.id] = i.second;
//add_log("vertex id: %d x: %d y: %d", i.second.id, i.second.x, i.second.y);
}
for(auto j:vVec){
add_log("vertex id: %d x: %d y: %d", j.id, j.x, j.y);
}
int n = vedges.size();
std::vector<double>minDist;
std::vector<int>prev;
prev.resize(n, -1);
minDist.resize(n, inf);
minDist[0] = 0;
std::set<std::pair<double, int>>vertex_queue;
vertex_queue.insert(std::make_pair(minDist[0], 0));
while(!vertex_queue.empty()){
double dist = vertex_queue.begin()->first;
int u = vertex_queue.begin()->second;
auto its = vedges.equal_range(std::make_pair(vVec[u].x, vVec[u].y));
for (auto it = its.first; it != its.second; ++it) {
int v;
double weight;
double distance_through_u;
if(it->second.x1 == vVec[u].x && it->second.y1 == vVec[u].y){
v = vertices.at(std::make_pair(it->second.x2, it->second.y2)).id;
weight = it->second.weight;
distance_through_u = it->second.dist + weight;
}else if(it->second.x2 == vVec[u].x && it->second.y2 == vVec[u].y){
v = vertices.at(std::make_pair(it->second.x1, it->second.y1)).id;
weight = it->second.weight;
distance_through_u = it->second.dist + weight;
}
add_log("distance_through_u: %f", distance_through_u);
add_log("Vertex ID: %d X: %d Y: %d", vVec[v].id, vVec[v].x, vVec[v].y);
if(distance_through_u < minDist[v]){
vertex_queue.erase(std::make_pair(minDist[v], v));
minDist[v] = distance_through_u;
prev[v] = u;
vertex_queue.insert(std::make_pair(minDist[v], v));
}
}
}
}
}
| |