Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const double INF = 1.0, EPS = 0.0000001;
  6.  
  7. int main()
  8. {
  9.     int n, m, s, f, x, y;
  10.     double p;
  11.     cin >> n >> m;
  12.     cin >> s >> f;
  13.  
  14.     vector<vector<pair<int, double> > > g(n + 1);
  15.     for (int i = 0; i < m; ++i) {
  16.         cin >> x >> y >> p;
  17.         g[x].push_back({y, (1.0 - p / 100.0)});
  18.         g[y].push_back({x, (1.0 - p / 100.0)});
  19.     }
  20.  
  21.     vector<bool> used(n + 1, 0);
  22.     vector<int> parents(n + 1, -1), ans;
  23.     vector<double> dist(n + 1, -INF);
  24.  
  25.     deque<int> deq1, deq2;
  26.     deq1.push_back(s);
  27.     dist[s] = 1.0;
  28.     parents[s] = s;
  29.  
  30.     while (!deq1.empty() || !deq2.empty()) {
  31.         //cout << 1 << "\n";
  32.         if (!deq1.empty()) {
  33.             while (!deq1.empty()) {
  34.  
  35.                 x = deq1.front();
  36.                 deq1.pop_front();
  37.  
  38.                 if (x == f) {
  39.                     //cout << 3;
  40.                     int tmp = x;
  41.                     while (parents[tmp] != tmp) {
  42.                         ans.push_back(tmp);
  43.                         tmp = parents[tmp];
  44.                     }
  45.                     ans.push_back(tmp);
  46.  
  47.                     cout << ans.size() << " ";
  48.  
  49.                     //cout << dist[f] << "\n";
  50.                     cout << fixed;
  51.                     cout.precision(7);
  52.                     cout << min(0.999999, 1.0 - dist[f]) << "\n";
  53.  
  54.                     for (int i = ans.size() - 1; i >= 0; --i) {
  55.                         cout << ans[i] << " ";
  56.                     }
  57.                     return 0;
  58.                 }
  59.  
  60.                 if (used[x])
  61.                     continue;
  62.  
  63.                 used[x] = 1;
  64.                 for (int i = 0; i < g[x].size(); ++i) {
  65.                     int u = g[x][i].first;
  66.                     double d = g[x][i].second;
  67.  
  68.                     if (!used[u] && dist[x] * d - dist[u] > EPS) {
  69.                         deq2.push_front(u);
  70.                         dist[u] = dist[x] * d;
  71.                         //cout << dist[u] << " " << ceil(dist[u] * 10000000) / 10000000 << "\n";
  72.                         dist[u] = round(dist[u] * 10000000) / 10000000;
  73.                         //cout << dist[u] << "\n";
  74.                         parents[u] = x;
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.         else {
  80.             while (!deq2.empty()) {
  81.                 x = deq2.front();
  82.                 deq2.pop_front();
  83.  
  84.                 if (x == f) {
  85.                     //cout << 4;
  86.                     int tmp = x;
  87.                     while (parents[tmp] != tmp) {
  88.                         ans.push_back(tmp);
  89.                         tmp = parents[tmp];
  90.                     }
  91.                     ans.push_back(tmp);
  92.  
  93.                     cout << ans.size() << " ";
  94.  
  95.                     //cout << dist[f] << "\n";
  96.                     cout << fixed;
  97.                     cout.precision(7);
  98.                     cout << min(0.999999, 1.0 - dist[f]) << "\n";
  99.  
  100.                     for (int i = ans.size() - 1; i >= 0; --i) {
  101.                         cout << ans[i] << " ";
  102.                     }
  103.                     return 0;
  104.                 }
  105.  
  106.                 if (used[x])
  107.                     continue;
  108.  
  109.                 used[x] = 1;
  110.                 for (int i = 0; i < g[x].size(); ++i) {
  111.                     int u = g[x][i].first;
  112.                     double d = g[x][i].second;
  113.  
  114.                     if (!used[u] && dist[x] * d - dist[u] > EPS) {
  115.                         deq1.push_front(u);
  116.                         dist[u] = dist[x] * d;
  117.                         //cout << dist[u] << " " << ceil(dist[u] * 10000000) / 10000000 << "\n";
  118.                         dist[u] = round(dist[u] * 10000000) / 10000000;
  119.                         //cout << dist[u] << "\n";
  120.                         parents[u] = x;
  121.                     }
  122.                 }
  123.             }
  124.         }
  125.     }
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement