Advertisement
den4ik2003

Untitled

Apr 29th, 2022
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. using std::vector;
  6.  
  7. const int kMaxWeight = 10;
  8. const int kInf = 1e9;
  9.  
  10. void BFS(vector<vector<std::pair<int, int>>>& neighbours,
  11.                 int start, int n, int& ans, vector<vector<int>>& m) {
  12.   vector<std::queue<int>> q(kMaxWeight * n + 1);
  13.   vector<int> distance(n, kInf);
  14.   vector<bool> used(n, false);
  15.   distance[start] = 0;
  16.   q[0].push(start);
  17.   for (int num_of_q = 0; num_of_q <= kMaxWeight * n; ++num_of_q) {
  18.     while (!q[num_of_q].empty()) {
  19.       int open_v = q[num_of_q].front();
  20.       q[num_of_q].pop();
  21.       if (used[open_v]) {
  22.         continue;
  23.       }
  24.       used[open_v] = true;
  25.       for (auto to : neighbours[open_v]) {
  26.         if (distance[to.first] != kInf and !used[to.first]) {
  27.           ans = std::min(ans, m[open_v][to.first] + (distance[open_v] + distance[to.first]));  // cycle
  28.         }
  29.         if (distance[to.first] > m[open_v][to.first] + distance[open_v]) {  // upgrade best distance
  30.           distance[to.first] = m[open_v][to.first] + distance[open_v];
  31.           q[distance[to.first]].push(to.first);
  32.         }
  33.       }
  34.     }
  35.   }
  36. }
  37.  
  38. //void remove(vector<std::pair<int, int>>& v, int first_value, int second_value) {
  39. //  for (int i = 0; i < v.size(); ++i) {
  40. //    if (v[i].first == first_value and v[i].second == second_value) {
  41. //      std::swap(v[i], v[v.size() - 1]);
  42. //      v.pop_back();
  43. //      break;
  44. //    }
  45. //  }
  46. //}
  47.  
  48. int main() {
  49.   std::ios_base::sync_with_stdio(false);
  50.   std::cin.tie(nullptr);
  51.  
  52.   int n, m;
  53.   std::cin >> n >> m;
  54.   std::vector<std::vector<int>> cheapest_edge(n, std::vector<int>(n, kInf));
  55.   std::vector<std::vector<int>> sec_cheapest_edge(n, std::vector<int>(n, kInf));
  56.   vector<vector<std::pair<int, int>>> neighbours(n);
  57.  
  58.   for (int k = 0; k < m; ++k) {
  59.     int first, second, weight;
  60.     std::cin >> first >> second >> weight;
  61.     if (cheapest_edge[first - 1][second - 1] > weight) {
  62.       sec_cheapest_edge[second - 1][first - 1] = cheapest_edge[second - 1][first - 1];
  63.       sec_cheapest_edge[first - 1][second - 1] = cheapest_edge[first - 1][second - 1];
  64.       cheapest_edge[first - 1][second - 1] = weight;
  65.       cheapest_edge[second - 1][first - 1] = weight;
  66.     } else if (sec_cheapest_edge[first - 1][second - 1] > weight) {
  67.       sec_cheapest_edge[second - 1][first - 1] = weight;
  68.       sec_cheapest_edge[first - 1][second - 1] = weight;
  69.     }
  70.     neighbours[first - 1].push_back({second - 1, weight});
  71.     neighbours[second - 1].push_back({first - 1, weight});
  72.   }
  73.  
  74.   int answer = kInf;
  75.   for (int v = 0; v < n; ++v) {
  76.     BFS(neighbours, v, n, answer, cheapest_edge);
  77.   }
  78.  
  79.   for (int i = 0; i < n; ++i) {
  80.     for(int k = 0; k < n; ++k){
  81.       if (answer > sec_cheapest_edge[i][k] + cheapest_edge[i][k]) {
  82.         answer = sec_cheapest_edge[i][k] + cheapest_edge[i][k];  // simple cycle
  83.       }
  84.     }
  85.   }
  86.   std::cout << answer << "\n";
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement