Naxocist

Escape

Apr 27th, 2022
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define INF 1e9
  5.  
  6. using ll = long long ;
  7. using pid = pair<int, double>;
  8. using pi = pair<pid, int>;
  9.  
  10. const int N = 1e5 + 3;
  11. vector<pid> G[N];
  12. pid dist[N];
  13. int par[N];
  14.  
  15.  
  16. int main(){
  17.     ios_base::sync_with_stdio(false); cin.tie(nullptr);
  18.  
  19.     int n, m, s, t; cin >> n >> m >> s >> t;
  20.  
  21.     for(int i=0; i<m; ++i){
  22.         int u, v, w;
  23.         cin >> u >> v >> w;
  24.         G[u].push_back({v, w/100.0});
  25.         G[v].push_back({u, w/100.0});
  26.     }
  27.  
  28.     for(int i=0; i<=n; ++i){
  29.         dist[i] = {INF, 0};
  30.     }
  31.  
  32.     dist[s] = {1, 0};
  33.     priority_queue<pi, vector<pi>, greater<pi>> pq;
  34.     pq.emplace(dist[s], s);
  35.    
  36.     while(pq.size()){
  37.         auto f = pq.top();
  38.         int u = f.second, uw = f.first.first;
  39.         double up = f.first.second;
  40.         pq.pop();
  41.  
  42.         for(auto x : G[u]){
  43.             int v = x.first;
  44.             double p = x.second;
  45.             int vw = dist[v].first;
  46.             double vp = dist[v].second;
  47.  
  48.             int nw = uw + 1;
  49.             double np = 1 - (1 - up) * (1 - p);
  50.             if(nw < vw || (nw == vw && np < vp)){
  51.                 dist[v].first = nw; dist[v].second = np;
  52.                 par[v] = u;
  53.                 pq.emplace(dist[v], v);
  54.             }
  55.         }
  56.     }
  57.    
  58.     int ansW = dist[t].first;
  59.     double ansP = dist[t].second;
  60.     stack<int> path;
  61.     path.push(t);
  62.     while(t != s) { // backtrack to s
  63.         t = par[t];
  64.         path.push(t);
  65.     }
  66.  
  67.     cout << fixed << setprecision(6) << ansW << ' ' << ansP << '\n';
  68.  
  69.     while(path.size()){
  70.         cout << path.top() << ' ';
  71.         path.pop();
  72.     }
  73.     return 0;
  74.  
  75. }
  76.  
  77. /*
  78. 4 4
  79. 1 3
  80. 1 2 50
  81. 2 3 50
  82. 1 4 10
  83. 4 3 10
  84. */
Advertisement
Add Comment
Please, Sign In to add comment