Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const double INF = 1.0, EPS = 0.0000001;
- int main()
- {
- int n, m, s, f, x, y;
- double p;
- cin >> n >> m;
- cin >> s >> f;
- vector<vector<pair<int, double> > > g(n + 1);
- for (int i = 0; i < m; ++i) {
- cin >> x >> y >> p;
- g[x].push_back({y, (1.0 - p / 100.0)});
- g[y].push_back({x, (1.0 - p / 100.0)});
- }
- vector<bool> used(n + 1, 0);
- vector<int> parents(n + 1, -1), ans;
- vector<double> dist(n + 1, -INF);
- deque<int> deq1, deq2;
- deq1.push_back(s);
- dist[s] = 1.0;
- parents[s] = s;
- while (!deq1.empty() || !deq2.empty()) {
- //cout << 1 << "\n";
- if (!deq1.empty()) {
- while (!deq1.empty()) {
- x = deq1.front();
- deq1.pop_front();
- if (x == f) {
- //cout << 3;
- int tmp = x;
- while (parents[tmp] != tmp) {
- ans.push_back(tmp);
- tmp = parents[tmp];
- }
- ans.push_back(tmp);
- cout << ans.size() << " ";
- //cout << dist[f] << "\n";
- cout << fixed;
- cout.precision(7);
- cout << min(0.999999, 1.0 - dist[f]) << "\n";
- for (int i = ans.size() - 1; i >= 0; --i) {
- cout << ans[i] << " ";
- }
- return 0;
- }
- if (used[x])
- continue;
- used[x] = 1;
- for (int i = 0; i < g[x].size(); ++i) {
- int u = g[x][i].first;
- double d = g[x][i].second;
- if (!used[u] && dist[x] * d - dist[u] > EPS) {
- deq2.push_front(u);
- dist[u] = dist[x] * d;
- //cout << dist[u] << " " << ceil(dist[u] * 10000000) / 10000000 << "\n";
- dist[u] = round(dist[u] * 10000000) / 10000000;
- //cout << dist[u] << "\n";
- parents[u] = x;
- }
- }
- }
- }
- else {
- while (!deq2.empty()) {
- x = deq2.front();
- deq2.pop_front();
- if (x == f) {
- //cout << 4;
- int tmp = x;
- while (parents[tmp] != tmp) {
- ans.push_back(tmp);
- tmp = parents[tmp];
- }
- ans.push_back(tmp);
- cout << ans.size() << " ";
- //cout << dist[f] << "\n";
- cout << fixed;
- cout.precision(7);
- cout << min(0.999999, 1.0 - dist[f]) << "\n";
- for (int i = ans.size() - 1; i >= 0; --i) {
- cout << ans[i] << " ";
- }
- return 0;
- }
- if (used[x])
- continue;
- used[x] = 1;
- for (int i = 0; i < g[x].size(); ++i) {
- int u = g[x][i].first;
- double d = g[x][i].second;
- if (!used[u] && dist[x] * d - dist[u] > EPS) {
- deq1.push_front(u);
- dist[u] = dist[x] * d;
- //cout << dist[u] << " " << ceil(dist[u] * 10000000) / 10000000 << "\n";
- dist[u] = round(dist[u] * 10000000) / 10000000;
- //cout << dist[u] << "\n";
- parents[u] = x;
- }
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement