Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <cmath>
- #include <algorithm>
- #include <vector>
- using namespace std;
- struct node {
- int idx;
- int distance;
- node() {}
- node(int _idx, int _distance) {
- idx = _idx;
- distance = _distance;
- }
- bool operator < (const node &tmp) const {
- return distance > tmp.distance;
- }
- };
- int main() {
- int n; // broj na teminja
- int m; // brojot na rebra
- cin >> n >> m;
- vector<pair<int, int> > graph[n + 1];
- for(int i = 0; i < m; i++) {
- int a, b, c; // povrzani se teminjata a i b so distanca c
- cin >> a >> b >> c;
- graph[a].push_back(make_pair(b, c));
- graph[b].push_back(make_pair(a, c));
- }
- int S, E;
- // S --> pocetno teme
- // E --> krajno teme
- cin >> S >> E;
- priority_queue<node> pq;
- pq.push(node(S, 0)); // prviot element vo priority queue
- bool visited[n + 1];
- int dist[n + 1];
- for(int i = 0; i < n; i++) {
- visited[i] = false;
- dist[i] = 2000000000;
- }
- dist[S] = 0;
- while(!pq.empty()) {
- node c = pq.top();
- pq.pop();
- visited[c.idx] = true;
- for(int i = 0; i < graph[c.idx].size(); i++) {
- int sosed = graph[c.idx][i].first;
- int rebro = graph[c.idx][i].second;
- if(!visited[sosed] and c.distance + rebro < dist[sosed]) {
- dist[sosed] = c.distance + rebro;
- pq.push(node(sosed, c.distance + rebro));
- }
- }
- }
- for(int i = 0; i < n; i++) {
- cout << S << " --> " << i << " " << dist[i] << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement