-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem1.cpp
More file actions
88 lines (87 loc) · 2.29 KB
/
Copy pathproblem1.cpp
File metadata and controls
88 lines (87 loc) · 2.29 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <cmath>
#include <cstring>
#include <numeric>
#include <limits>
using namespace std;
#include "graph.hpp"
#include "kml_generator.hpp"
struct DijkstraResult
{
vector<double> dist;
vector<int> parent;
};
DijkstraResult dijkstraDistance(int source)
{
int n = graph.size();
vector<double> dist(n, 1e18);
vector<int> parent(n, -1);
priority_queue<pair<double,int>, vector<pair<double,int>>, greater<pair<double,int>>> pq;
dist[source] = 0;
pq.push({0, source});
while(!pq.empty())
{
pair<double,int> top = pq.top();
pq.pop();
double curDist = top.first;
int u = top.second;
if(curDist > dist[u]) continue;
for(auto &edge : graph[u])
{
if(edge.mode != CAR) continue;
if(curDist + edge.distance < dist[edge.to])
{
dist[edge.to] = curDist + edge.distance;
parent[edge.to] = u;
pq.push({dist[edge.to], edge.to});
}
}
}
return {dist, parent};
}
int main()
{
readCsv("Roadmap-Dhaka.csv", true, CAR);
double slon, slat, dlon, dlat;
cin >> slon >> slat >> dlon >> dlat;
int s = findNearestNode(slon, slat);
int d = findNearestNode(dlon, dlat);
auto result = dijkstraDistance(s);
if(result.dist[d] == 1e18)
{
cout << "No car route\n";
}
else
{
cout << "Shortest distance: " << result.dist[d] << " km\n";
vector<int> path;
int curr = d;
while(curr != -1)
{
path.push_back(curr);
curr = result.parent[curr];
}
reverse(path.begin(), path.end());
vector<pair<double,double>> coords;
for(int nodeId : path)
{
coords.push_back(coordinate[nodeId]);
}
writeSimpleKML(
"problem1_route.kml",
coords,
"Problem 1: Shortest Car Route",
"Distance: " + to_string(result.dist[d]) + " km"
);
cout << "\nKML file created: problem1_route.kml" << endl;
cout << "Upload to https://www.google.com/mymaps to view!" << endl;
}
}