-
Notifications
You must be signed in to change notification settings - Fork 3
/
2642.cpp
48 lines (43 loc) · 1.35 KB
/
2642.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
class Graph {
public:
int N;
vector<vector<pair<int, int>>> adjacency;
Graph(int n, vector<vector<int>>& edges) {
this->N = n;
adjacency.resize(n);
for (auto& edge : edges) {
adjacency[edge[0]].push_back({edge[1], edge[2]});
}
}
void addEdge(vector<int> edge) {
adjacency[edge[0]].push_back({edge[1], edge[2]});
}
int shortestPath(int node1, int node2) {
vector<int> costs(N, INT_MAX);
vector<bool> visited(N, false);
priority_queue<pair<int, int>> pq;
pq.push({0, node1});
costs[node1] = 0;
while (!pq.empty()) {
auto [cost, node] = pq.top();
visited[node] = true;
cost = -cost;
pq.pop();
for (auto& [neighbor, pathCost] : adjacency[node]) {
int newCost = cost + pathCost;
if (newCost < costs[neighbor]) {
costs[neighbor] = newCost;
if (!visited[neighbor]) pq.push({-newCost, neighbor});
}
}
}
if (costs[node2] == INT_MAX) return -1;
return costs[node2];
}
};
/**
* Your Graph object will be instantiated and called as such:
* Graph* obj = new Graph(n, edges);
* obj->addEdge(edge);
* int param_2 = obj->shortestPath(node1,node2);
*/