Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ll long long
- #define ld long double
- #define ull unsigned long long
- #define pb push_back
- #define ppb pop_back
- #define pf push_front
- #define ppf pop_front
- #define mp make_pair
- #define F first
- #define S second
- #define PI 3.1415926535897932384626
- #define sz(x) ((int)(x).size())
- #define vset(v, n, val) v.clear(); v.resize(n, val)
- typedef pair<int, int> pii;
- typedef pair<ll, ll> pll;
- typedef vector<int> vi;
- typedef vector<ll> vll;
- typedef vector<ull> vull;
- typedef vector<bool> vb;
- typedef vector<char> vc;
- typedef vector<string> vs;
- typedef vector<pii> vpii;
- typedef vector<pll> vpll;
- typedef vector<vi> vvi;
- typedef vector<vll> vvll;
- typedef vector<vull> vvull;
- typedef vector<vb> vvb;
- typedef vector<vc> vvc;
- typedef vector<vs> vvs;
- const double eps = 1e-11;
- class Solution {
- public:
- // to store the input graph
- vector<vector<pair<int, double>>> g;
- // n = #vertices, m = #edges in the input graph
- int n, m;
- double dijkstra_modified(int src, int dst) {
- // d[i] stores the maximum success probability of ith node from given src
- vector<double> d(n);
- // to keep track of visited nodes
- vb vis(n, 0);
- // initialising the distances of all nodes from src as infinity
- for(int i = 0; i < n; i++) d[i] = 0.0;
- // the maximum success probability of src from itself = 1.0
- d[src] = 1.0;
- // priority queue (max heap) to repeatedly find out the node having
- // maximum success probability from src
- priority_queue<pair<double, int>> q;
- // inserting the src to initialise the process
- q.push({1.0, src});
- while(!q.empty()) {
- // extract the node which is currently at minimum distance from src
- int v = q.top().S;
- double mx = q.top().F;
- q.pop();
- if(v == dst) return mx;
- if(vis[v] or abs(mx - 0.0) <= eps) continue;
- vis[v] = true;
- for(auto x: g[v]) {
- if((mx * x.S) > d[x.F]) {
- d[x.F] = mx * x.S;
- q.push({d[x.F], x.F});
- }
- }
- }
- return 0.0;
- }
- double maxProbability(int nodes, vector<vector<int>>& edges, vector<double>& succProb, int start, int end) {
- n = nodes;
- m = sz(edges);
- g.clear(); g.resize(n);
- for(int i = 0; i < m; i++) {
- int x = edges[i][0], y = edges[i][1]; double wt = succProb[i];
- g[x].pb({y, wt});
- g[y].pb({x, wt});
- }
- return dijkstra_modified(start, end);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement