STANAANDREY

pb2 3/2/2021

Mar 2nd, 2021 (edited)
621
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <queue>
  3. #include <list>
  4. #include <algorithm>
  5. #include <limits>
  6. using namespace std;
  7. const int NMAX = 103;
  8. const int OO = numeric_limits<int>::max();
  9. int n, m, k, disMin = OO, closest;
  10. list<pair<int, int>> edges[NMAX];
  11. int dis[NMAX];
  12. struct QElem {
  13.     int to, cost;
  14.     QElem(int to, int cost) : to(to), cost(cost) {}
  15.     bool operator < (const QElem &other) const noexcept {
  16.         return this->cost > other.cost;
  17.     }
  18. };
  19.  
  20. void read() {
  21.     cin >> n >> m >> k;
  22.     for (int i = 0; i < m; i++) {
  23.         int a, b, c;
  24.         cin >> a >> b >> c;
  25.         edges[a].push_back(make_pair(b, c));
  26.     }
  27. }
  28.  
  29. void dijkstra() {
  30.     priority_queue<QElem> q;
  31.     q.push(QElem(k, 0));
  32.     dis[k] = 0;
  33.     while (!q.empty()) {
  34.         int node = q.top().to;
  35.         q.pop();
  36.         for (auto it : edges[node]) {
  37.             auto [to, cost] = it;
  38.             if (dis[to] > dis[node] + cost) {
  39.                 dis[to] = dis[node] + cost;
  40.                 q.push(QElem(to, dis[to]));
  41.             }
  42.         }
  43.     }
  44. }
  45.  
  46. void solveA() {
  47.     cout << "Orase accesibile: ";
  48.     for (int i = 1; i <= n; i++) {
  49.         if (dis[i] != OO) {
  50.             cout << i << ' ';
  51.         }
  52.         if (i != k && disMin > dis[i]) {
  53.             disMin = dis[i];
  54.             closest = i;
  55.         }
  56.     }
  57.     cout << endl;
  58. }
  59.  
  60. void dfs(int node, int cdis, list<int> road, int nrc) {
  61.     if (dis[node] == cdis) {
  62.         for (int it : road) {
  63.             cout << it << ' ';
  64.         }
  65.         cout << endl;
  66.     }
  67.     if (nrc == 4) {
  68.         cout << "oras aflat la distanta 3" << node << "dis:" << dis[node] << endl;
  69.     }
  70.     for (auto it : edges[node]) {
  71.         auto [to, cost] = it;
  72.         road.push_back(to);
  73.         dfs(to, cdis + cost, road, nrc + 1);
  74.     }
  75. }
  76.  
  77. int main() {
  78.     read();
  79.     fill(dis + 1, dis + n + 1, OO);
  80.     dijkstra();
  81.     solveA();
  82.     cout << "cel mai apropiat oras: " << closest << endl;
  83.     cout << "drumuri: " << endl;
  84.     dfs(k, 0, {k}, 0);
  85.     return 0;
  86. }
  87.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×