-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathgetshorty.cc
38 lines (35 loc) · 855 Bytes
/
getshorty.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// https://open.kattis.com/problems/getshorty
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0), ios::sync_with_stdio(0);
int n, m, u, v;
double w;
while (1) {
cin >> n >> m;
if (!n) break;
vector<vector<tuple<int, double>>> g(n);
for (int i = 0; i < m; i++) {
cin >> u >> v >> w;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
priority_queue<tuple<double, int>> q;
q.push({1, 0});
vector<bool> visited(n);
vector<double> d(n);
d[0] = 1;
while (!q.empty()) {
tie(w, u) = q.top();
q.pop();
if (visited[u]) continue;
visited[u] = 1;
if (u == n - 1) {
cout << setprecision(4) << fixed << w << '\n';
break;
}
for (auto [v, x] : g[u])
if (d[v] < w * x) d[v] = w * x, q.push({d[v], v});
}
}
}