Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Public and published.
- * Written by Gabriel Hearot <gabriel@hearot.it>.
- * See https://training.olinfo.it/#/task/dijkstra/statement.
- * https://forum.olinfo.it/t/execution-killed-with-signal-11-su-dijkstra/6236.
- */
- #include <fstream>
- #include <queue>
- #include <vector>
- #define int long long
- using namespace std;
- int32_t main() {
- int N, M;
- int src, dest, weight;
- int departure, arrival;
- ifstream cin("input.txt");
- ofstream cout("output.txt");
- cin >> N >> M;
- cin >> departure >> arrival;
- departure--, arrival--;
- vector<vector<pair<int, int>>> graph(N);
- vector<int> weights(N, -1);
- priority_queue<int, vector<pair<int, int>>, greater<pair<int, int>>> pq;
- while (M--) {
- cin >> src >> dest >> weight;
- graph[src - 1].push_back({dest - 1, weight});
- }
- weights[departure] = 0;
- pq.push({0, departure});
- while (!pq.empty()) {
- auto current = pq.top();
- if (current.second == arrival) {
- cout << current.first << endl;
- return 0;
- }
- pq.pop();
- for (auto e : graph[current.second]) {
- if (weights[e.first] == -1 ||
- weights[e.first] > e.second + current.first) {
- weights[e.first] = e.second + current.first;
- pq.push({weights[e.first], e.first});
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement