Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <iomanip>
- #include <vector>
- #include <queue>
- #include <algorithm>
- using namespace std;
- int n;
- vector<vector<pair<int, double>>> g;
- vector<bool> used;
- vector<double> probability;
- void dijkstra(int start = 0) {
- probability[start] = 1;
- for (int i = 0; i < n; i++) {
- // n итераций
- int max_v = -1;
- double max_value = 0;
- for (int j = 0; j < n; j++) {
- if (probability[j] >= max_value && !used[j]) {
- max_v = j;
- max_value = probability[j];
- }
- }
- if (max_v == -1)
- return;
- used[max_v] = true;
- for (auto &j: g[max_v]) {
- probability[j.first] = max(probability[max_v] * j.second, probability[j.first]);
- }
- }
- }
- int main() {
- int m, s, f;
- cin >> n >> m >> s >> f;
- s--; f--;
- g = vector<vector<pair<int, double>>>(n);
- probability = vector<double>(n, 0);
- used = vector<bool>(n);
- for (int i = 0; i < m; i++){
- int fr, to;
- double value;
- cin >> fr >> to >> value;
- fr--;to--;
- g[fr].emplace_back(to, 1 - value / 100);
- g[to].emplace_back(fr, 1 - value / 100);
- }
- dijkstra(s);
- if (!used[f])
- cout << -1;
- else
- cout << fixed << 1 - probability[f];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement