Advertisement
Nik_Perepelov

4 контест 3 задача

Oct 5th, 2021
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. #include <vector>
  5. #include <queue>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. int n;
  11. vector<vector<pair<int, double>>> g;
  12. vector<bool> used;
  13. vector<double> probability;
  14.  
  15. void dijkstra(int start = 0) {
  16.     probability[start] = 1;
  17.     for (int i = 0; i < n; i++) {
  18.         // n итераций
  19.         int max_v = -1;
  20.         double max_value = 0;
  21.         for (int j = 0; j < n; j++) {
  22.             if (probability[j] >= max_value && !used[j]) {
  23.                 max_v = j;
  24.                 max_value = probability[j];
  25.             }
  26.         }
  27.         if (max_v == -1)
  28.             return;
  29.         used[max_v] = true;
  30.         for (auto &j: g[max_v]) {
  31.             probability[j.first] = max(probability[max_v] * j.second, probability[j.first]);
  32.         }
  33.     }
  34. }
  35.  
  36.  
  37. int main() {
  38.     int m, s, f;
  39.     cin >> n >> m >> s >> f;
  40.     s--; f--;
  41.  
  42.     g = vector<vector<pair<int, double>>>(n);
  43.     probability = vector<double>(n, 0);
  44.     used = vector<bool>(n);
  45.  
  46.     for (int i = 0; i < m; i++){
  47.         int fr, to;
  48.         double value;
  49.         cin >> fr >> to >> value;
  50.         fr--;to--;
  51.         g[fr].emplace_back(to, 1 - value / 100);
  52.         g[to].emplace_back(fr, 1 - value / 100);
  53.     }
  54.  
  55.     dijkstra(s);
  56.  
  57.     if (!used[f])
  58.         cout << -1;
  59.     else
  60.         cout << fixed << 1 - probability[f];
  61.  
  62.  
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement