Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define OO 10000000
- int n, m;
- vector<vector<pair<int, int>>> G;
- int dijkstra(int source, int dst) {
- vector<int> dist(n + 2, OO);
- priority_queue<pair<int, int>> pq;
- dist[source] = 0;
- pq.push({0, source});
- while(!pq.empty()) {
- auto [d, v] = pq.top();
- d *= -1;
- pq.pop();
- if(d > dist[v])
- continue;
- for(auto [u, cost] : G[v]) {
- if(dist[u] > dist[v] + cost) {
- dist[u] = dist[v] + cost;
- pq.push({-dist[u], u});
- }
- }
- }
- return dist[dst];
- }
- int main(){
- cin >> n >> m;
- G.resize(n + 2);
- for(int i = 0; i < m; ++i) {
- int u, v, w;
- cin >> u >> v >> w;
- G[u].push_back({v, w});
- G[v].push_back({u, w});
- }
- cout << dijkstra(0, n + 1) << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement